Show pageOld revisionsBacklinksBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== Linux : Compression and Archiving ====== ===== What is Compression? ===== **Compression** is a process or technique to reduce the size of a data file. This is achieved using specific algorithms that identify patterns in the data to reduce its size. Compression is divided into two types: * **Lossless Compression**: Data is compressed without any loss of information. The decompressed data is identical to the original. * **Lossy Compression**: Some information is intentionally removed to reduce file size. The decompressed result may not be identical to the original. ===== What is Archiving? ===== Before exploring compression, it is important to understand **archiving**. **Archiving** is the process of collecting multiple files or directories into a single file. This archive can then be compressed using compression tools. ==== tar ==== **tar** is a common archiving tool on Linux. Example usage: <code bash> tar -cvf archive.tar file1 file2 directory </code> In the example above: * <code>-c</code>: Create a new archive * <code>-v</code>: Verbose mode (shows detailed process) * <code>-f</code>: Specifies the archive file name <WRAP info> You can add compression options: <code>-z</code> for gzip, <code>-j</code> for bzip2, or <code>-J</code> for xz. </WRAP> ===== Compression Tools ===== Common compression tools on Linux: ^ Compression Tool ^ Compression Algorithm ^ | **gzip** | DEFLATE | | **bzip2** | Burrows-Wheeler | | **xz** | LZMA | | **zip** | DEFLATE | ==== gzip ==== **gzip** is a widely used compression utility using the **DEFLATE** algorithm. * Compress a file: <code bash> gzip filename </code> → Produces <code>filename.gz</code> * Decompress a file: <code bash> gunzip filename.gz # or gzip -d filename.gz </code> * View compression info: <code bash> gzip -l filename.gz </code> ==== bzip2 ==== **bzip2** uses the **Burrows-Wheeler** algorithm for better compression than gzip. * Compress a file: <code bash> bzip2 filename </code> → Produces <code>filename.bz2</code> * Decompress a file: <code bash> bunzip2 filename.bz2 # or bzip2 -d filename.bz2 </code> * View compression info: <code bash> bzcat filename.bz2 | wc -c </code> ==== xz ==== **xz** uses the **LZMA (Lempel-Ziv-Markov chain algorithm)**. It offers higher compression ratios but is slower and more resource-intensive. * Compress a file: <code bash> xz filename </code> → Produces <code>filename.xz</code> * Decompress a file: <code bash> unxz filename.xz # or xz -d filename.xz </code> * View compression info: <code bash> xz -l filename.xz </code> ==== zip ==== **zip** is commonly used to compress and archive multiple files. * Compress files: <code bash> zip archive.zip file1 file2 folder1 </code> * Recursively compress a folder: <code bash> zip -r archive.zip folder1 </code> * Add files to existing zip: <code bash> zip -u archive.zip file3 </code> * Password-protect a zip: <code bash> zip -r -e archive.zip folder1 </code> → Produces <code>archive.zip</code> * Decompress: <code bash> unzip archive.zip </code> * View compression info: <code bash> unzip -l archive.zip </code> ===== Archiving and Compression with tar ===== **tar** supports built-in compression in one command: ==== tar + gzip ==== <code bash> # Compress tar -czvf archive.tar.gz directory/ # Decompress tar -xzvf archive.tar.gz </code> ==== tar + bzip2 ==== <code bash> # Compress tar -cjvf archive.tar.bz2 directory/ # Decompress tar -xjvf archive.tar.bz2 </code> ==== tar + xz ==== <code bash> # Compress tar -cJvf archive.tar.xz directory/ # Decompress tar -xJvf archive.tar.xz </code> <WRAP info> Explanation of flags: * <code>-c</code>: Create archive * <code>-x</code>: Extract archive * <code>-z</code>: Use gzip * <code>-j</code>: Use bzip2 * <code>-J</code>: Use xz * <code>-v</code>: Verbose (detailed output) * <code>-f</code>: Specify archive file name </WRAP> system/linux/file-compression-archiving.txt Last modified: 2026/02/01 21:08by 127.0.0.1