Creating and Extracting ZIP Archives With Zip and Unzip
In this article you will learn how to create ZIP archives, list, test and extract them. After reading this article you will know how to manipulate ZIP archives on GNU/Linux systems. We already know that zip works well with tar from the Using TAR Command post. Let us discuss how to work with zip and unzip.
How to create a ZIP archive?
I believe learning by example is the easiest way. Let me walk you through an example straightaway. Let us assume we have two files - abc.txt and xyz.txt in the current working directory.
[sudheer@localhost zip]$ ls
abc.txt xyz.txtTo compress the text files and to archive them use the below command:
zip foo *txtThe syntax we followed is:
zip zipfile listWhere
- zip is the command
- zipfile is the name of the ZIP archive to be created
- list is the list of files to be compressed and included in the archive.
In our example foo is the name of the ZIP archive file to be created. *txt is the list of files to be compressed and included in the ZIP archive.
The result of the above command is the creation of the file named foo.zip which contains the archive of the compressed files in the current working directory whose names end with txt.
We could also use
zip foo abc.txt xyz.txtto achieve the same result.
The output of the above command should look something like:
[sudheer@localhost zip]$ zip foo abc.txt xyz.txt
adding: abc.txt (stored 0%)
adding: xyz.txt (stored 0%)
[sudheer@localhost zip]$ ls
abc.txt foo.zip xyz.txtHow to extract a zip archive?
To extract a zip archive you use unzip. Zip and unzip are companion programs to each other. To extract a ZIP archive you just need to type unzip followed by the name of the archive. Here is an example. Let us extract the ZIP archive - foo.zip, we created in our previous example.
unzip foo.zipThis will extract the contents of foo.zip to the current working directory.
Unzip can also list and test ZIP archives. To list the contents of a ZIP archive use the -l switch.
unzip -l foo.zip The output of the above command looks like:
Archive: foo.zip
Length Date Time Name
-------- ---- ---- ----
13 03-23-08 21:58 abc.txt
13 03-23-08 21:44 xyz.txt
-------- -------
26 2 filesUnzip -l prints the names, uncompressed file sizes and modification dates and times of the specified files along with totals. File size in this case is the length of characters.
You can test whether a ZIP archive is damaged using the -t option.
unzip -t foo.zipUnzip extracts the contents of foo.zip in memory. Unzip uses CRC to verify the archive. If it detects any problems with the archive it promptly prints them on the screen.
[sudheer@localhost arc]$ unzip -t foo.zip
Archive: foo.zip
testing: abc.txt OK
testing: xyz.txt OK
No errors detected in compressed data of foo.zip.Isn't it very convenient to test a ZIP archive before extracting it, especially if the archive size is too large?
You can of course read more about zip and unzip in the manual pages and other documentation sources. This primer introduced you to the most common operations performed using zip and unzip.









Post new comment