259

What is the difference between tar and zip? What are the use cases for each?

1

1 Answer 1

451

tar in itself just bundles files together (the result is called a tarball), while zip applies compression as well.

Usually you use gzip along with tar to compress the resulting tarball, thus achieving similar results as with zip.

For reasonably large archives there are important differences though. A zip archive is a collection of compressed files. A gzipped tar is a compressed collection (of uncompressed files). Thus a zip archive is a randomly accessible list of concatenated compressed items, and a .tar.gz is an archive that must be fully expanded before the catalog is accessible.

  • The caveat of a zip is that you don't get compression across files (because each file is compressed independent of the others in the archive, the compression cannot take advantage of similarities among the contents of different files); the advantage is that you can access any of the files contained within by looking at only a specific (target file dependent) section of the archive (as the "catalog" of the collection is separate from the collection itself).
  • The caveat of a .tar.gz is that you must decompress the whole archive to access files contained therein (as the files are within the tarball); the advantage is that the compression can take advantage of similarities among the files (as it compresses the whole tarball).
5
  • 3
    I'm a little confused. The last paragraph and the list seem to contradict each other. A zip compresses files into a catalog, but the caveat is that you don't get compression across files? Similarly for .tar.gz. Is there a typo in there? Jun 26, 2020 at 17:42
  • 4
    @DillonRyanRedding Edited. Does this resolve your confusion?
    – Attila
    Jun 28, 2020 at 22:07
  • What do you mean with tar.gzyou need to decompress the whole file to access files? I easily do tar -f myfile.tr.gz -xzv file-that-i-want-to-extract.pdf and it works. Nov 16, 2022 at 2:49
  • 2
    @mariano-daniel A .tar.gz file is a gzip-ed tar file, which means tar will have to unzip the .tar.gz file (possibly in memory) to get to the contained (uncompressed) .tar file, which contains the file you want to ultimately extract. To see, try: gunzip myfile.tar.gz; you will get the contained (uncompressed) myfile.tar file (NOTE: gunzip will delete the original myfile.tar.gz file, so you might want to make a backup copy first)
    – Attila
    Nov 21, 2022 at 17:54
  • @Attila thanks for clarifying! So tar will have to unzip the file, not you ;-) But seriously, now I understand what you meant, I took it too literally and thought you meant I needed to unzip it, then extract from the tar. Have a nice day! Nov 23, 2022 at 3:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.