/** * lzma 壓縮 * * <p>MODE_1(1, 0, 16, 64, Encoder.EMatchFinderTypeBT2, 3, 0, 2), * * <p>MODE_2(2, 0, 20, 64, Encoder.EMatchFinderTypeBT2, 3, 0, 2), * * <p>MODE_3(3, 1, 19, 64, Encoder.EMatchFinderTypeBT4, 3, 0, 2), * * <p>MODE_4(4, 2, 20, 64, Encoder.EMatchFinderTypeBT4, 3, 0, 2), * * <p>MODE_5(5, 2, 21, 128, Encoder.EMatchFinderTypeBT4, 3, 0, 2), * * <p>MODE_6(6, 2, 22, 128, Encoder.EMatchFinderTypeBT4, 3, 0, 2), * * <p>MODE_7(7, 2, 23, 128, Encoder.EMatchFinderTypeBT4, 3, 0, 2), * * <p>MODE_8(8, 2, 24, 255, Encoder.EMatchFinderTypeBT4, 3, 0, 2), * * <p>MODE_9(9, 2, 25, 255, Encoder.EMatchFinderTypeBT4, 3, 0, 2); * * @param value * @return */ public static byte[] lzma(byte[] value) { byte[] result = new byte[0]; // ByteArrayInputStream in = null; ByteArrayOutputStream out = null; try { Encoder encoder = new Encoder(); in = new ByteArrayInputStream(value); out = new ByteArrayOutputStream(); // fast encoder.SetEndMarkerMode(true); encoder.SetAlgorithm(0); // dictionarysize是主要佔用記憶體的地方(0-30) encoder.SetDictionarySize(1 << 20); // fb是主要速度的地方(越小越快壓縮率越低,5-273) encoder.SetNumFastBytes(64); encoder.SetMatchFinder(Encoder.EMatchFinderTypeBT2); encoder.SetLcLpPb(3, 0, 2); encoder.WriteCoderProperties(out); // encoder.Code(in, out, -1, -1, null); result = out.toByteArray(); } catch (Exception ex) { ex.printStackTrace(); } finally { IoHelper.close(in); IoHelper.close(out); } return result; }
private static byte[] encodeToLZMA(byte[] detailsData) throws IOException { Encoder lzma = new Encoder(); ByteArrayOutputStream lzOut = new ByteArrayOutputStream(); lzma.SetEndMarkerMode(true); lzma.WriteCoderProperties(lzOut); lzOut.write(getBytes(detailsData.length)); lzma.Code( new ByteArrayInputStream(detailsData), lzOut, 0, 0, new ProgressLZMA(detailsData.length)); lzOut.close(); detailsData = lzOut.toByteArray(); return detailsData; }