You have probably noticed .zip, but don’t know much about the .tar file. If so, then you are in the right place to learn about how you use tar and zip. We will look at the differences, how to compress and uncompress and see when to use which.
Zip is common for a Windows user, but on Linux OS, you’ll find .tar format more common. The .tar and .tar.gz format are quite common on Unix and Linux based systems. In this post, we’ll talk about .zip and .tar format and their differences.
Tar vs. Zip
The zip format is common on Windows OS, while the tar format is common on Linux. Both of these formats archive many files into one. The zip format creates an archive and additionally also compresses the files.
The size of the zip file will be less than the original files. The tar format creates a single archive of multiple files without compression. To put in simple words, the zip format is a collection of compressed files. The tar format is a collection of uncompressed files.
The tar format is rigid. It needs to be extracted if you need to read or view a single file. The tar format is not randomly accessible. On the other hand, you can view any single file from the zip format without decompressing it. This means that the zip format is randomly accessible.
The rigid structure of tar format means you can’t add a new file or delete a file without decompressing it. The tar format treats the archive as one single file. So, if you want to add a new file, you’ll need to extract it. Add the new file and then create a tar archive again.
Zip format is common on Windows because it stores MSDOS attributes. These attributes include Hidden, System, Readonly, and Archive. On the other hand, the tar format is common on Linux because it stores Unix file attributes. These include gid, uid, and permissions, such as “executable” on scripts.
When to use tar and zip?
Now let’s talk about which format is best suited to you. It totally depends on your needs. The tar format alone is just an archiver. It will just merge your files into one. On the other hand, the zip format is an archiver, as well as a compressor. Choose tar if you need to archive files. Choose zip you need to archive and compress files.
You can choose tar format if you are working on a Linux system. Tar contains executable script permissions, which the zip format lacks. If you’re a Windows user, you can go for the zip format.
If you have a very large archive, then use zip. You can access any file you want. Don’t use tar format for a large archive. You’ll need to extract the entire archive to view on file.
How to create a tar file
tar -zcvf sample.tar /path_to_directory
Use this command to archive a whole directory. Replace the sample with the name of your new archive file. Also, replace /path_to_directory with the path to the directory you want to archive. You can archive a file by replacing /path_to_directory with the path to the file.
How to create a zip file
zip -r test.zip /path_to_directory
Execute the command above to create a zip file of an entire directory. Replace the test with the name of your new archive file. Replace /path_to_directory with the path to the directory you want to archive. You can archive a file by replacing /path_to_directory with the path to the file.
How to extract a tar file
tar -zxvf file.tar
Run the command to extract your file.
How to extract a zip file
Run the command to extract your zip file.
There you go! You have now learned how to use tar and zip. Also, you have seen why and when to use the tar and zip format. Lastly, you may want to read How to enable ZipArchive for PHP if you are doing PHP development.
Watch the following video to see how tar is used for cronjobs:
About the Authors
Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic!
Support the Cause
Support Anto Online and buy us a coffee. Anything is possible with coffee and code.