266

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

Richie Thomas
  • 3,073
  • 4
  • 32
  • 55
mtk
  • 13,221
  • 16
  • 72
  • 112
  • 1
    See these two answers from [SuperUser](https://superuser.com/): - [Which is more efficient - tar or zip compression? What is the difference between tar and zip?](https://superuser.com/questions/173756/which-is-more-efficient-tar-or-zip-compression-what-is-the-difference-between) - [On Linux/Unix, does .tar.gz versus .zip matter?](https://superuser.com/questions/146754/on-linux-unix-does-tar-gz-versus-zip-matter) (By the way, StackOverflow is for programming problems. You'd probably do better asking questions like this at SuperUser in the future) – SomeKittens May 10 '12 at 19:35

1 Answers1

467

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).
Attila
  • 28,265
  • 3
  • 46
  • 55
  • 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? – Dillon Ryan Redding Jun 26 '20 at 17:42
  • 4
    @DillonRyanRedding Edited. Does this resolve your confusion? – Attila Jun 28 '20 at 22:07
  • What do you mean with `tar.gz`you 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. – mariano-daniel Nov 16 '22 at 02: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 '22 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! – mariano-daniel Nov 23 '22 at 03:01