LZMA Streams in Java

The Lempel-Ziv Markov-chain Algorithm is a very effective and relatively fast compression technique used in the ‘7z’ format of the 7-Zip archiver. There are implementations of LZMA in C/C++, Java, C#, Python, and other languages.

I needed a Java implementation of LZMA for a particular project. In Java, there is a quasi-standard for FilterOutputStreams — streams that transparently compress or encrypt data sent to them (and in reverse for FilterInputStreams). Unfortunately, the SevenZip implementation did not conform to this standard. It was written in a completely different (and far more natural!) style.

So, the package net.contrapunctus.lzma provides implementations of LzmaInputStream and LzmaOutputStream that interact with underlying LZMA encoders and decoders running in separate threads. This way I could get the desired interface without having to restructure the LZMA implementation, which would undoubtedly introduce bugs.

Note: These streams are not directly compatible with the 7-zip files! The 7z format is a container for multiple files (and their metadata) that are individually compressed using LZMA. These streams support the compression format but not the container format. It's the same reason that GZipInputStream cannot open .zip files. There are command-line tools lzma and unlzma that compress only, and those are compatible with the LZMA streams.

The Jar file available for download includes the compiled classes for the (unmodified) LZMA SDK and for my interface, so that may be all you need. Try this, for a simple test:

 $ java -cp lzmajio.jar net.contrapunctus.lzma.RoundTrip
It should show some compressed bytes and output a short text message twice.

Please don't hesitate to contact me with any bug reports, questions, or feature requests.

Visit lzmajio on github
Download bundled releases

There is not much documentation, but see this post.

Like the original LZMA SDK, this code is available under the GNU LGPL or the Common Public License.

©20022015 Christopher League