[拾 得] zip gzip bzip2 & tar 壓縮/打包 四大金剛
阿新 • • 發佈:2017-10-08
減少 www pex utm mage err direct $? zip命令 堅持知識分享,該文章由Alopex編著, 轉載請註明源地址: http://www.cnblogs.com/alopex/
索引:
由上表,我們可以看出
從壓縮時間: 我們可以看出, zip & gzip 時間相差是很小的 並且顯著比 bzip2 用時短接近 42%
從壓縮效率: 我們可以看出, bzip2的壓縮率最高, 明顯高於zip & gzip, 高出了一大截.
這也表明了, 計算機中其中一大矛盾 , 性能 和 時間 二者是很難兼得的
目錄
參考網站:
http://m.hangge.com/news/cache/detail_1666.html Linux - zip壓縮、unzip解壓縮命令的使用詳解
https://www.wikiwand.com/en/Zip_(file_format) Zip (file format)
https://my.oschina.net/CandyMi/blog/688887 Linux 使用unzip命令解壓其中的單個文件到指定文件夾
https://www.qcloud.com/community/article/164816001481011798 如何在 Linux 系統通過命令行生成隨機文件
- 介紹壓縮和打包
- gzip bzip2 zip 的基本使用
- gzip bzip2打包文件 需要 tar 的支援
- 同一文件使用不同壓縮工具的比較
- 對zip gzip bzip2 信息總結
- 能夠完成文件的壓縮和解壓的基本操作
- 能夠明白壓縮和打包的概念
- 對於選擇何種壓縮命令有初步認識
- 涉及命令 : zip(unzip), gzip, bzip2, tar, dd, time, tree
- 介紹壓縮和打包
- zip gzip bzip2 的基本使用
- 壓縮文件
- 解壓(指定路徑)zip壓縮文件
- 查看文件情況, 驗證文件完整性
- 壓縮文件夾
- 指定文件解壓
- 刪除zip壓縮文件中的文件
- 增加指定文件到 zip壓縮文件中
> ########################################################1 > # 使用zip壓縮文件 zip 命令後添加 壓縮後文件的名字, 約定成俗的習慣是 .zip 後跟需要壓縮的文件 > # zip filegzip 在Linux中常用的壓縮命令之一 默認情況下 [gzip 壓縮/解壓後原文件將消失] [不能用於壓縮文件夾] 例子目錄索引:.zip file > ls dir video.mp4 > zip video.mp4.zip video.mp4 adding: video.mp4 (deflated 100%) > ls dir video.mp4 video.mp4.zip #########################################################2 > # 解壓zip 文件 , 解壓後 原壓縮包 不會被刪除 > # 解壓zip 文件, 指定解壓路徑 > ls dir video.mp4.zip > unzip video.mp4.zip Archive: video.mp4.zip inflating: video.mp4 > ls dir video.mp4 video.mp4.zip > unzip video.mp4.zip -d dir/ Archive: video.mp4.zip inflating: dir/video.mp4 > ls dir/video.mp4 dir/video.mp4 > > #########################################################3 > # 使用 unzip -v 查看 zip 壓縮文件中的文件, 但不解壓文件 > unzip -v video.mp4.zip Archive: video.mp4.zip Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 268435456 Defl:N 260516 100% 10-02-2017 21:55 2a0e7dbb video.mp4 -------- ------- --- ------- 268435456 260516 100% 1 file > # 使用unzip -t 選項 查看 zip 壓縮文件 是否有損壞 > unzip -t video.mp4.zip Archive: video.mp4.zip testing: video.mp4 OK No errors detected in compressed data of video.mp4.zip. > #########################################################4 > # 使用 unzip -r 遞歸壓縮 壓縮文件夾(dir/) > ls dir video.mp4 > zip -r compress.zip dir/ video.mp4 adding: dir/ (stored 0%) adding: dir/file2 (stored 0%) adding: dir/file1 (stored 0%) adding: video.mp4 (deflated 100%) > #########################################################5 > # 查看 zip 壓縮文件中的文件, 創建一個 another_dir > # 使用 unzip [zip壓縮文件] [需要解壓的文件] -d [解壓文件到指定的目錄] > # 實現指定解壓zip文件中指定文件 > unzip -v compress.zip Archive: compress.zip Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 0 Stored 0 0% 10-02-2017 23:20 00000000 dir/ 16 Stored 16 0% 10-02-2017 23:21 b9bb280c dir/file2 16 Stored 16 0% 10-02-2017 23:21 b9bb280c dir/file1 268435456 Defl:N 260516 100% 10-02-2017 21:55 2a0e7dbb video.mp4 -------- ------- --- ------- 268435488 260548 100% 4 files > mkdir another_dir > ls -F another_dir/ compress.zip dir/ video.mp4 > unzip compress.zip dir/file1 -d another_dir/ Archive: compress.zip extracting: another_dir/dir/file1 > ls -l another_dir/dir/file1 -rw-r--r-- 1 root root 16 Oct 2 23:21 another_dir/dir/file1 > #########################################################6 > # 在無需解壓的情況下, 刪除相關的文件 > # zip [需要操作的zip文件] -d [需要刪除的文件] > unzip -v compress.zip Archive: compress.zip Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 0 Stored 0 0% 10-02-2017 23:20 00000000 dir/ 16 Stored 16 0% 10-02-2017 23:21 b9bb280c dir/file2 16 Stored 16 0% 10-02-2017 23:21 b9bb280c dir/file1 268435456 Defl:N 260516 100% 10-02-2017 21:55 2a0e7dbb video.mp4 -------- ------- --- ------- 268435488 260548 100% 4 files > zip compress.zip -d dir/file2 deleting: dir/file2 > unzip -v compress.zip Archive: compress.zip Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 0 Stored 0 0% 10-02-2017 23:20 00000000 dir/ 16 Stored 16 0% 10-02-2017 23:21 b9bb280c dir/file1 268435456 Defl:N 260516 100% 10-02-2017 21:55 2a0e7dbb video.mp4 -------- ------- --- ------- 268435472 260532 100% 3 files > #########################################################7 > # 添加新的文件到 zip 壓縮文件中 > # zip [需要操作的zip文件] -g [需要添加的文件] > zip -g compress.zip dir/file2 adding: dir/file2 (stored 0%) > unzip -v compress.zip Archive: compress.zip Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 0 Stored 0 0% 10-02-2017 23:20 00000000 dir/ 16 Stored 16 0% 10-02-2017 23:21 b9bb280c dir/file1 268435456 Defl:N 260516 100% 10-02-2017 21:55 2a0e7dbb video.mp4 16 Stored 16 0% 10-02-2017 23:21 b9bb280c dir/file2 -------- ------- --- ------- 268435488 260548 100% 4 files
- 壓縮文件 , 和保留原文件壓縮
- 解壓(指定路徑)gzip壓縮文件 , 和保留原文件壓縮
- 查看文件情況, 驗證文件完整性
> #########################################################1 > # 使用gzip 壓縮文件,自動為文件添加後綴 .gz 不保留原文件 > # gzip <需要壓縮的文件> > ls -F dir/ video.mp4 > gzip video.mp4 > ls -F dir/ video.mp4.gz > # # # # # # # # # # # # > # 使用 gzip 壓縮文件, 保留原文件 > # 這裏使用的是重定向方式 , 借此保留了原文件 > # gzip -c <需要壓縮的文件> > <壓縮後文件的名字> > ls -F dir/ video.mp4 > gzip -c video.mp4 > video.mp4.gz > ls -F dir/ video.mp4 video.mp4.gz > #########################################################2 > # 使用gzip 解壓文件 , 命令 gzip -d (decompose) <需要解壓的文件> > # 解壓後原文件會消失 > ls -F dir/ video.mp4.gz > gzip -d video.mp4.gz > ls -F dir/ video.mp4 > # # # # # # # # # # # # > # 使用 gzip 解壓文件, 命令 gzip -cd <需要解壓的文件> > <解壓後文件的命名> > # 保留原文件 > ls -lh total 260K drwxr-xr-x 2 root root 4.0K Oct 3 00:06 dir -rw-r--r-- 1 root root 255K Oct 3 00:25 video.mp4.gz > gzip -cd video.mp4.gz > video.mp4 > ls -lh total 257M drwxr-xr-x 2 root root 4.0K Oct 3 00:06 dir -rw-r--r-- 1 root root 256M Oct 3 00:40 video.mp4 -rw-r--r-- 1 root root 255K Oct 3 00:25 video.mp4.gz > #########################################################3 > # 查看壓縮文件相關信息 gzip -l <需要查看的壓縮文件> > # 測試壓縮文件的完整性 gzip -t <需要驗證的壓縮文件> > gzip -l video.mp4.gz compressed uncompressed ratio uncompressed_name 260544 268435456 99.9% video.mp4 > gzip -t video.mp4.gz > echo $? 0bzip2 在Linux中常用的壓縮命令之一 默認情況下 [bzip2 壓縮/解壓後原文件將消失] [不能用於壓縮文件夾] 例子目錄索引:
- 壓縮文件 , 和保留原文件壓縮
- 解壓(指定路徑)bzip壓縮文件 , 和保留原文件壓縮
- 查看文件情況, 驗證文件完整性
> #########################################################1 > # 使用bzip2 壓縮文件,自動為文件添加後綴 .bz 不保留原文件 > # bzip2 <需要壓縮的文件> > ls -F dir/ video.mp4 > bzip2 video.mp4 > ls -F dir/ video.mp4.bz2 > # # # # # # # # # # # # > # 使用 -k (keep) 解壓文件, 保留原文件 > # bzip2 -k <需要壓縮的文件> > ls -F dir/ video.mp4 > bzip2 -k video.mp4 > ls -F dir/ video.mp4 video.mp4.bz2 > #########################################################2 > # 使用bzip2 解壓文件 , 命令 bzip2 -d (decompose) <需要解壓的文件> > # 解壓後原文件會消失 > ls -F dir/ video.mp4.bz2 > bzip2 -d video.mp4.bz2 > ls -F dir/ video.mp4 > # # # # # # # # # # # # > # 使用 gzip 解壓文件, 命令 bzip2 -k <需要解壓的文件> > # 保留原文件 > ls -F dir/ video.mp4.bz2 > bzip2 -dk video.mp4.bz2 > ls -F dir/ video.mp4 video.mp4.bz2 > #########################################################3 > # 查看壓縮文件相關信息 bzcat <需要查看的壓縮文件> > # 測試壓縮文件的完整性 bzip2 -t <需要驗證的壓縮文件> > cat file1 This is a file. > bzip2 -k file1 > bzcat file1.bz2 This is a file. > > bzip2 -t video.mp4.bz2 > echo $? 0
- gzip bzip2打包文件 需要 tar 的支援
> # 使用 tar 進行打包 -c 創建一個打包文件 > # 值得關註的是 命令執行後 打包的原始文件(../dir/file1 和 ../dir/file2)是不會消失的 > # tar -cf <打包後的文件名.tar> <需要打包的文件> ... <需要打包的文件> > ls dir video.mp4 > tree dir/ dir/ ├── file1 └── file2 0 directories, 2 files > tar -cf dir.tar dir/file1 dir/file2 > ls dir dir.tar video.mp4 > > # 顯示打包後的文件 -t 顯示打包文件中的信息 > # tar -tf <需要查看的打包文件.tar> > tar -tf dir.tar dir/file1 dir/file2 > # 如果希望得到更加詳細的信息(權限,用戶,用戶組,修改時間mtime), 可以加上 -v 選項 > # tar -tvf <需要查看的打包文件.tar> > tar -tvvf dir.tar -rw-r--r-- root/root 16 2017-10-02 23:21 dir/file1 -rw-r--r-- root/root 16 2017-10-02 23:21 dir/file2 > # 在原有的打包文件中,追加文件 -r 追加文件到打包文件中 > # 值得關註的是 命令執行後 追加的文件(video.mp4)是不會消失的 > # tar -rvf <需要追加的文件> <存在的打包文件.tar> > tar -rvf dir.tar video.mp4 video.mp4 > tar -tf dir.tar dir/file1 dir/file2 video.mp4 > # 在打包文件中 提取指定文件 -x 提取文件 > # tar -xf <打包文件.tar> <在打包文件中需要提取的文件> > ls -F dir/ dir.tar > tar -tf dir.tar dir/file1 dir/file2 video.mp4 > tar -xf dir.tar video.mp4 > ls -F dir/ dir.tar video.mp4 > # 指定存取目錄, 將打包文件提取到該路徑下 -C /PATH/TO/SOMEDIR > # 值得註意的是, 指定的存取路徑下文件夾必須存在 > # tar -xf <打包的文件.tar> -C /存取/的/目錄 > ls -F another_dir/ dir/ dir.tar video.mp4 > tar -xf dir.tar -C another_dir > tree another_dir/ another_dir/ ├── dir │ ├── file1 │ └── file2 └── video.mp4 1 directory, 3 files > # 將兩個打包文件進行合並 > # 值得註意的是,放在前面的合並文件, 將會成為合並後的文件(包含了自己內容和被合並文件的內容) > # 被合並的文件在合並後, 不會小時 > # tar -Af <合並後的文件/合並的打包文件.tar> <被合並的打包文件2.tar> > ls -F dir/ dir.tar video.mp4 video.tar > tar -tf dir.tar dir/file1 dir/file2 > tar -tf video.tar video.mp4 > tar -Af dir.tar video.tar > tar -tf dir.tar dir/file1 dir/file2 video.mp4 > ls -F dir/ dir.tar video.mp4 video.tar > tar -tf video.tar video.mp4 > # 刪除打包文件中的指定文件 --delete > # tar --delete --file <包含刪除文件的打包文件.tar> <需要刪除的文件> .. <需要刪除的文件> > # tar -f <包含刪除文件的打包文件.tar> --delete <需要刪除的文件> .. <需要刪除的文件> > # 方法一 > tar -tf dir.tar dir/file1 dir/file2 > tar --delete --file dir.tar dir/file1 > tar -tf dir.tar dir/file2 > > # 方法二 > tar -tf dir.tar dir/file1 dir/file2 > tar -f dir.tar --delete dir/file1 > tar -tf dir.tar dir/file2 > # # # # # # # # # # # # # # # # # # # # # # # # # # # 這裏插播一下 , 如果僅僅對數據進行打包處理 # 我們的文件大小是不會減少的 # 反倒會增加文件的大小 > ls dir video.mp4 > tar -cf dir.tar dir/* > tar -cf video.tar video.mp4 > ls -lh total 513M drwxr-xr-x 2 root root 4.0K Oct 3 10:14 dir -rw-r--r-- 1 root root 20K Oct 3 11:33 dir.tar -rw-r--r-- 1 root root 256M Oct 3 10:39 video.mp4 -rw-r--r-- 1 root root 257M Oct 3 11:34 video.tar > # 因此我們的打包 一般需要和壓縮一起進行使用 # # # # # # # # # # # # # # # # # # # # # # # # # #
# > 打包的同時,使用gzip 進行文件壓縮 -z(gzip) # > 完成後,文件不消失 # > tar -zcvf <打包壓縮後文件.tar.gz> <需要操作的文件目錄/文件> > ls -F dir/ video.mp4 > tar -zcvf dir.tar.gz dir/ dir/ dir/file2 dir/file1 > ls -F dir/ dir.tar.gz video.mp4 > tar -tf dir.tar.gz dir/ dir/file2 dir/file1 > # > 解壓 tar 包的文件 -x , 如需指定命令 同樣 -C /path/to/some_dir # > tar -zxvf <需要解壓的文件.tar.gz> > ls -F dir.tar.gz video.mp4 > tar -zxvf dir.tar.gz dir/ dir/file2 dir/file1 > ls -F dir/ dir.tar.gz video.mp4 > tree dir/ dir/ ├── file1 └── file2 0 directories, 2 files > # > 打包的同時,使用bzip2 進行文件壓縮 -j(bzip2) # > 完成後,文件不消失 # > tar -jcvf <打包壓縮後文件.tar.bz2> <需要操作的文件目錄/文件> > ls -F dir/ video.mp4 > tar -jcvf dir.tar.bz2 dir/ dir/ dir/file2 dir/file1 > tar -tf dir.tar.bz2 dir/ dir/file2 dir/file1 # > 解壓 tar 包的文件 -x , 如需指定命令 同樣 -C /path/to/some_dir # > tar -jxvf <需要解壓的文件.tar.bz2> > ls -F dir.tar.bz2 video.mp4 > tar -jxvf dir.tar.bz2 dir/ dir/file2 dir/file1 > ls -F dir/ dir.tar.bz2 video.mp4 > tree dir/ dir/ ├── file1 └── file2 0 directories, 2 files
- 同一文件使用不同壓縮工具的比較
# > 我們在這裏模擬創建一些文件, 使測試目錄的大小為1.2G > dd if=/dev/zero of=file_5M bs=1M count=5 5+0 records in 5+0 records out 5242880 bytes (5.2 MB) copied, 0.00330545 s, 1.6 GB/s > dd if=/dev/zero of=file_10M bs=1M count=10 10+0 records in 10+0 records out 10485760 bytes (10 MB) copied, 0.00722154 s, 1.5 GB/s > dd if=/dev/zero of=file_50M bs=1M count=50 50+0 records in 50+0 records out 52428800 bytes (52 MB) copied, 0.100913 s, 520 MB/s > dd if=/dev/zero of=file_100M bs=1M count=100 100+0 records in 100+0 records out 104857600 bytes (105 MB) copied, 0.205377 s, 511 MB/s > > dd if=/dev/zero of=file_1000M bs=1M count=1000 1000+0 records in 1000+0 records out 1048576000 bytes (1.0 GB) copied, 11.5899 s, 90.5 MB/s > > ls -lh total 1.2G -rw-r--r-- 1 root root 10K Oct 3 10:54 file1 -rw-r--r-- 1 root root 1000M Oct 3 12:17 file_1000M -rw-r--r-- 1 root root 100M Oct 3 12:17 file_100M -rw-r--r-- 1 root root 10M Oct 3 12:16 file_10M -rw-r--r-- 1 root root 16 Oct 2 23:21 file2 -rw-r--r-- 1 root root 50M Oct 3 12:17 file_50M -rw-r--r-- 1 root root 5.0M Oct 3 12:16 file_5M > > # 先使用 zip 做測試 > time zip -r compress.zip dir/ adding: dir/ (stored 0%) adding: dir/file2 (stored 0%) adding: dir/file_5M (deflated 100%) adding: dir/file_50M (deflated 100%) adding: dir/file_100M (deflated 100%) adding: dir/file_10M (deflated 100%) adding: dir/file_1000M (deflated 100%) adding: dir/file1 (deflated 99%) real 0m16.237s user 0m3.588s sys 0m5.380s > # 接著是 gzip > time tar -zcvf compress.tar.gz dir/ dir/ dir/file2 dir/file_5M dir/file_50M dir/file_100M dir/file_10M dir/file_1000M dir/file1 real 0m16.242s user 0m7.764s sys 0m1.956s > # 最後是 bzip2 > time tar -jcvf compress.tar.bz2 dir/ dir/ dir/file2 dir/file_5M dir/file_50M dir/file_100M dir/file_10M dir/file_1000M dir/file1 real 0m28.010s user 0m16.940s sys 0m2.137s
> # 我們再查看各個壓縮文件的大小 > ls -l compress.* -rw-r--r-- 1 root root 1434 Oct 3 12:23 compress.tar.bz2 -rw-r--r-- 1 root root 1186140 Oct 3 12:21 compress.tar.gz -rw-r--r-- 1 root root 1186932 Oct 3 12:20 compress.zip > ls -lh compress.* -rw-r--r-- 1 root root 1.5K Oct 3 12:23 compress.tar.bz2 -rw-r--r-- 1 root root 1.2M Oct 3 12:21 compress.tar.gz -rw-r--r-- 1 root root 1.2M Oct 3 12:20 compress.zip為了讓上面的信息更加明朗, 我們使用列表進行一個統計
壓縮/打包壓縮命令 | 壓縮的工具(版本號) | 所花費的時間 | 文件的大小 |
zip -r compress.zip dir/ | zip (3.0) | 16.237s | 1186932(1.2M) |
tar -zcvf compress.tar.gz dir/ | gzip (1.3.12) | 16.242s | 1186140(1.2M) |
tar -jcvf compress.tar.bz2 dir/ | bzip2 (1.0.5) | 28.010s | 1434 (1.5K) |
測試系統 2.6.32-696.6.3.e16.i686 / CentOS release 6.9(Final) |
- 對zip gzip bzip2 信息總結
命令工具 | 壓縮文件公式 | 壓縮後原文件是否保留 (保留命令公式) | 解壓文件公式 | 解壓後原文件是否保留 (保留命令公式) | 查看壓縮文件內文件 |
zip | zip2 [file_need_compress] | 保留 | unzip [file_suffix.tar] | 保留 | unzip -v [file_suffix.tar] |
gzip | gzip [file_need_compress] | 不保留 gzip -c [file_need_compress] > [file_suffix.gz] | gzip -d [file_suffix.gz] | 不保留 gzip -cd [file_suffix.gz] > [file_name] | gzip -l [file_suffix.gz] |
bzip2 | bzip2 [file_need_compress] | 不保留 bzip2 -k [file_need_compress] | bzip2 -d [file_suffix.bz2] | 不保留 bzip2 -dk [file_suffix.bz2] | bzcat [file_suffix.bz2] 可查看文件的內容 |
命令工具 | 壓縮目錄 | 解壓文件 | 查看文件 |
gzip | tar -zcvf [file_suffix.tar.gz] [file_need_compress&archive] | tar -zxvf [file_suffix.tar.gz] | tar -tf [file_suffix.tar.gz] |
bzip2 | tar -jcvf [file_suffix.tar.bz2] [file_need_compress&archive] | tar -zxvf [file_suffix.tar.bz2] | tar -tf [file_suffix.tar.bz2] |
[拾 得] zip gzip bzip2 & tar 壓縮/打包 四大金剛