Using tar with Your Favorite Compression

Here's a fun one! You may already know that tarball is a pure archive format, and that any compression is applied to the whole archive as a unit. That is to say that compression is not actually applied at the file level, but to the entire archive.

This is a trade-off the designers made to limit complexity, and as a side-effect, is the reason why you can't randomly access parts of a compressed tarball.

What you may not know is that the tar utility has built-in support for a few formats! GZIP is probably the most commonly used for historical reasons, but zstd and lz4 are built-in options on my Mac. This is probably system-dependent, so check your local manpages.

Here's an example of compressing and decompressing with zstd:

tar --zstd -cf directory.tar.zst directory/
tar --zstd -xf directory.tar.zst

You can also use this with any (de)compression program that operates on stdin and stdout!

tar --use-compress-program zstd -cf directory.tar.zst directory/

Pretty cool, huh? It's no different that using pipes at the end of the day, but it does simplify the invocation a bit in my opinion.

After I initially published this article, @cartocalypse@norden.social noted that some versions of tar include the -a/--auto-compress option which will automatically determine format and compression based on the suffix! Check your manpages for details; it appears to work on FreeBSD, macOS (which inherits the FreeBSD implementation), and GNU tar.

Posts from blogs I follow

Why fsyncing a File Isn't Safe Enough

Recently I learnt something interesting about writing files to disk. Something that connects my first ever low-level database PR with the reason you can’t pull a USB disk out of your laptop without ejecting it first. As you may know, when y...

via Kris Jenkins' Blog

Four levels of in-place initialization

IntroductionThe goal of in-place initialization is to enable the construction of types directly into a memory location without any additional moves or copies. When working with big types this can be more efficient and even prevent stack ove...

via Yosh Wuyts — Blog

Generated by openring-rs from my blogroll.