Java 7zip compression is too big

Commons Compress is starting a new block in the container file for each archive entry. Note the block counter here:

block-per-file

Not quite the answer you were hoping for, but the docs say it doesn't support "solid compression" - writing several files to a single block. See paragraph 5 in the docs here.

A quick look around found a few other Java libraries that support LZMA compression, but I couldn't spot one that could do so within the parent container file format for 7-Zip. Perhaps someone else knows of an alternative...

It sounds like a normal zip file format (e.g. via ZipOutputStream) is not an option?


I don't have enough rep to comment anymore so here are my thoughts:

  • I don't see where you set the compression ratio so it could be that SevenZOutputFile uses no (or very low) compression. As @CristiFati said, the difference in compression is odd, especially for text files
  • As noted by @df778899, there is no support for solid compression, which is how the best compression ratio is achieved, so you won't be able to do as well as the 7z command line

That said, if zip really isn't an option, your last resort could be to call the proper command line directly within your program.

If pure 7z is not mandatory, another option would be to use a "tgz"-like format to emulate solid compression: first compress all files to a non-compressed file (e.g. tar format, or zip file with no compression), then compress that single file in zip mode with standard Java Deflate algorithm. Of course that will be viable only if that format is recognized by further processes using it.


Use 7-Zip file archiver instead, it compresses 832 KB file to 26.0 KB easily:

  1. Get its Jar and SDK.
  2. Choose LZMA Compression .java related files.
  3. Add Run arguments to project properties: e "D:\\2017ASP.pdf" "D:\\2017ASP.7z", e stands for encode, "input path" "output path".
  4. Run the project [LzmaAlone.java].

Results

Case1 (.pdf file ): From 33,969 KB to 24,645 KB.

Case2 (.docx file ): From 832 KB to 26.0 KB.