As files get larger and larger we need a way of making them as small as possible. Whether it is transferring files over the internet or saving disk space compression is a necessary tool for reducing the amount of space it takes to store the file or folder. For example, when you have a server that is operating 24/7 log files will grow and you still need a way to keep the log files but you need them to take up less space. This is where file compression comes to the rescue.
Below we are going to look at one such archive file format called a zip archive and how to create, extract, and everything else around the zip command line utility on Linux.
Understanding The Basics:
So what is compression, compression is a process of reducing the size of files or data to optimize storage space and facilitate faster transmission. It works by encoding information more efficiently, removing redundant or unnecessary bits without compromising the essential content. In the context of file compression, a compressed file, often referred to as an archive, is created, containing the original data in a more compact form.
Keep in mind that files do not all compress the same way. The file type of file will affect how much a file can be compressed for example a JPEG will compress differently than large text files.
A zip archive is one such file archive that combines both file compression and archive files. This distinction is being made as these are technically separate things. That said some file types combine these rather than doing one than the other. This is more prominent in the Linux world because there is so much choice.
Creating a Zip Archive
To create a zip archive we first need to know what we want to add to the newly created zip archive.
Once that is decided, the next step is to determine what the command needs to be to include the desired files and directories. Below are some basic examples of creating zip archives in Linux.
To Specified Files File To The Archive File:
zip archive.zip file
To Add Specific Files To The Archive
zip archive.zip file1.txt file2.txt
Zip All Files In Current Directory
zip archive.zip *
Zips Up An Entire Directory Recursively
zip -r archive.zip foo/
Extracting Zip Archive
Now that you have data in a zip archive archive let’s go over how to extract and get the data back out. To do that we will be using a second command conveniently named the unzip command.
To View The Contents Without Extracting
unzip -l archive.zip
Note that this may not be suitable for zip archives with many files, as all names will be output to your shell.
To View The Contents Of A Specified Directory In The Zip File
unzip -l archive.zip 'specific_directory/*'
Extract Zip File to Current Directory
unzip archive.zip
Extract to a Specific Directory
unzip archive.zip -d destination_folder/
Extract Selected Files:
unzip file.zip file1 file2
Key Options And Flags
Here are some examples of the different options or flags for both the zip command and its counterpart the unzip command in Linux.
Zip Command:
-0 to -9: Set compression level (0 for no compression, 9 for maximum compression).
-e: Encrypt the contents of the zip file.
-P: password: Set a password for the zip file.
-x: file_pattern: Exclude files that match the specified pattern
-u: Update the zip file with newer files.
-T: Preserve file timestamps.
-z: Include a comment in the zip file.
-r: Recursively include subdirectories, but exclude system and hidden files.
-s split_size: Create split zip files with the specified size.
Unzip Command:
-o: Overwrite files without prompting.
-q: Quiet mode (suppress all messages).
-j: Junk paths. Do not create directories for extracted files; extract in the current directory.
-u: Only extract files that are newer than the existing ones.
-P: Prompt for a password to extract encrypted files.
-t: Uses to verify file integrity of the zip file.
Advanced Use Cases
Using the flags listed above let us look at a few advanced use cases.
Zip
Create a Zip File with Maximum Compression:
zip -9 archive.zip file1 file2 directory
This command creates a zip file named archive.zip with a maximum compression level rather than the default compression level.
Create an Encrypted Zip File with Password:
zip -e -P secret archive.zip sensitive_file
This command creates an encrypted zip file named archive.zip with the password “secret” for the file sensitive_file. If you use this command make sure you replace “secret” with a secure password.
Update an Existing Zip File:
zip -u existing.zip new_file.txt
This command updates the existing zip file existing.zip by adding the new file new_file.txt to it.
Excluding Files With Specific File Extension
zip archive.zip source_folder/ -x "*.log"
Include a Comment in the Zip File:
zip -z "This is a comment" archive.zip file1 file2
This command adds a comment “This is a comment” to the zip file archive.zip while zipping file1 and file2.
Unzip
Overwrite Existing Files Without Prompting:
unzip -o backup.zip
This command extracts the contents of backup.zip, overwriting existing files without prompting.
Extract with Full Path Ignoring Directory Structure:
unzip -j archive.zip
This command extracts the contents of archive.zip into the current directory, ignoring the directory structure in the zip file.
Test the Integrity of the Zip File:
unzip -t archive.zip
This command tests the integrity of the zip file archive.zip without extracting its contents.
Decrypt Encrypted Files During Extraction:
unzip -d destination -P secret encrypted.zip
This command extracts the contents of the encrypted zip file encrypted.zip to the specified destination directory. Make sure to replace “secret” with the actual password of the password-protected zip file.
Comparisons With Other Compression Tools
So you may be wondering why I would use zip over another compression and archive combination. One of the biggest reasons is going to be cross-comparability with other operating systems.
If you’re going to only work with the files on Linux then there isn’t necessary to stick with a zip archive. So let us compare the more common compression and archive tools.
tar: The tar command in Linux creates archives but does not natively have libraries for compressing files though it does have flags that let you use gzip and bz2 compression.
- Built-in Compression Algorithm: None (compression flags use external libraries)
- File Extension: Archive files typically have a .tar extension.
gzip: The gzip utility only does file compression but does not allow creation of archive files
- Built-in Compression Algorithm: It uses the DEFLATE compression algorithm.
- File Extension: Compressed files typically have a .gz extension.
bzip2: The bzip2 utility only does file compression but does not allow creation of archive files
- Built-in Compression Algorithm: It uses the Burrows-Wheeler algorithms.
- File Extension: Compressed files typically have a .bz2 extension.
7z: The 7z command is used for creating and extracting 7z archives, which can contain multiple files and support various compression algorithms.
- Built-in Compression Algorithms: It supports LZMA, LZMA2, PPMD, BZip2, and other compression algorithms.
- File Extension: Compressed files typically have a .7z extension.
rar: The rar utility is used for creating and extracting RAR archives. It supports file splitting, password protection, and recovery records.
- Built-in Compression Algorithm: It uses the RAR compression algorithm.
- File Extension: Compressed files typically have a .rar extension.
Conclusion
In conclusion, the Linux Zip command provides a versatile and efficient solution for file compression and archiving, crucial for tasks such as data transmission and optimizing storage space. This comprehensive guide has covered the basics of compression, creating and extracting Zip archives, key options and flags, advanced use cases, and comparisons with other compression tools. Whether you’re compressing log files on a 24/7 server or managing data on Linux, the Zip command stands out for its cross-compatibility with other operating systems.
Embracing a lifelong passion for technology since childhood, CJ delved into the intricate workings of systems, captivated by the desire to understand the unknown. This innate curiosity led to his discovery of Linux, a revelation that resonated deeply. With more than 7 years of on the job experience, he’s honed his technical skills as a Geek and Senior Linux Systems Administrator.
Leave a Reply