关于xz-java库是否支持及如何实现Extreme预设的技术咨询
I have several technical questions regarding the xz-java library, which I've also sent to the library author via email. The xz command-line tool supports «Extreme» presets, corresponding to the
-0e … -9eoptions. Below are excerpts from the xz manual covering standard and Extreme presets:
Standard Preset Summary
| Preset | Dictionary Size | Compression CPU Usage | Compression Memory Usage | Decompression Memory Usage |
|---|---|---|---|---|
| -0 | 256 KiB | 0 | 3 MiB | 1 MiB |
| -1 | 1 MiB | 1 | 9 MiB | 2 MiB |
| -2 | 2 MiB | 2 | 17 MiB | 3 MiB |
| -3 | 4 MiB | 3 | 32 MiB | 5 MiB |
| -4 | 4 MiB | 4 | 48 MiB | 5 MiB |
| -5 | 8 MiB | 5 | 94 MiB | 9 MiB |
| -6 | 8 MiB | 6 | 94 MiB | 9 MiB |
| -7 | 16 MiB | 6 | 186 MiB | 17 MiB |
| -8 | 32 MiB | 6 | 370 MiB | 33 MiB |
| -9 | 64 MiB | 6 | 674 MiB | 65 MiB |
-e, --extreme: Uses the slow variant of the selected compression preset level (-0 ... -9) to try to improve the compression ratio, though it may sometimes make it worse. This parameter does not affect decompression memory usage, but slightly increases compression memory usage for preset levels -0 ... -3. Since there are two presets with dictionary sizes of 4 MiB and 8 MiB, the -3e and -5e presets use slightly faster settings (lower CompCPU values) than -4e and -6e to ensure all presets are unique.
Extreme Preset Summary
| Preset | Dictionary Size | Compression CPU Usage | Compression Memory Usage | Decompression Memory Usage |
|---|---|---|---|---|
| -0e | 256 KiB | 8 | 4 MiB | 1 MiB |
| -1e | 1 MiB | 8 | 13 MiB | 2 MiB |
| -2e | 2 MiB | 8 | 25 MiB | 3 MiB |
| -3e | 4 MiB | 7 | 48 MiB | 5 MiB |
| -4e | 4 MiB | 8 | 48 MiB | 5 MiB |
| -5e | 8 MiB | 7 | 94 MiB | 9 MiB |
| -6e | 8 MiB | 8 | 94 MiB | 9 MiB |
| -7e | 16 MiB | 8 | 186 MiB | 17 MiB |
| -8e | 32 MiB | 8 | 370 MiB | 33 MiB |
| -9e | 64 MiB | 8 | 674 MiB | 65 MiB |
Technical Question Answers
1. Does xz-java support Extreme presets?
From reviewing the xz-java source code (specifically the org.tukaani.xz.LZMA2Options class), there is no built-in support for the Extreme (-e) presets offered by the xz CLI. The library's preset handling only covers the standard -0 through -9 levels, with no logic to implement the "extreme" variants that adjust CPU usage and (for lower presets) compression memory to optimize for compression ratio.
2. Can Extreme presets be implemented via client code if xz-java doesn't support them natively?
Absolutely! You can manually replicate the Extreme preset configurations by mapping the xz CLI's Extreme preset values to the corresponding settings in LZMA2Options. Here's a practical implementation based on the manual excerpts you shared:
import org.tukaani.xz.LZMA2Options; public class XzExtremePresetHelper { // Returns LZMA2Options configured for the given Extreme preset level (0-9) public static LZMA2Options getExtremePreset(int level) { if (level < 0 || level > 9) { throw new IllegalArgumentException("Preset level must be between 0 and 9"); } LZMA2Options options = new LZMA2Options(level); // Apply Extreme preset adjustments based on xz manual specs switch (level) { case 0: options.setCPUThreads(8); break; case 1: options.setCPUThreads(8); break; case 2: options.setCPUThreads(8); break; case 3: options.setCPUThreads(7); // -3e uses lower CompCPU than -4e break; case 4: options.setCPUThreads(8); break; case 5: options.setCPUThreads(7); // -5e uses lower CompCPU than -6e break; case 6: options.setCPUThreads(8); break; case 7: options.setCPUThreads(8); break; case 8: options.setCPUThreads(8); break; case 9: options.setCPUThreads(8); break; } // Dictionary sizes match standard presets, so no need to override them // Compression memory usage is derived from dict size + CPU threads, which aligns with manual values return options; } }
Key Notes:
- The
setCPUThreadsmethod inLZMA2Optionsdirectly maps to the "CompCPU" value from the xz manual - Dictionary sizes are identical between standard and Extreme presets of the same base level, so we don't need to modify them
- As noted in the xz manual, Extreme presets don't guarantee better compression ratios—test with your specific data to see if it's beneficial
- Since you've already contacted the library author, keep an eye out for their response—they may have plans to add native Extreme preset support in a future release
内容的提问来源于stack exchange,提问作者Sergey Vyacheslavovich Brunov

