It is possible to use the "tar" command to create and expand 7-Zip (7z
) files. On platforms like macOS that have tar, no separate 7-Zip program is needed.
With the "a" flag, we tell tar to compress an archive based on the specified file name. So if we use a ".7z" extension, 7-Zip is used.
To begin we need to compress an archive. Suppose we are in a folder with a directory named "work" that we want to compress with 7-Zip.
' Part 1: for default compression with tar and 7-zip: tar -acf archive.tar.7z work ' Part 2: for optimal compression: tar --options 7zip:compression-level=9 -acf archive.tar.7z work
On macOS, Windows and Linux it is usually possible to expand the archive by clicking on it. But we can use tar to do this directly, which is sometimes desired.
' To decompress 7z file with tar: mkdir temp tar -xf archive.tar.7z -C temp
When using tar, it is important to specify "c" or "x" to compress or expand. And "a" means tar can detect the compression format from the file name.
' Some tar flags: a - auto compress c - create new x - expand existing archive f - file to use (can read from archive or write to file)
The 7-Zip format is both efficient and widely-used. It can compress better than many other formats, and even programs like tar can handle it.