How to Handle Zip Files on Linux
- Simple Compression With ZIP in Linux
- Using the Multiple File Compression in Linux
- Adding Folder Directories in Linux
- Extracting a Zipfile in Linux
- Freshen or Update Files Within a ZIPfile in Linux
- Creating Spanned ZIPfiles in Linux
Zipped files, or zipfiles, are quite common to the layperson as a good way to combine and compress multiple files and directories into a single file.
The ZIP format has been around since 1989 and has been supported by modern operating systems since at least the 2000s.
If you write scripts to manipulate ZIP files or wish to use the command line more often, you may find this helpful.
This tutorial will introduce the average Linux user to ZIP file creation, updating, combining-and-splitting, and extraction, all on a Bash shell command line.
Simple Compression With ZIP in Linux
The original purpose of a zipfile was compression. In a nutshell, takes one or many files/directories.
It combines common patterns within each file and uses a special encoding to remove redundant information that allows the data in each file to be reconstructed losslessly.
You can read the full specification here.
This is a very good demonstration of how well this compression works and how to create a zipfile.
To create a file with similar characters, such as Lorem Ipsum
, containing identical information. Then use the zip
command to put them into a zipfile.
# we assume a file named lorem.txt exists
# that is at least 15 MB large - you can
# use any online site to make such a file!
user@linux:~$ ls -lh lorem.txt
-rw-r--r-- 1 user user 15M Jan 1 00:00 lorem.txt
user@linux:~$ zip lorem.zip lorem.txt
adding: lorem.txt (deflated 89%)
user@linux:~$ ls -lh lorem
-rw-r--r-- 1 user user 15M Jan 1 00:00 lorem.txt
-rw-r--r-- 1 user user 1.6M Jan 1 00:00 lorem.zip
The lorem.txt
file comprises 26 possible English letters, commas, full stops, spaces, and newlines. For a total of 30 unique characters, a large file containing just this combination of characters is very easy to compress.
ZIP’s encoding effectively stores the number of unique characters, keeping a tally of how many times they appear and where which is far. Smaller to store than the original file and allows for full reconstruction when the file is extracted from the zipfile.
Using the Multiple File Compression in Linux
The syntax is simple enough to compress multiple files with no directories.
# assume we have three files - lorem1, lorem2, lorem3.
user@linux:~$ zip lorem.zip lorem*
adding: lorem1.txt (deflated 48%)
adding: lorem2.txt (deflated 50%)
adding: lorem3.txt (deflated 49%)
user@linux:~$ ls -lh lorem*
-rw-r--r-- 1 user user 832 Jan 1 00:00 lorem1.txt
-rw-r--r-- 1 user user 862 Jan 1 00:00 lorem2.txt
-rw-r--r-- 1 user user 919 Jan 1 00:00 lorem3.txt
-rw-r--r-- 1 user user 1.8K Jan 1 00:00 lorem.zip
Adding Folder Directories in Linux
To add folders to a zipfile, specify the recursive -r
option and specify the folder names after the zip file path, as follows.
# assuming a folder named loremfiles exists
user@linux:~$ zip -r lorem.zip loremfiles/
Extracting a Zipfile in Linux
The zip
utility does not include extraction functionality. Instead, this will handle by unzip
. The syntax for this is quite simple.
unzip lorem.zip
Freshen or Update Files Within a ZIPfile in Linux
Make a change to a file that you’d like to update accordingly in a zipfile that contains it. You can use the freshen -f
flag to make zip
look for the files within the zipfile where they were original.
Check if they have changed since the zipfile was modified, and add newer versions of existing files.
For example, in the previous example, if we modify lorem1.txt
and run the following, we can “freshen” our lorem
zipfile with changes.
user@linux:~$ zip -f lorem.zip
freshening: lorem1.txt (deflated 48%)
We can use the update -u
option for more functionality.
In addition to freshening the zipfile with newer files, it can add new files to an existing zipfile and create the zipfile if it doesn’t exist.
# we change lorem3.txt, and create lorem4 and lorem5
user@linux:~$ zip -u lorem.zip lorem*
updating: lorem3.txt (deflated 49%)
adding: lorem4.txt (deflated 49%)
adding: lorem5.txt (deflated 49%)
Creating Spanned ZIPfiles in Linux
We may avoid sending one huge file across the Internet to save bandwidth. Instead, send multiple files so that at least parts of the file can get across if others fail.
We can try resending them. ZIP provides an option to split a very large zipfile into multiple parts, called a spanned archive.
To try an example, find either a large file or create one with random data, as shown below.
dd if=/dev/urandom of=frnd count=16384 bs=1024
This produces a 16 MB file named frnd
in the local directory. Then we split this into a 2-part zipfile, each part 8 MB, using the zip
command as shown.
zip -r -s 8m rnd.zip frnd
This produces two 8 MB files named rnd.z01
and rnd.z02
and an end-of-file zip called rnd.zip
. The last one is necessary to indicate to unzip
that the end-of-file signature has been reached.
The multiple files make up a valid zip archive to unzip, concatenate the files into a single zip, and unzip the result.
cat rnd.z* > final.zip
unzip final.zip