技术员联盟提供win764位系统下载,win10,win7,xp,装机纯净版,64位旗舰版,绿色软件,免费软件下载基地!

当前位置:主页 > 教程 > 服务器类 >

Linux中文件的压缩与解压缩命令操作示例集锦

来源:技术员联盟┆发布时间:2018-01-08 18:13┆点击:

  所谓压缩就是将原有的文件通过不同的编码技术进行运算,以减少数据存储所需要的空间,使用前再利用解压缩还原源文件的内容即可。

  和windows一样,在linux下也存在多种压缩与解压缩方法。

  1、zip压缩与解压缩

  zip是最为广泛使用的压缩程序,经它压缩的文件会产生扩展名为zip的压缩文件,而且这种格式在多种系统上可以使用,像windows中的winzip

  下面看一下在linux中如何建立zip文件。

  我们在终端中输入zip会出现这个命令的一些介绍和参数的意义。

  代码如下:

  xiaopeng@ubuntu:~/test$ zip

  Copyright (c) 1990-2006 Info-ZIP - Type 'zip "-L"' for software license.

  Zip 2.32 (June 19th 2006). Usage:

  zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]

  The default action is to add or replace zipfile entries from list, which

  can include the special name - to compress standard input.

  If zipfile and list are omitted, zip compresses stdin to stdout.

  -f freshen: only changed files -u update: only changed or new files

  -d delete entries in zipfile -m move into zipfile (delete files)

  -r recurse into directories -j junk (don't record) directory names

  -0 store only -l convert LF to CR LF (-ll CR LF to LF)

  -1 compress faster -9 compress better

  -q quiet operation -v verbose operation/print version info

  -c add one-line comments -z add zipfile comment

  -@ read names from stdin -o make zipfile as old as latest entry

  -x exclude the following names -i include only the following names

  -F fix zipfile (-FF try harder) -D do not add directory entries

  -A adjust self-extracting exe -J junk zipfile prefix (unzipsfx)

  -T test zipfile integrity -X eXclude eXtra file attributes

  -y store symbolic links as the link instead of the referenced file

  -R PKZIP recursion (see manual)

  -e encrypt -n don't compress these suffixes

  下面我们就最简单的实验一下。我们就是把当前目录下文件名以test开头的所有文件压缩文一个文件,并可以查看一下压缩比。(红色是我的注释)

  代码如下:

  xiaopeng@ubuntu:~/test$ ls -lh

  总用量 24K

  代码如下:

  -rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 14:13 test1

  -rw-r--r-- 1 xiaopeng xiaopeng 1.3K 2009-06-25 14:13 test2

  -rw-r--r-- 1 xiaopeng xiaopeng 3.4K 2009-06-25 14:14 test3

  -rw-r--r-- 1 xiaopeng xiaopeng 9.9K 2009-06-25 14:14 test4

  代码如下:

  xiaopeng@ubuntu:~/test$ zip test.zip test*

  zip命令后面先跟压缩后的文件名,这里是test.zip,当然后缀名不是必须的。然后跟要压缩的文件名。这里用的test*指的是全部以test开头的文件,包括test1 test2 test3 test4

  adding: test1 (deflated 30%) 这里显示的是压缩比

  adding: test2 (deflated 65%)

  adding: test3 (deflated 64%)

  adding: test4 (deflated 73%) 大体可以看出源文件越大,压缩比就越大

  代码如下:

  xiaopeng@ubuntu:~/test$ ls -lh

  总用量 32K

  代码如下:

  -rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 14:13 test1

  -rw-r--r-- 1 xiaopeng xiaopeng 1.3K 2009-06-25 14:13 test2

  -rw-r--r-- 1 xiaopeng xiaopeng 3.4K 2009-06-25 14:14 test3

  -rw-r--r-- 1 xiaopeng xiaopeng 9.9K 2009-06-25 14:14 test4

  -rw-r--r-- 1 xiaopeng xiaopeng 5.0K 2009-06-25 14:17 test.zip

  xiaopeng@ubuntu:~/test$

  上面是压缩了相同类型的文件,其实也可以把不同类型的文件压缩到一起。有时候为了节省硬盘空间,可以在建立压缩文件后,自动删除原始文件,此时只要带一个 -m 的参数就可以。

  代码如下:

  xiaopeng@ubuntu:~/test$ ls -lh

  总用量 24K

  代码如下:

  -rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 14:13 test1

  -rw-r--r-- 1 xiaopeng xiaopeng 1.3K 2009-06-25 14:13 test2

  -rw-r--r-- 1 xiaopeng xiaopeng 3.4K 2009-06-25 14:14 test3

  -rw-r--r-- 1 xiaopeng xiaopeng 9.9K 2009-06-25 14:14 test4

  xiaopeng@ubuntu:~/test$ zip -m test.zip test* 带参数-m

  updating: test1 (deflated 30%)

  updating: test2 (deflated 65%)

  updating: test3 (deflated 64%)

  updating: test4 (deflated 73%)

  xiaopeng@ubuntu:~/test$ ls -lh

  总用量 8.0K

  代码如下:

  -rw-r--r-- 1 xiaopeng xiaopeng 5.0K 2009-06-25 14:26 test.zip

  xiaopeng@ubuntu:~/test$

  可以看出 原始文件已经被删除,只有压缩文件留下了。

  在压缩一些目录的时候,经出在目录中会有子目录,此时根据子目录中的文件是否压缩分为两种情况,一种是压缩,一种是忽略自录中的内容,如果选择压缩子目录,则使用-r参数,如果不压缩,则使用-j 参数

  下面举例,一个是-r 一个是-j

  代码如下:

  xiaopeng@ubuntu:~/test$ ls -lh

  总用量 28K

  代码如下:

  drwxr-xr-x 2 xiaopeng xiaopeng 4.0K 2009-06-25 14:31 pdf

  -rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 14:13 test1

  -rw-r--r-- 1 xiaopeng xiaopeng 1.3K 2009-06-25 14:13 test2

  -rw-r--r-- 1 xiaopeng xiaopeng 3.4K 2009-06-25 14:14 test3

  -rw-r--r-- 1 xiaopeng xiaopeng 9.9K 2009-06-25 14:14 test4

  xiaopeng@ubuntu:~/test$ zip -r test.zip * 压缩当前目录所有内容,r 参数说明pdf这个子目录中的内容也压缩

  adding: pdf/ (stored 0%)

  adding: pdf/case_Contact.pdf (deflated 10%)

  adding: pdf/case_KRUU.pdf (deflated 9%)

  adding: pdf/case_howard_county_library.pdf (deflated 24%)

  adding: test1 (deflated 30%)

  adding: test2 (deflated 65%)

  adding: test3 (deflated 64%)

  adding: test4 (deflated 73%)

  xiaopeng@ubuntu:~/test$

  下面的情况是子目录不压缩

  代码如下:

  xiaopeng@ubuntu:~/test$ ls -l

  总用量 28

  代码如下:

  drwxr-xr-x 2 xiaopeng xiaopeng 4096 2009-06-25 14:31 pdf

  -rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 14:13 test1

  -rw-r--r-- 1 xiaopeng xiaopeng 1233 2009-06-25 14:13 test2

  -rw-r--r-- 1 xiaopeng xiaopeng 3412 2009-06-25 14:14 test3

  -rw-r--r-- 1 xiaopeng xiaopeng 10091 2009-06-25 14:14 test4

  xiaopeng@ubuntu:~/test$ zip -j test.zip *

  adding: test1 (deflated 30%)

  adding: test2 (deflated 65%)

  adding: test3 (deflated 64%)

  adding: test4 (deflated 73%)

  子目录pdf被忽略

  代码如下:

  xiaopeng@ubuntu:~/test$