/** * Creates a new XZ compressor using the default LZMA2 options. * This is equivalent to <code>XZCompressorOutputStream(outputStream, 6)</code>. * @param outputStream the stream to wrap * @throws IOException on error */ public XZCompressorOutputStream(final OutputStream outputStream) throws IOException { out = new XZOutputStream(outputStream, new LZMA2Options()); }
@Override public void write(final byte[] buf, final int off, final int len) throws IOException { out.write(buf, off, len); }
@Override public void close() throws IOException { out.close(); } }
private static byte[] xz(byte[] data, byte[] checksums, File path) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); LZMA2Options options = new LZMA2Options(); options.setPreset(8); XZOutputStream xz = new XZOutputStream(out, options); int x = checksums.length; xz.write(data); xz.write(checksums); xz.write(new byte[]{ (byte) (x & 0x000000FF), (byte)((x & 0x0000FF00) >> 8), (byte)((x & 0x00FF0000) >> 16), (byte)((x & 0xFF000000) >> 24) }); xz.write("SIGN".getBytes()); //Add our sign to validate xz.close(); byte[] xzed = out.toByteArray(); Files.write(xzed, new File(path.getAbsolutePath() + ".pack.xz")); return xzed; }
public static void main(String[] args) throws Exception { // InputStream fis; //File file = new File(DEFAULT_DIR, TEMP_FILE); // This works both within Eclipse project and in runnable JAR //InputStream fis = StandardCompressXz.class.getResourceAsStream("SurfaceMarsMap.dat"); // This works both within Eclipse project and in runnable JAR //InputStream fis = this.getClass().getClassLoader().getResourceAsStream("/map/SurfaceMarsMap.dat"); //fis = this.getClass().getClassLoader().getResourceAsStream("examples/resources/verdana.ttf"); // fis = StandardCompressXz.class.getClassLoader().getResourceAsStream("SurfaceMarsMap.dat.7z"); FileInputStream inFile = new FileInputStream(StandardCompressXz.class.getClassLoader().getResource("/map/SurfaceMarsMap.dat").toExternalForm());//"SurfaceMarsMap.dat"); FileOutputStream outfile = new FileOutputStream("/map/SurfaceMarsMap.xz"); LZMA2Options options = new LZMA2Options(); options.setPreset(7); // play with this number: 6 is default but 7 works better for mid sized archives ( > 8mb) XZOutputStream out = new XZOutputStream(outfile, options); byte[] buf = new byte[8192]; int size; while ((size = inFile.read(buf)) != -1) out.write(buf, 0, size); out.finish(); } }
/** * Finishes compression without closing the underlying stream. * No more data can be written to this stream after finishing. * @throws IOException on error */ public void finish() throws IOException { out.finish(); }
@Override public void run() { try { DataBuffer buffer = writableBuffers.take(); int currentFile = buffer.fileID; byte[][] dataBuffers = buffer.data; FileOutputStream logOutputStream = new FileOutputStream(new File(out + "robotData." + currentFile + ".xz")); XZOutputStream xzOutputStream = new XZOutputStream(logOutputStream, new LZMA2Options(COMPRESSION_LEVEL)); int writtenLength = 0; for (int i = 0; i < getNumberOfVariables(); i++) { writtenLength += dataBuffers[i].length; xzOutputStream.write(dataBuffers[i]); } System.out.println("Wrote " + writtenLength / (1024 * 1024) + " MB"); xzOutputStream.close(); logOutputStream.close(); availableBuffers.put(buffer); } catch (IOException | InterruptedException e) { e.printStackTrace(); } }
/** * Finishes compression without closing the underlying stream. * No more data can be written to this stream after finishing. * @throws IOException on error */ public void finish() throws IOException { out.finish(); }
/** * Creates a new XZ compressor using the specified LZMA2 preset level. * <p> * The presets 0-3 are fast presets with medium compression. * The presets 4-6 are fairly slow presets with high compression. * The default preset is 6. * <p> * The presets 7-9 are like the preset 6 but use bigger dictionaries * and have higher compressor and decompressor memory requirements. * Unless the uncompressed size of the file exceeds 8 MiB, * 16 MiB, or 32 MiB, it is waste of memory to use the * presets 7, 8, or 9, respectively. * @param outputStream the stream to wrap * @param preset the preset * @throws IOException on error */ public XZCompressorOutputStream(final OutputStream outputStream, final int preset) throws IOException { out = new XZOutputStream(outputStream, new LZMA2Options(preset)); }
@Override public void run() { try { DataBuffer buffer = writableBuffers.take(); int currentFile = buffer.fileID; byte[][] dataBuffers = buffer.data; FileOutputStream logOutputStream = new FileOutputStream(new File(targetDirectory, "robotData." + currentFile + ".xz")); XZOutputStream xzOutputStream = new XZOutputStream(logOutputStream, new LZMA2Options(COMPRESSION_LEVEL)); int writtenLength = 0; for (int i = 0; i < getNumberOfVariables(); i++) { writtenLength += buffer.length * 8; xzOutputStream.write(dataBuffers[i], 0, buffer.length * 8); } out.println("Wrote " + writtenLength / (1024 * 1024) + " MB to robotData." + currentFile + ".xz"); xzOutputStream.close(); logOutputStream.close(); availableBuffers.put(buffer); incrementProgressMonitor(); } catch (IOException | InterruptedException e) { e.printStackTrace(); } }
@Override public void write(final int b) throws IOException { out.write(b); }
@Override public void finish() throws IOException { xz.finish(); IOUtils.finishStream(out); } };
@Override public void close() throws IOException { out.close(); } }
break; case COMPRESSION_FORMAT_XZ_LZMA2: compressionOut = new XZOutputStream(bufferedOut, new LZMA2Options()); mimeTypeRef = "application/x-xz"; break;
@Override public void run() { try { DataBuffer buffer = writableBuffers.take(); int currentFile = buffer.fileID; byte[][] dataBuffers = buffer.data; FileOutputStream logOutputStream = new FileOutputStream(new File(targetDirectory, "robotData." + currentFile + ".xz")); XZOutputStream xzOutputStream = new XZOutputStream(logOutputStream, new LZMA2Options(COMPRESSION_LEVEL)); int writtenLength = 0; for (int i = 0; i < getNumberOfVariables(); i++) { writtenLength += buffer.length * 8; xzOutputStream.write(dataBuffers[i], 0, buffer.length * 8); } out.println("Wrote " + writtenLength / (1024 * 1024) + " MB to robotData." + currentFile + ".xz"); xzOutputStream.close(); logOutputStream.close(); availableBuffers.put(buffer); incrementProgressMonitor(); } catch (IOException | InterruptedException e) { e.printStackTrace(); } }
@Override public void write(final byte[] buf, final int off, final int len) throws IOException { out.write(buf, off, len); }
@Override public void finish() throws IOException { xz.finish(); IOUtils.finishStream(out); } };
break; case COMPRESSION_FORMAT_XZ_LZMA2: compressionOut = new XZOutputStream(bufferedOut, new LZMA2Options()); mimeTypeRef.set("application/x-xz"); break;
@Override public void write(final int b) throws IOException { out.write(b); }
@Override public OutputStream chainOutputStream(final OutputStream outputStream, final Path fileName) throws IOException { final FilterOptions filterOptions = new LZMA2Options(LZMA2Options.PRESET_MAX); return new XZOutputStream(outputStream, filterOptions); }