Wednesday, January 24, 2018

File Compression and Decompression with Zip and Gzip


File Compression and Decompression with Zip and Gzip

Compressed files use less disk space and download faster than large, uncompressed files. You can compress Linux files with the open-source compression tool Gzip or with Zip, which is recognized by most operating systems.
By convention, compressed files are given the extension .gz. The command Gzip creates a compressed file ending with .gz; Gunzip extracts the compressed files and removes the .gz file.

To compress a file, type the following command:

[root@host~]# vim file1
[root@host~]# ls -la

[root@host~]# gzip file1
[root@host~]# ls -la

Note: The command Gzip creates a compressed file ending with .gz

To decompress a file,  type the following command:

[root@host~]# gunzip file1.gz
[root@host~]# ls -la

Note: The command Gunzip remove .gz   extension  and  replace file1.gz to file1

=================================================================

If you exchange files with non-Linux users, you may want to use zip to avoid compatibility problems. Red Hat Linux can easily open zip or gzip files, but non-Linux operating systems may have problems with gzip files.


To compress a file with zip, type the following: 

[root@host~]# zip -r file1.zip file1
[root@host~]# ls -la

Note: In this example file1.zip represents the file you are creating and file1 represents the file you want to put in this file.

To extract the contents of a zip file, type the following command

[root@host~]# unzip file1.zip 
[root@host~]# ls -la

=======================================================


tar file

The utility tar (for Tape ARchive) is a good way to package up multiple files into a sinfle package. tar is available for all platforms and is installed by default on most Linux/Unix systems and Mac.

Suppose you have file1, file2, and file3

[root@host~]# vim file1
[root@host~]# vim file2
[root@host~]# vim file3

Now we create a tar file named myproject.tar including file1, file2, and file3

[root@host~]# tar -cvf myproject.tar file1 file2 file3


To list the contents of a tar file, type:


[root@host~]# tar -tvf myproject.tar

To extract the contents of a tar file, type: 

[root@host~]# tar -xvf myproject.tar

Now I shall want to use gzip to compress the tar file for fastest upload:

[root@host~]# gzip myproject.tar

[root@host~]# ls -la

Note: This command will create a file named myproject.tar.gz

Other option is:

[root@host~]# tar -cvzf myproject.tar

Compressed tar files are conventionally given the extension .tgz and are compressed with gzip. 

To expand a compressed tar file type:

 [root@host~]# tar -xvzf myproject.tgz


Alternately most versions of tar allow you to create a gzipped tar file in one action. Using the above example to create a gzipped tar file called myproject.tar.gz

[root@host~]# tar -cvzf myproject.tar.gz file1 file2 file3
[root@host~]# ls -la












No comments:

Post a Comment