LZ4 simply is amazing, with the windows tool provided I get to witness speediness of compressing/decompressing while maintaining a good compression ratio.
However, I just can't seem to implement it into my Java project which involves reading lots of small text files in a folder.
It worked with windows tool provided, but the size of compressed files after I use Java's LZ4, is even larger than original.
I wonder if anyone may help me with putting it to good use in java? thanks!
Here's the sample code which resembles much of my lost code of it.
I've basically used file writer or similar to write compressed byte arrays into .txt or blank files.
I didn't try decompressing as I thought I needed to pass decompressedlength or compressedlength, but I thought it'll be troublesome for ~10 million pieces of text files of variable lengths. Also the compression didn't really work.
LZ4Factory factory = LZ4Factory.fastestInstance();
byte[] data = "12345345234572".getBytes("UTF-8");
final int decompressedLength = data.length;
// compress data
LZ4Compressor compressor = factory.fastCompressor();
int maxCompressedLength = compressor.maxCompressedLength(decompressedLength);
byte[] compressed = new byte[maxCompressedLength];
int compressedLength = compressor.compress(data, 0, decompressedLength, compressed, 0, maxCompressedLength);
// decompress data
// - method 1: when the decompressed length is known
LZ4FastDecompressor decompressor = factory.fastDecompressor();
byte[] restored = new byte[decompressedLength];
int compressedLength2 = decompressor.decompress(compressed, 0, restored, 0, decompressedLength);
// compressedLength == compressedLength2
// - method 2: when the compressed length is known (a little slower)
// the destination buffer needs to be over-sized
LZ4SafeDecompressor decompressor2 = factory.safeDecompressor();
int decompressedLength2 = decompressor2.decompress(compressed, 0, compressedLength, restored, 0);
// decompressedLength == decompressedLength2