Apache Commons Compress 仅适用于存档文件(如果我错了,请纠正我)。我需要类似的东西
MyDB.put(LibIAmLookingFor.compress("My long string to store"));
String getBack = LibIAmLookingFor.decompress(MyDB.get()));
LZW 只是一个例子,可以是任何类似的东西。谢谢你。
Apache Commons Compress 仅适用于存档文件(如果我错了,请纠正我)。我需要类似的东西
MyDB.put(LibIAmLookingFor.compress("My long string to store"));
String getBack = LibIAmLookingFor.decompress(MyDB.get()));
LZW 只是一个例子,可以是任何类似的东西。谢谢你。
Java 内置了用于 ZIP 压缩的库:
http://docs.oracle.com/javase/6/docs/api/java/util/zip/package-summary.html
那会做你需要的吗?
你有很多选择——
您可以将java.util.Deflater用于 Deflate 算法,
try {
// Encode a String into bytes
String inputString = "blahblahblah??";
byte[] input = inputString.getBytes("UTF-8");
// Compress the bytes
byte[] output = new byte[100];
Deflater compresser = new Deflater();
compresser.setInput(input);
compresser.finish();
int compressedDataLength = compresser.deflate(output);
// Decompress the bytes
Inflater decompresser = new Inflater();
decompresser.setInput(output, 0, compressedDataLength);
byte[] result = new byte[100];
int resultLength = decompresser.inflate(result);
decompresser.end();
// Decode the bytes into a String
String outputString = new String(result, 0, resultLength, "UTF-8");
} catch(java.io.UnsupportedEncodingException ex) {
// handle
} catch (java.util.zip.DataFormatException ex) {
// handle
}
但是您可能更喜欢使用流式压缩器,例如带有GZIPOutputStream的 gzip 。
如果您需要更好的压缩(以速度为代价),您可能需要使用bzip2。
如果您需要更快的速度(以压缩为代价),您可能需要使用lzo。