所謂壓縮就是將原有的文件通過(guò)不同的編碼技術(shù)進(jìn)行運(yùn)算,以減少數(shù)據(jù)存儲(chǔ)所需要的空間,使用前再利用解壓縮還原源文件的內(nèi)容即可。
和windows一樣,在linux下也存在多種壓縮與解壓縮方法。
1、zip壓縮與解壓縮
zip是最為廣泛使用的壓縮程序,經(jīng)它壓縮的文件會(huì)產(chǎn)生擴(kuò)展名為zip的壓縮文件,而且這種格式在多種系統(tǒng)上可以使用,像windows中的winzip
下面看一下在linux中如何建立zip文件。
我們?cè)诮K端中輸入zip會(huì)出現(xiàn)這個(gè)命令的一些介紹和參數(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
下面我們就最簡(jiǎn)單的實(shí)驗(yàn)一下。我們就是把當(dāng)前目錄下文件名以test開(kāi)頭的所有文件壓縮文一個(gè)文件,并可以查看一下壓縮比。(紅色是我的注釋)
代碼如下:
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,當(dāng)然后綴名不是必須的。然后跟要壓縮的文件名。這里用的test*指的是全部以test開(kāi)頭的文件,包括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$
上面是壓縮了相同類(lèi)型的文件,其實(shí)也可以把不同類(lèi)型的文件壓縮到一起。有時(shí)候?yàn)榱斯?jié)省硬盤(pán)空間,可以在建立壓縮文件后,自動(dòng)刪除原始文件,此時(shí)只要帶一個(gè) -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)被刪除,只有壓縮文件留下了。
在壓縮一些目錄的時(shí)候,經(jīng)出在目錄中會(huì)有子目錄,此時(shí)根據(jù)子目錄中的文件是否壓縮分為兩種情況,一種是壓縮,一種是忽略自錄中的內(nèi)容,如果選擇壓縮子目錄,則使用-r參數(shù),如果不壓縮,則使用-j 參數(shù)
下面舉例,一個(gè)是-r 一個(gè)是-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 * 壓縮當(dāng)前目錄所有內(nèi)容,r 參數(shù)說(shuō)明pdf這個(gè)子目錄中的內(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$