zoukankan      html  css  js  c++  java
  • 压缩(zip)

      默认情况这些压缩工具在压缩后会删除源文件(zip除外);而且默认只压缩文件,而不压缩目录(链接到打包程序)。

      1. gzip

      2. bzip2

      3. zip

      4. GNU tar

       

      • 1.gzip


      1.1.压缩

        gzip 压缩工具压缩一个普通文件扩展名为“.gz”。压缩后删除了原文件。

      $ gzip user.sh

        可以一次压缩多个文件。压缩时每个文件独立压缩。

      $ gzip *

      1.2.解压

        解压缩;同样删除原文件。

      $ gunzip user.sh.gz

        其实,解压缩使用的命令是个脚本。

      #!/bin/sh
      PATH=${GZIP_BINDIR-'/bin'}:$PATH
      exec gzip -d "$@"

      1.3.测试压缩文件完整性

      $ gzip -t hello
      gzip: hello: unexpected end of file

      1.4.查看压缩文件内容

      $ zcat hello.gz
      • 2.bzip2/xz


      2.1.压缩

        压缩后扩展名“.bz2”,同样会删除原文件。

      $ bzip2 *

        使用星号时,会压缩多个文件,每个文件独立压缩。

      2.2.解压

      $ bunzip2 *
      $ bzip2 -d *

      2.3.保留原文件

      $ bzip2 -k hello

        不支持使用星号

      2.4.查看压缩文件内容

      $ bzcat hello.bz2
      • 3.zip


        压缩时指定压缩包名称为“foo”,最后生成压缩包名为“foo.zip”。追加成员,无须特定选项。
        压缩时,运行在unix环境下,支持正在表达式匹配:?、*、[]。分别代表匹配单个字符、匹配任意数量字符(包括空)、其中之一匹配。

      3.1.压缩目录

        可以直接压缩目录

      $ zip mash.zip mash/

        这样确实可以直接压缩目录,但是如果目录“mash”下全是目录文件,那么压缩包里就是空的。因为之压缩了目录(确实是压缩了)。

        压缩目录 "zip-dir" 下的所有文件(包含其中子目录及子目录中文件),解压后还有目录 “zip-dir”。(选项带“-r”、末尾带星号“*”)

      $ zip -r zip-dir.zip zip-dir/*

      3.2.压缩文件

      压缩当前目录下的所有文件和目录(不包含当前目录子目录中的文件)。

      $ zip zip-dir *

      3.3.指定压缩包位置

      指定到其它目录下。

      $ zip /test/zip-dir *

      3.4.压缩后删除原始文件

      创建压缩文件“all.zip”的同时删除原始文件“all”。

      $ cd zip-dir
      $ mkdir all
      $ zip -rm all all

      追加当前目录下的文件到压缩文件;并删除原始文件。

      $ zip -rm all *

      3.5.为压缩包设置密码

        给压缩包加密,使用选项“-e”。

      $ zip -r -e bin.zip bin/*
      Enter password: 
      Verify password: 
        adding: bin/zcw_bak4release-3.2.sh (deflated 57%)
        adding: bin/zcw_mkdir4bak-2.2.sh (deflated 52%)
        adding: bin/zcw_replace4release-2.2.sh (deflated 58%)
        adding: bin/zcw_Virtualfile-1.3.sh (deflated 56%)

        可以直接在命令行指定加密密码,使用大写“-P”:

      # zip -r -e -P hello bin.zip bin/*

      3.6.显示压缩了多少文件

      在压缩完成后显示压缩了多少文件。

      $ zip -dc bin.zip bin/*
        0/  4 updating: bin/zcw_bak4release-3.2.sh (deflated 57%)
        1/  3 updating: bin/zcw_mkdir4bak-2.2.sh (deflated 52%)
        2/  2 updating: bin/zcw_replace4release-2.2.sh (deflated 58%)
        3/  1 updating: bin/zcw_Virtualfile-1.3.sh (deflated 56%)

      3.7.压缩后去掉目录

        仅压缩指定目录下的文件而不包含其中子目录,也不包含当前目录(zip-dir)。(解压后只有文件不见目录)

      $ zip -j zip-dir zip-dir/*

        实例:获取tomcat的日志(压缩后路径较长,使用不便):

      [root@right mag]# zip -j catalina.zip /home/work/tomcat4file/logs/catalina.out 
        adding: catalina.out (deflated 93%)

      3.8.改变压缩输入方式(压缩文件来自管道)

      $ find /etc/sysconfig/network-scripts -name "ifcfg*" -print | zip ifcfg -@

      3.9.追加文件到压缩包

        追加文件到压缩包,啥都不要直接操作。

      [root@right mag]# zip back 1.txt
        adding: 1.txt (stored 0%)
      [root@right mag]# zip back 2.conf
        adding: 2.conf (stored 0%)
      [root@right mag]# zip back 3.xml 4.html 5.sql
        adding: 3.xml (stored 0%)
        adding: 4.html (stored 0%)
        adding: 5.sql (stored 0%)
      [root@right mag]# unzip -l back
      Archive:  back.zip
        Length      Date    Time    Name
      ---------  ---------- -----   ----
              0  01-11-2014 15:55   1.txt
              0  01-11-2014 15:55   2.conf
              0  01-11-2014 15:55   3.xml
              0  01-11-2014 15:55   4.html
              0  01-11-2014 15:55   5.sql
      ---------                     -------
              0                     5 files

      3.10.压缩指定文件

        通过选项“-i”、“--include”获取指定格式的文件:

      zip -r foo . -i *.c

        只压缩某些日志,实例:

      [root@right mag]# zip -r lz.zip /home -i *.out
        adding: home/work/tomcat4file/logs/catalina.out (deflated 93%)
      [root@right mag]# unzip -l lz.zip 
      Archive:  lz.zip
        Length      Date    Time    Name
      ---------  ---------- -----   ----
      132020334  01-11-2014 15:09   home/work/tomcat4file/logs/catalina.out
      ---------                     -------
      132020334                     1 file

        研发常常会找运维人员要文件:

      [root@iZ28srao5ssZ mag]# zip -rj config.zip /home/work -i "*config.properties"
        adding: config.properties (deflated 63%)

      3.11.压缩时跳过指定文件

        指定选项“-x”、“--exclude”。
        跳过手机客户程序:

      # zip -r 160.zip /home/work/release/* -x *.apk

      3.12.删除压缩文件中某个指定文件

        指定名称名,使用选项“-d”删除。

      [root@iZ28srao5ssZ mag]# zip -d config.zip home/work*
      deleting: home/work/release/caiBao/WEB-INF/classes/config.properties
      [work@app47 .donatello]$ zip -d 10_zcw_release-3.0.1.zip _re*
      deleting: _replace4release-3.2.sh

      3.13.检查文件

        选项“-T”

      # zip -T cat-47.zip 
      test of cat-47.zip OK
      • unzip

      3.1.测试压缩文件

        检查压缩后有没有错误产生

      $ unzip -t bin
      Archive:  bin.zip
          testing: bin/                     OK
          testing: bin/zcw_bak4release-3.2.sh   OK
          testing: bin/zcw_Virtualfile-1.3.sh   OK
      No errors detected in compressed data of bin.zip.
      $ unzip -tq bin
      No errors detected in compressed data of bin.zip.

      3.2.列出压缩包中的文件

      $ unzip -l zip-dir.zip

        详细列出压缩包中的文件信息

      $ unzip -v zip-dir.zip

      3.3.解压所有文件  

        提取zcw.zip中所有文件

      $ unzip zcw
      Archive:  zcw.zip
        inflating: zcw_replace4release-2.3.sh  
        inflating: zcw_Virtualfile-1.3.sh

        解压时,去掉目录结构。

      $ unzip -j bin

        解压指定的某个文件

      $ unzip bin *zcw_V*.sh
      Archive:  bin.zip
        inflating: bin/zcw_Virtualfile-1.3.sh
      • FAQ: 解压后的位置

      $ zip base.repo.zip /etc/yum.repos.d/CentOS-Base.repo
      $ unzip -l base.repo.zip
      Archive:  base.repo.zip
        Length      Date    Time    Name
      ---------  ---------- -----   ----
           1926  12-28-2014 14:22   etc/yum.repos.d/CentOS-Base.repo
      ---------                     -------
           1926                     1 file

      即:

      $ cd /data/Test
      $ unzip /root/base.repo.zip
      $ ls -R etc/
      wKiom1SiZtjDnZJyAABF2UK-zJg639.jpg
      $ ls /data/Test/etc/yum.repos.d/CentOS-Base.repo
      /data/Test/etc/yum.repos.d/CentOS-Base.repo

      3.4.指定解压目录

        默认解压所有文件、子目录到当前目录下,使用选项“-d”可以指定目录。

      # unzip caiBao.zip -d /home/work/payment_front_pro/release/

      主要操作模式

        -cf  归档打包

        -tf  查看归档包(效果类似于“ls”的显示样式,but不是以空白分割,而是换行分割)

        -tvf  查看归档包(效果类似于“ls -l”的显示样式)

        -xf  归档解包

      通用选项

        -C  指定解压目录

        -v  详细列出已处理的文件

      $ tar -cf demo.tar demo/
      $ tar -tf demo.tar
      ……
      
       
      
      $ tar -xf nari.tar

      4.1.压缩

      $ tar -zcvf 2022.tar.gz 2022

        压缩时带上时间信息

      $ tar -zcf etc_$(date +%F).tar.gz /etc
      $ tar -jcf root_$(date +%Y.%m.%d-%H%M%S).tar.bz2 /root
      $ tar -jcf root_$(date +%y.%m.%d-%H%M%S).tar.bz2 /root

       

      4.2.解压缩

      $ tar -zxvf 2022.tar.gz
      $ tar -zxvf 2022.tgz
      $ tar -jxvf 2022.tar.bz2
      $ tar -Jxvf 2022.tar.Z
      $ tar -xf 2022.tar.gz

        默认还原到原始打包的路径下;可以使用选项“-C”指定解压后的目录。

      $ tar -zxf /opt/etc.tar.gz
      $ tar -zxf /opt/etc.tar.gz -C /root

      4.3.查看压缩包文件结构

      查看压缩文件列表

      $ tar -tf etc.tar.gz

      详细查看压缩文件列表

      $ tar -tvf etc.tar.gz

      4.4.打包时跳过某些文件

      选项“-X”、“--exclude”

      $ tar --exclude=/data/tomcat_cb/logs/* -zcvf  tom.tgz /data/tomcat_cb/
      一切代码都是为了生活,一切生活都是调剂
    1. 相关阅读:
      npm 安装卸载模块 & ionic插件安装与卸载
      Vue中v-model解析、sync修饰符解析
      Vue props用法详解
      vue页面跳转
      Swift 4 中的泛型
      Swift枚举的全用法
      蓝牙 BLE 三种 UUID 格式转换
      SVG图案
      SVG渐变
      SVG坐标系统及图形变换
    2. 原文地址:https://www.cnblogs.com/argor/p/7908179.html
    Copyright © 2011-2022 走看看