Use SevenZip Java LZMA compression library
This commit is contained in:
@@ -2,21 +2,41 @@
|
||||
|
||||
package com.google.typography.font.compression;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import SevenZip.Compression.LZMA.Encoder;
|
||||
|
||||
/**
|
||||
* @author raph@google.com (Raph Levien)
|
||||
*/
|
||||
public class CompressLzma {
|
||||
// This is currently implemented by shelling out to a command line helper,
|
||||
// which is fine for research purposes, but obviously problematic for
|
||||
// production.
|
||||
|
||||
public static byte[] compress(byte[] input) {
|
||||
try {
|
||||
String[] args = {"/usr/bin/lzma"};
|
||||
CommandResult result = new Command(args).execute(input);
|
||||
return result.getStdout();
|
||||
} catch (CommandException e) {
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(input);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
|
||||
Encoder encoder = new Encoder();
|
||||
encoder.SetAlgorithm(2);
|
||||
encoder.SetDictionarySize(1 << 23);
|
||||
encoder.SetNumFastBytes(128);
|
||||
encoder.SetMatchFinder(1);
|
||||
encoder.SetLcLpPb(3, 0, 2);
|
||||
encoder.SetEndMarkerMode(true);
|
||||
encoder.WriteCoderProperties(out);
|
||||
for (int i = 0; i < 8; i++) {
|
||||
out.write((int) ((long) -1 >>> (8 * i)) & 0xFF);
|
||||
}
|
||||
encoder.Code(in, out, -1, -1, null);
|
||||
|
||||
out.flush();
|
||||
out.close();
|
||||
|
||||
return out.toByteArray();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user