Working With Compressed Files on a Raspberry Pi

Almost all downloadable files on the internet are compressed. The reduced file size makes data transfer quicker and also cheaper. That is why in this tutorial, we’re going to learn how to compress and uncompress files on a Raspberry Pi.

File Compression

File compression is putting one or more files in an archive so that their cumulative size is reduced. Having a smaller file is advantageous when dealing with the internet. Firstly, they won’t take too much space. Hosting a website, especially for DIY projects using an Arduino or a Raspberry Pi, is often limited in memory space. With a smaller file size, data storage is relieved. Also, for rated internet services, a reduced file size means saving money.

By using a specialized program, you can compress and decompress files without any degradation. It’s especially easy on a Raspberry Pi.

Zip Files

One of the earliest compressed file formats is zip. It’s so popular that even today, saying zip or zipped files is equivalent to all compressed file types. There are more than a dozen file formats, of course. There’s zip, tar, rar, 7z, arc, arj, cab, and many more, but they all work the same way. So to make things terser, we’ll focus more on the two most popular ones: the zip and tar file formats.

Extracting Zip Files

Extracting a file means getting the original files out of a compressed file format. To uncompress a zip file, simply write the command below.

unzip filename.zip

The zip and unzip commands are default to the Raspberry Pi OS, so no need to install them explicitly. The command is also straightforward. Just enter unzip then the file name of the archive file. The compressed files inside will go to your current directory in no particular order.

Compressing Zip Files

On the other hand, if you want to compress one or multiple files into a zip archive, use the command syntax below.

zip filename.zip file1 file2 file3

Take note that files 1 to 3 must be located in your current directory. Additionally, if you want to compress a whole directory to a zip file, enter the following command.

zip -r filename.zip /targetdirectory

The -r switch makes sure that all the files in your target directory are compressed properly.

Tar Files

Meanwhile, the default compressed file of the Raspberry Pi is called a tar file. They are the output of a GNU Linux command called tar. Tar in itself bundles files together without compressing. These bundled files are called a “tarball.” Tar is often paired with gzip to compress files into a .tar.gz file or .tar.bz2 file.

Tar can be accessed directly using the terminal. This is why it’s the preferred compressed file type on a Linux environment.

Extracting Tar Files

To get the original files from a tar archive, enter the command below.

tar -xzvf filename.tar.gz

The tar utility has a similar syntax with Linux-based commands. It uses a couple of switches that may seem daunting at first but are actually very convenient. Here’s what the switches on the sample command mean:

-r Extract an archive.
-z Use gzip.
-v Enter verbose mode. Verbose means it will display all that’s happening to your files.
-f Lets you specify the filename of tar file.

Compressing Tar Files

Similarly, to compress using tar, we use the command below.

tar -cvjf filename.tar.gz /targetdirectory

The switches are almost the same as the ones we used when extracting except -c and -j, which means creating an archive and using bzip2, respectively.

-c Create an archive.
-j Use bzip2.

Source