用于压缩(例如LZW)字符串的Java库

Apache Commons Compress仅适用于存档文件(如果我错了,请纠正我)。 我需要类似的东西

MyDB.put(LibIAmLookingFor.compress("My long string to store")); String getBack = LibIAmLookingFor.decompress(MyDB.get())); 

而LZW只是一个例子,可能是类似的。 谢谢。

你有很多选择 –

您可以将java.util.Deflater用于Deflate algortihm,

 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。

如果您真的想要LZW ,可以使用多种实现方式。

如果您需要更好的压缩(以速度为代价),您可能需要使用bzip2 。

如果你需要更高的速度(以压缩为代价),你可能想要使用lzo 。

Java内置了用于ZIP压缩的库:

http://docs.oracle.com/javase/6/docs/api/java/util/zip/package-summary.html

这会做你需要的吗?