Linux压缩和解压缩(tar, zip, bz2, 7z, xz, rar, etc)

目录

1. 前言

2. tarball压缩与解压

3. gz文件压缩与解压缩

4. zip文件[解]压缩

5. bz2文件[解]压缩

5. 7z文件[解]压缩 

6. Xz文件[解]压缩 

7. Rar文件压缩与解压缩

8. 性能比较


扫描二维码关注公众号,回复: 14969914 查看本文章

1. 前言

        汇总简介linux系统中常见各种文件压缩格式,包括tarball, zip, bz2, 7z, xz, rar等等的压缩和解压缩。本文中只介绍所涉及到的工具或命令常见使用方法,更详细更高端的使用方法可以用"cmd --help"的方式进行查看。

2. tarball压缩与解压


The tar command is the most used command to archive multiple files into a tarball — a common Linux file format that is similar to zip format, with compression being optional. 用tar将文件打包,可以压缩也可以不压缩。当然通常来说会以压缩的形式进行打包。

This command is quite complex with a long list of functions such as adding new files into an existing archive, listing the content of an archive, extracting the content from an archive, and many more. Check out some practical examples to know more about other functions. 或者使用“tar --help”查看手册。

tarball压缩的常用命令格式:

tar -czvf my-new-archive.tar.gz file1 file2 ... # 将所列文件打包压缩
tar -czvf my-new-archive.tar.gz * # 将当前目录下所有文件打包压缩

tar -czvf my-new-archive.tgz /path/to/folder # 将指定目录下所有文件打包压缩

其中:

“c” 表示create,即创建压缩包;

“z”表示指定用gzip进行压缩,未指定的话就只是单纯的打包;

“v”表示verbose,即解压过程中会打印出详细的解压信息,不指定的话工具会默默地干活了事

“f”表示指定待处理文件

注意压缩文件后缀并非必需。但是通常带个后缀可以让人知道这是一个什么文件,常用的就是.tar.gz或者更加简洁一点的tgz。

文件解压的常用命令格式:

与“c”相对,“x”表示extract,即提取,解压缩。

tar -xzvf file.tar.gz  #    解压并生成在当前目录下

3. gz文件压缩与解压缩

当然,如果是单个文件的压缩处理的话,就直接用gzip也可以。

gzip bigfile

gunzip bigfile.gz

gzip执行的是in-place压缩,即压缩完后,原始文件被所生成的bigfile.gz所替代。

4. zip文件[解]压缩


Use the zip command to compress your files into a zip archive, and use the unzip command to extract the zipped files from a zip archive.

        解压缩*.zip文件:

unzip filename.zip   

        压缩*.zip文件:

zip name-of-zip-archive filename1 filename2 filename3

zip name-of-zip-archive *

zip -r name-of-zip-archive /Path/To/Folde

5. bz2文件[解]压缩

        file.bz2文件创建方法:

bzip2 bigfile

        As with the gzip command, bzip2 will compress the file that you select "in place", leaving only the original file.


        file.bz2文件解压缩方法:

tar -jxvf   file.bz2

bunzip2  file.bz2

5. 7z文件[解]压缩 

        不同版本的linux系统下命令可能会略有不同。 

7za x  file.7z  # 解压缩,redhat

7za a file.7z folderpath  # 压缩,redhat

6. Xz文件[解]压缩 


Xz is a popular algorithm for compressing files based on the LZMA algorithm. By convention, the name of a tar archive compressed with xz ends with either .tar.xz or .txz. 

        Xz文件创建

xz bigfile

        A relative newcomer to the compression command team, xz is a front runner in terms of how well it compresses files. Like (gzip, bzip), you only need to supply the file name to the command. Again, the original file is compressed in place.

        Xz文件解压(整个文件包)

tar -xf archive.tar.xz

tar -xvf archive.tar.xz

tar -xf archive.tar.xz -C extract-to-path

tar -xf archive.tar.xz --directory extract-to-path

xz -d bigfile.xz 

unxz bigfile.xz

         

        也可以解压某个或某些文件或文件夹:

tar -xf archive.tar.xz path-to-file1 path-to-file2 

tar -xf archive.tar.xz dir1 dir2

tar -xf archive.tar.xz --wildcards '*.png'

        也可以不解压只列出压缩包内容

        使用这个命令,需要指定待解压文件的精确的文件名(含路径)。当待解压包非常大,而只需要从中提取少量文件时可以用这种方法解压。如果要知道压缩包中有什么文件时,可以用"--list"或者“-t”选项。甚至还可以用通配符来指定提取某些有共同特征的文件。

Xz压缩文件创建

tar -cf archive.tar foo bar  # Create archive.tar from files foo and bar.

tar -cf archive.tar dir  # Create archive.tar from the specified directory.

7. Rar文件压缩与解压缩

Another way to compress files on Linux via the terminal is with Rar. Rar is a free utility that allows those who install it to interact with the RAR archive format. Rar is a proprietary format, so this tool is incredibly useful if you have RAR archives on your Linux PC or know someone who uses the format.

To compress files in with the Rar utility in Linux, you must first install the program. The reason that this program must be installed is that Unrar supports a proprietary format, and many Linux distributions don’t like that fact. 

rar文件压缩生成方法:

rar a -r my-rar-archive.rar /home/username/path/to/folder/

rar a my-rar-archive.rar /home/username/path/to/file.file

rar文件解压缩方法: 

unrar my-rar-archive.rar

8. 性能比较

        论压缩率:xz > bz2 > gz, tgz, zip

        论运行时间,恰与压缩率相反。这符合“No free lunch principle”,压缩率是以更复杂的运算换取的。

        顺便说一句,解压缩通常比压缩要快得多。

        bzip2gzip 和xz等是用压缩文件替换原文件,而tar,zip则会保留源文件。 

猜你喜欢

转载自blog.csdn.net/chenxy_bwave/article/details/130463034