1. 程式人生 > >第07課 壓縮與打包

第07課 壓縮與打包

目錄

  1. 常見壓縮格式
  2. gzip
  3. bzip2
  4. xz
  5. zip
  6. tar

1. 常見壓縮格式

  • Windows:.rar.zip.7z

  • Linux:.zip.gz.bz2.xz.tar.gz.tar.bz2.tar.xz

2. gzip

Linux 上最經典也是最常見的壓縮軟體,但是已經有些年頭了,演算法、壓縮率等已不及 bzip2、xz、zip 等

2.1 特點

  • 壓縮比與其他後起之秀相比還是比較差的
  • 還是目前見到的使用最多的壓縮工具
  • 無法壓縮目錄
  • 預設情況下,壓縮或解壓縮完成後,都會將原檔案刪除
  • 檢視壓縮檔案只能使用 zcat
    命令
  • 預設壓縮比為 6

2.2 選項

  • -d:解壓
  • -#:指定壓縮比(1~9,預設為6)
  • -c:重定向至螢幕(常用於保留原檔案)
  • -v:顯示壓縮過程
  • -t:顯示壓縮比

2.3 用法

  • 壓縮
    gzip <file>
    [root@localhost tmp]# find /etc/ -type f -name "*.conf" -exec cat {} >> 1.txt \;
    [root@localhost tmp]# ll
    總用量 132
    -rw-r--r--. 1 root root 131592 527
17:15 1.txt [root@localhost tmp]# du -sh 1.txt 132K 1.txt [root@localhost tmp]# gzip 1.txt ; ll 總用量 36 -rw-r--r--. 1 root root 36836 527 17:15 1.txt.gz # 原檔案被刪除了,自動命名為相同檔名加.gz [root@localhost tmp]# du -sh 1.txt.gz 36K 1.txt.gz #壓縮後明顯變小了
  • 解壓縮
    gzip -d <file.gz>gunzip <file.gz>
    [[email protected] tmp]# gzip -d 1.txt.gz ; ll
    總用量 132
    -rw-r--r--. 1 root root 131592 5月  27 17:15 1.txt
    [[email protected] tmp]# gunzip 1.txt.gz ; ll
    總用量 260
    -rw-r--r--. 1 root root 263184 5月  27 17:20 1.txt
  • 保留原檔案或改名
    gzip -c <file> > <rename.gz>
    [[email protected] tmp]# gzip -c 1.txt > 100.gz ; ll         # 壓縮時保留原檔案並自行命名
    總用量 332
    -rw-r--r--. 1 root root  73397 5月  27 17:25 100.gz
    -rw-r--r--. 1 root root 263184 5月  27 17:20 1.txt

    [[email protected] tmp]# gzip -d -c 100.gz > new.txt ; ll    #解壓時保留原檔案並自行命名
    總用量 592
    -rw-r--r--. 1 root root  73397 5月  27 17:25 100.gz
    -rw-r--r--. 1 root root 263184 5月  27 17:20 1.txt
    -rw-r--r--. 1 root root 263184 5月  27 17:27 new.txt

3. bzip2

除了壓縮演算法更優良外,其他基本與 gzip 相同

3.1 特點

  • 無法壓縮目錄
  • 預設情況下,壓縮或解壓縮完成後,都會將原檔案刪除,但-k選項可保留原檔案
  • 檢視壓縮檔案只能使用 bzcat 命令
  • 預設壓縮比為 9

3.2 選項

  • -d:解壓
  • -#:指定壓縮比(1~9,預設為9)
  • -k:保留原檔案
  • -c:重定向至螢幕(常用於保留原檔案)
  • -v:顯示壓縮過程
  • -t:顯示壓縮比

3.3 用法

  • 壓縮
    bzip2 <file>
    [root@localhost tmp]# ls
    1.txt
    [root@localhost tmp]# bzip2 1.txt ; ll
    總用量 44
    -rw-r--r--. 1 root root 43017 527 17:20 1.txt.bz2
    [root@localhost tmp]# du -hs 1.txt.bz2
    44K 1.txt.bz2
  • 解壓縮
    bzip2 -d <file.bz2>bunzip2 <file.bz2>
    [[email protected] tmp]# ls
    1.txt.bz2
    [[email protected] tmp]# bzip2 -d 1.txt.bz2
    [[email protected] tmp]# ls
    1.txt
    [[email protected] tmp]# gzip 1.txt
    [[email protected] tmp]# ls
    1.txt.gz
    [[email protected] tmp]# bunzip2 1.txt.gz ;ll
    bunzip2: Can't guess original name for 1.txt.gz -- using 1.txt.gz.out
    bunzip2: 1.txt.gz is not a bzip2 file.
    總用量 72
    -rw-r--r--. 1 root root 73397 5月  27 17:20 1.txt.gz
  • 保留原檔案
    bzip2 -k <file>bzip2 -d -k <file.bz2>
    [root@localhost tmp]# ls
    1.txt
    [root@localhost tmp]# bzip2 -k 1.txt ; ll
    總用量 304
    -rw-r--r--. 1 root root 263184 527 17:20 1.txt                # 並沒有刪掉原檔案
    -rw-r--r--. 1 root root  43017 527 17:20 1.txt.bz2

    ## 解壓試試
    [root@localhost tmp]# ls
    1.txt.bz2
    [root@localhost tmp]# bzip2 -dk 1.txt.bz2 ; ls
    1.txt  1.txt.bz2
    [root@localhost tmp]#
  • 保留原檔案且改變檔名
    bzip2 -c <file> > <rename.bz2>
    [root@localhost tmp]# ls
    1.txt
    [root@localhost tmp]# bzip2 -c 1.txt > 2.txt.bz2 ; ls
    1.txt  2.txt.bz2
    [root@localhost tmp]#

4. xz

壓縮演算法更先進,用法與gzipbzip2 相同

4.1 特點

  • 無法壓縮目錄
  • 預設情況下,壓縮或解壓縮完成後,都會將原檔案刪除
  • 檢視壓縮檔案只能使用 xzcat 命令

4.2 選項

  • -d:解壓
  • -#:指定壓縮比(1~9,預設為6)
  • -k:保留原檔案
  • -c:重定向至螢幕(常用於保留原檔案)
  • -v:顯示壓縮過程

4.3 用法

  • 壓縮
    xz <file>
    [root@localhost tmp]# ls
    1.txt
    [root@localhost tmp]# du -sh 1.txt
    260K    1.txt
    [root@localhost tmp]# xz 1.txt ; ll
    總用量 36
    -rw-r--r--. 1 root root 33892 527 17:20 1.txt.xz
    [root@localhost tmp]# du -sh 1.txt.xz
    36K 1.txt.xz
  • 解壓縮
    xz -d <file.xz>unxz <file.xz>
    [root@localhost tmp]# ls
    1.txt.xz
    [root@localhost tmp]# xz -d 1.txt.xz ; ll
    總用量 260
    -rw-r--r--. 1 root root 263184 527 17:20 1.txt
    [root@localhost tmp]# ls
    1.txt.xz
    [root@localhost tmp]# unxz 1.txt.xz ; ll
    總用量 260
    -rw-r--r--. 1 root root 263184 527 17:20 1.txt
  • 保留原檔案
    xz -k <file>xz -d -k <file.xz>
    [root@localhost tmp]# ls
    1.txt
    [root@localhost tmp]# xz -k 1.txt ; ll
    總用量 296
    -rw-r--r--. 1 root root 263184 527 17:20 1.txt              #並沒有刪掉原檔案
    -rw-r--r--. 1 root root  33892 527 17:20 1.txt.xz


    ## 解壓試試
    [root@localhost tmp]# ls
    1.txt.xz
    [root@localhost tmp]# xz -dk 1.txt.xz ; ll
    總用量 296
    -rw-r--r--. 1 root root 263184 527 17:20 1.txt
    -rw-r--r--. 1 root root  33892 527 17:20 1.txt.xz
    [root@localhost tmp]#
  • 保留原檔案且改變檔名
    xz -c <file> > <rename.xz>
    [[email protected] tmp]# ls
    1.txt
    [[email protected] tmp]# xz -c 1.txt > 2.txt.xz ; ll
    總用量 296
    -rw-r--r--. 1 root root 263184 5月  27 17:20 1.txt
    -rw-r--r--. 1 root root  33892 5月  27 18:37 2.txt.xz

5. zip、unzip

目前所知,唯一能壓縮目錄的純壓縮軟體
用法與gzipbzip2xz 不同

5.1 特點

  • 能夠壓縮目錄
  • 預設不刪除原檔案,這樣可能導致解壓時會覆蓋原檔案
  • 配套解壓工具 unzip 需要單獨安裝
  • 無類似zcatbzcatxzcat檢視檔案內容的工具,僅有-l選項可檢視壓縮包中的檔案列表

5.2 選項

  • zip:
    -r:壓縮目錄
    -#:1-9,壓縮比(預設為6)

  • unzip:
    -l:檢視壓縮包中檔案列表

5.3 用法

  • 壓縮檔案
    zip <rename.zip> <file>
    [[email protected] tmp]# ls
    1.txt
    [[email protected] tmp]# zip 1.txt.zip 1.txt ; ll
      adding: 1.txt (deflated 72%)
    總用量 332
    -rw-r--r--. 1 root root 263184 5月  27 17:20 1.txt
    -rw-r--r--. 1 root root  73533 5月  27 18:48 1.txt.zip
  • 壓縮目錄
    zip -r <rename.zip> <dir>
    [[email protected] tmp]# tree
    .
    └── test
        ├── 1
        └── 2

    1 directory, 2 files
    [[email protected] tmp]# zip -r test.zip test/
      adding: test/ (stored 0%)
      adding: test/1 (stored 0%)
      adding: test/2 (stored 0%)
    [[email protected] tmp]# ls
    test  test.zip
    [[email protected] tmp]# unzip -l test.zip
    Archive:  test.zip
      Length      Date    Time    Name
    ---------  ---------- -----   ----
            0  05-27-2018 18:52   test/
            0  05-27-2018 18:52   test/1
            0  05-27-2018 18:52   test/2
    ---------                     -------
            0                     3 files
  • 解壓縮
    由於預設不刪原檔案,解壓時將可能碰到需要覆蓋原檔案的情況
    unzip <file.zip>
    [[email protected] tmp]# ls
    test  test.zip
    [[email protected] tmp]# unzip test.zip
    Archive:  test.zip
    replace test/1? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
     extracting: test/1
    replace test/2? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
     extracting: test/2
  • 指定解壓的目標目錄
    unzip <file.zip> -d <dst_dir>
    [root@localhost tmp]# ls
    aaa  test  test.zip
    [root@localhost tmp]# unzip test.zip -d aaa/ ; tree aaa
    Archive:  test.zip
       creating: aaa/test/
     extracting: aaa/test/1
     extracting: aaa/test/2
    aaa
    └── test
        ├── 1
        └── 2

    1 directory, 2 files

6. tar

最常見的打包工具,並且能獲得 gzip、bzip2、xz 的支援,打包後進一步降低使用空間

6.1 特點

  • 可以打包並壓縮目錄
  • 可獲得壓縮工具的支援
  • 命令分打包、檢視、解包三種

6.2 選項

*-c: 建立包
*-t:檢視包
*-x:解包
* -C:解包至指定位置
*-v:顯示詳細過程
*-f:後接 tar 包的名字
*-z:獲取 gzip 的支援
*-j:獲取 bzip2 的支援
*-J:獲取 xz 的支援
*--exclude=:打包時,排除個別檔案或目錄

6.3 用法

  • 打包
    tar -cvf <file.tar> <file1> <file2> <dir1>
    [root@localhost tmp]# tar -cvf www.tar aaa/ test/ test.zip
    aaa/
    aaa/test/
    aaa/test/1
    aaa/test/2
    test/
    test/1
    test/2
    test.zip
  • 打包且壓縮
    tar -vzvf <file.tar.gz> <file1> <dir1>
    [root@localhost tmp]# tar -czvf etc.tar.gz /etc/
    tar: 從成員名中刪除開頭的“/”
    /etc/
    /etc/fstab
    /etc/crypttab
    /etc/mtab
    .
    .
    .
    [root@localhost tmp]# ls
    etc.tar.gz
  • 打包,耽排除個別檔案
    [[email protected] tmp]# ls
    1  100  2  etc.tar.bz2
    [[email protected] tmp]# tar -czvf /tmp.tar.bz2  /tmp/ --exclude=1
    tar: 從成員名中刪除開頭的“/”
    /tmp/
    /tmp/.X11-unix/
    /tmp/.ICE-unix/
    /tmp/.XIM-unix/
    /tmp/.font-unix/
    /tmp/.Test-unix/
    /tmp/etc.tar.bz2
    /tmp/2
    /tmp/100
    [[email protected] tmp]# tar -tzf /tmp.tar.bz2
    tmp/
    tmp/.X11-unix/
    tmp/.ICE-unix/
    tmp/.XIM-unix/
    tmp/.font-unix/
    tmp/.Test-unix/
    tmp/etc.tar.bz2
    tmp/2
    tmp/100
  • 解包
    tar -xzvf <file.tar.gz>
    [[email protected] tmp]# ls
    etc.tar.gz
    [[email protected] tmp]# tar -xzf etc.tar.gz
    [[email protected] tmp]# ll
    總用量 6544
    drwxr-xr-x. 74 root root    4096 527 17:53 etc
    -rw-r--r--.  1 root root 6692238 527 19:11 etc.tar.gz
  • 解包至指定位置
    [root@localhost tmp]# ll
    總用量 5704
    -rw-r--r--. 1 root root 5837206 527 19:18 etc.tar.bz2
    [root@localhost tmp]# ll /root/
    總用量 0
    [root@localhost tmp]# tar -xjf etc.tar.bz2 -C /root/
    [root@localhost tmp]# ll /root/
    總用量 8
    drwxr-xr-x. 74 root root 4096 527 17:53 etc
  • 檢視包內容
    tar -tjf <file.tar.bz2>
    [root@localhost tmp]# ls
    etc.tar.bz2
    [root@localhost tmp]# tar -tjf etc.tar.bz2
    etc/
    etc/fstab
    etc/crypttab
    etc/mtab
    etc/pki/
    etc/pki/rpm-gpg/

相關推薦

07 壓縮打包

目錄 常見壓縮格式 gzip bzip2 xz zip tar 1. 常見壓縮格式 Windows:.rar、.zip、.7z Linux:.zip、.gz、.bz2、.xz、.tar.gz、.tar.

九章、文件文件系統的壓縮打包

一個 and cpio ora 自己 align node filename ace 第九章、文件與文件系統的壓縮與打包 1. 壓縮文件的用途與技術 2. Linux 系統常見的壓縮命令   2.1 compress   2.2 gzip, zcat   2.3 bzip2

八章-文檔的壓縮打包

有一種 .gz direct 壓縮工具 local updating 是否 空間 bzcat 在windows下我們接觸最多的壓縮文件就是.rar格式的了。但在linux下這樣的格式是不能識別的,它有自己所特有的壓縮工具。但有一種文件在windows和linux下都能使用那

[CentOS 7系列]壓縮打包(下)

linux windows 壓縮文件 壓縮包 除了gzip、bzip2和xz外,linux中還有一款壓縮軟件。它支持壓縮目錄,也可以解壓windows中同類型的文件。它就是我們熟悉的zip。1、zip命 令作 用zip 1.txt.zip 1.txt壓縮zip -r 123.zip

關於文件文件系統的壓縮打包命令-Linux(筆記)

ber 磁盤 數據流重定向 java lock level lines markdown con 1、gzip : 壓縮命令 gzip [-cdtv#] 文件名稱 (後綴為.gz) -c :將壓縮的數據輸出到屏幕上,可通過數據流重定向處理

壓縮打包

壓縮 打包 gzip zcat bzip2 bzcat 壓縮與打包打包壓縮的用途: 減少磁盤使用空間 方便備份目錄等多個文件 方便傳輸,減少帶寬打包壓縮的技術: 簡單理解為把不完全滿的空間壓縮填滿壓縮的命令:gzip、zcat、bzip2、bzcat、xzgzi

Linux學習之三:檔案文件系統的壓縮打包

常用 etc 存在 filename 目錄 時有 blog 備份工具 restore 將檔案進行壓縮處理是為了使文件更加方便在網絡上傳輸以及降低硬盤使用量。進行壓縮的原理就是檔案在存儲時有很多的空間是無用的,而壓縮就是將這些空間給釋放出來。 Linux下幾種常見的壓縮方式後

Linux文件壓縮打包

hello tar.gz 拓展 數據流 tar打包 div log 變慢 str 1.壓縮與打包 壓縮:利用更節省空間的記錄方式來記錄文件數據,讓文件占用的容量下降。優點:相同容量能夠存儲更多數據,傳輸時數據量降低,從而速度更快。 打包:將多個文件打包為一個大文件,實際文件

Excel課程學習排序替換

分享圖片 選中 表格 技術分享 定位 自定義 復制 可見 方向 一、排序 1、簡單排序 點到某一個單元格,然後選擇排序,就可以按照相應的順序來排序。 2、自定義排序 按照重要性條件來排序 也可以按照重要性從輕到重挨個排序。 3、按顏色排序 4、 按照中文數字排序,按照自

文檔的壓縮打包

title png oss alt itl watermark size mage ges 文檔的壓縮與打包

文件的壓縮打包

font 解壓 支持 壓縮 dirname 更新 中一 class 16px 1.gzip 2.bzip2 [-dz] 文件( -z 壓縮文件,-d 解壓縮)加或者不加z都可壓縮 文件 3.tar 選項:-f : 使用文檔名字,放在最後,後面只跟文件名(必須用)   這

潭州課堂25班:Ph201805201 爬蟲基礎 Python常見加密方式 (課堂筆記)

加密算法 string 寫法 one python dac 獲得 分解 符號 前言 我們所說的加密方式,都是對二進制編碼的格式進行加密的,對應到Python中,則是我們的Bytes。 所以當我們在Python中進行加密操作的時候,要確保我們操作的是Bytes,否則就會報錯。

iOS NSArrayNSMutableArray

NSArray基本概念 什麼是NSArray NSArray是OC中的陣列類,開發中建議儘量使用NSArray替代C語言中的陣列 NSArray有哪些使用注意 只能存放任意OC物件, 並且是有順序的 不能儲存非OC物件, 比如int\float\double\char\enu

《快學 Go 語言》 10 —— 錯誤異常

Go 語言的異常處理語法絕對是獨樹一幟,在我見過的諸多高階語言中,Go 語言的錯誤處理形式就是一朵奇葩。一方面它鼓勵你使用 C 語言的形式將錯誤通過返回值來進行傳遞,另一方面它還提供了高階語言一般都有的異常丟擲和捕獲的形式,但是又不鼓勵你使用這個形式。後面我們統一將返回值形式的稱為「錯誤」,將丟擲捕獲形式的稱

課課通2單元4—字元字串

//p2-4-1 #include<iostream> using namespace std; int main(){    char c1,c2,c3;    c1 = 48;    c2 = 65;   &nbs

課課通2單元3-常量變數

2.閱讀程式,寫出結果 #include<iostream> using namespace std; int a,b,c,ans = 100; int main(){     a = 1; b = 2; c = 5;     a = a +

AutoCAD2012從入門到精通中文視訊教程 19 修剪延伸(個人收藏)

修剪和延伸的基本技巧首先是選擇,首先要選擇修剪、延伸邊界,或稱為切割物件,也就是選擇作為修剪和延伸的基準的物件,然後就是選擇要被修剪或延伸的物件,掌握了這兩者的選擇技巧就基本掌握了修剪和延伸的操作。 CAD修剪(Trim)和延伸(Extend)的使用技巧 2

《快學 Go 語言》 13 —— 併發安全

上一節我們提到併發程式設計不同的協程共享資料的方式除了通道之外還有就是共享變數。雖然 Go 語言官方推薦使用通道的方式來共享資料,但是通過變數來共享才是基礎,因為通道在底層也是通過共享變數的方式來實現的。通道的內部資料結構包含一個數組,對通道的讀寫就是對內部陣列的讀寫。 在併發環境下共享讀寫變數必須要使用鎖

Delphi 之 過程函式

  這講是核心重點*****     什麼是過程?什麼又是函式?過程和函式在delphi中無處不再。過程簡單的理解就是單擊一個按鈕這就是一個過程,函式和過程不一樣的地方就是函式能把這個過程的結果返回給我們,過程的關鍵字用procedure ,函式的關鍵字Function 

linux中檔案壓縮打包

一.常見的壓縮命令     在linux環境中,壓縮檔案的副檔名大多是*.tar,*.tar.gz,*.tgz,*.gz,*.Z,*.bz2,首先我們來介紹以下這些壓縮文案的副檔名:、     *.Z:compress程式壓縮的檔案