技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機純凈版,64位旗艦版,綠色軟件,免費軟件下載基地!

當前位置:主頁 > 教程 > 服務(wù)器類 >

Linux中文件的壓縮與解壓縮命令操作示例集錦

來源:技術(shù)員聯(lián)盟┆發(fā)布時間:2018-01-08 18:13┆點擊:

  所謂壓縮就是將原有的文件通過不同的編碼技術(shù)進行運算,以減少數(shù)據(jù)存儲所需要的空間,使用前再利用解壓縮還原源文件的內(nèi)容即可。

  和windows一樣,在linux下也存在多種壓縮與解壓縮方法。

  1、zip壓縮與解壓縮

  zip是最為廣泛使用的壓縮程序,經(jīng)它壓縮的文件會產(chǎn)生擴展名為zip的壓縮文件,而且這種格式在多種系統(tǒng)上可以使用,像windows中的winzip

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

  我們在終端中輸入zip會出現(xiàn)這個命令的一些介紹和參數(shù)的意義。

  代碼如下:

  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$

  上面是壓縮了相同類型的文件,其實也可以把不同類型的文件壓縮到一起。有時候為了節(jié)省硬盤空間,可以在建立壓縮文件后,自動刪除原始文件,此時只要帶一個 -m 的參數(shù)就可以。

  代碼如下:

  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* 帶參數(shù)-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$

  可以看出 原始文件已經(jīng)被刪除,只有壓縮文件留下了。

  在壓縮一些目錄的時候,經(jīng)出在目錄中會有子目錄,此時根據(jù)子目錄中的文件是否壓縮分為兩種情況,一種是壓縮,一種是忽略自錄中的內(nèi)容,如果選擇壓縮子目錄,則使用-r參數(shù),如果不壓縮,則使用-j 參數(shù)

  下面舉例,一個是-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 * 壓縮當前目錄所有內(nèi)容,r 參數(shù)說明pdf這個子目錄中的內(nèi)容也壓縮

  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$