How to XZ a directory with TAR using maximum compression?

  • So I need to compress a directory with max compression.

    How can I do it with xz? I mean I will need tar too because I can't compress a directory with only xz. Is there a oneliner to produce e.g. foo.tar.xz?

    FWIW, `man 1 xz` says `it's not a good idea to blindly use -9 for everything like it often is with gzip(1) and bzip2(1).` `-7 ... -9 [...] These are useful only when compressing files bigger than 8 MiB, 16 MiB, and 32 MiB, respectively.` RTFM for more info.

  • Shadur

    Shadur Correct answer

    9 years ago

    Assuming xz honors the standard set of commandline flags - including compression level flags, you could try:

    tar -cf - foo/ | xz -9 -c - > foo.tar.xz 
    

    and this uses maximum compression level with XZ?

    adding -9 to xz will make it max

    `-9e` is the best level, but it will take very long

    `-9e` will not always give you the best result - see point 8 here https://www.rootusers.com/13-simple-xz-examples/

    Also, you might see significant improvement if you add `--threads=0` to xz

    @KolonUK reading that article, it shows that `-e` (extreme mode) always improves compression ratio; the comparison is between `-0e` and `-6`; while `-e` always improves compression ratio within the same compression level, a higher compression level may be more effective than "extreme mode". There is no evidence that `-9e` can yield a worse compression ratio than `-9`.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM