FileInputStream inFile = new FileInputStream("src.tar"); FileOutputStream outfile = new FileOutputStream("src.tar.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();
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(); } }
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; }