How to make it work in Java?

664 views
Skip to first unread message

Kaiden Sin

unread,
May 19, 2014, 3:56:50 PM5/19/14
to lz...@googlegroups.com
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

Yann Collet

unread,
May 19, 2014, 7:27:54 PM5/19/14
to lz...@googlegroups.com
Hello


Java is not my programming language of experience, so Adrien Grand is probably the right expert to contact for your questions.

However, if I do understand your test program correctly,
I would say that the amount of bytes you try to compress is way too low (about 12 bytes).
It seems normal that it does not compress at all.

For both C/Windows and Java versions, you should experience some visible compression ratio as soon as the filesize becomes "reasonably large", with reasonably large being defined as >= 4KB (obviously, it entirely depends on data content, so this is just an indicative threshold).


Regards

Kaiden Sin

unread,
May 20, 2014, 11:17:29 AM5/20/14
to lz...@googlegroups.com
I see! Thanks! Yup the files are too fragmented : D


--
Vous recevez ce message, car vous êtes abonné à un sujet dans le groupe Google Groupes "LZ4c".
Pour vous désabonner de ce sujet, visitez le site https://groups.google.com/d/topic/lz4c/kU8M1WHcOzU/unsubscribe.
Pour vous désabonner de ce groupe et de tous ses sujets, envoyez un e-mail à l'adresse lz4c+uns...@googlegroups.com.
Pour obtenir davantage d'options, consultez la page https://groups.google.com/d/optout.



--
Yours sincerely,

Kaiden Sin
D-B1-2931-1
University of Macau
Faculty of Science and Technology
Bachelor of Science in  Computer Science
Mobile : (+853) 63932808
Reply all
Reply to author
Forward
0 new messages