I'm working in Linux environment and want to know about tar and zip commands.
Which is more efficient - tar or zip? I also need to know the differences between the tar and zip commands. Can anyone explain them to me?
Stack Exchange network consists of 181 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.Visit Stack Exchange
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
tar only makes a single file out of multiple files, it doesn't do compression unless combined a compression program such as
bzip2 (which you can call from within
tar by using the
-j options, respectively).
zip combines both the archiving and compression in one program.
The fact that zip compresses the files separately will impact compression ratios, particularly on many small similar files.
(At least this was exactly correct a decade ago.)
unziputilities always store and restore Unix file permissions,
unziprestores the Unix file timestamps unless you provide the
unzipeven restores the UID and GID if you provide the
Tar preserves much more metadata than Zip, see my comparison (it's slightly outdated):
(Click to zoom in)
Tar passes 65% of the tests, where Zip only passes 17%. I have made the test suite available on github under BSD license so you can try for yourself if you have Mac. For linux there I'm not sure if there are any metadata, so these tests may not be relevant.
unziputilities always store and restore contents and file permissions, by default store and restore file timestamps, and can store and restore file ownership (UID and GID). This is often all you need.
Efficiency can be measured in different ways:
There are other questions, too, like "How common are the tools to manipulate the resulting archives?"
So, for example,
bzip2 creates smaller files than
gzip, but it can take significantly longer. Also, in my experience
gzip is universal on Unix-like systems, but
bzip2 is still not (though it's very common and usually easy to get).
As Wim noted, tar itself doesn't compress. If you do add compress the tar (e.g. to get a .tar.gz or .tar.bz2), you're compressing the whole tar file at once. In contrast, zip compresses each file individually.
The efficiency depends on the workload. Specifically, zip allows you to access individual files directly. With tar, you have to first seek through the unwanted (compressed) files before. The compression performance depends on what you're compressing.
bzip2 is often better for a large number of similar files (e.g. a source directory).
zip could be better if each file has very different content.
Zip archives contain a central directory of their contents at the end (most likely to avoid having to create the directory beforehand, where you don't yet know what will be inside). This allows to quickly extract single files without having to unpack the whole archive: Just read the archive directory and extract only what is needed. However, this requires that the whole archive is accessible, and requires random access which is only available on block devices (floppy disks, hard drives). In addition, the archive directory is vulnerable: If the archive gets truncated for some reason, it requires heavy wizardry to extract anything useful from the archive.
Zip archives were created for BBS use, where it was important to be able to bundle the contents of a directory into one single (and compressed) file---instead of having to download possibly thousands of single files. Much like most web sites bundle their downloads even today, for the same reasons.
Tar archives were devised for bundling backups to be used for tape drives, hence for sequential access. There is no central directory; instead, the archive contains header blocks at regular intervals which indicate which files will follow in the next few blocks. Tar archives are intended to be read in one fell swoop; if only a single file is to be extracted, the archive is read sequentially, starting from the very beginning until the requested file is found (which may as well be at the very end). Compression is applied on top of that; each of the various compression programs that are applied to tar archives (
bzip2 etc.) are stream compressors and don't alter the sequential nature of the archive in any matter. In the worst case, you'd need slightly more blocks until you can start extracting.
This may sound like a trivial difference, but in fact represents a polar opposite in philosophy. With zip archives, there is always the need to have the entire file at hand to do anything useful with it, whereas a tar archive can be streamed to a pipeline. I can download a large tar archive and start extracting it right from the start, as soon as the first few blocks come in (and maybe interrupt the download as soon as I get the file I am looking for). For a Zip archive, I have to wait until the archive directory appears, which comes at the very end of the archive. But once I do have the entire file at hand, extracting partial contents from it will be much quicker from a tar file.
Both formats have one very strong point going for them, depending on where and how they are used. Since pipelines (and thus the notion of streaming data from one process to another) only really exist in the Unix world, the main advantare of tar archives is lost on other systems, which is why Zip archives are much more popular there. But tar archives are more flexible, which is why I prefer them whenever I have a choice.
As the other already said, tar creates a large "block" of all the files that can be compressed with a stream comrpessor like gzip or bzip2.
The disadvantage of this is that you have to decompress the whole file to access one file inside the archive.
The advantage of this is that the compress ratio is usually higher, especially when the compressed files are very similar.
Other packer like "rar" have a "block mode" (or similar) to have the same effect.