zoukankan      html  css  js  c++  java
  • 压缩工具gzip、bzip2、xz的使用

    本文使用

    • 为了要压缩
    • 常见压缩格式
    • 压缩工具
      • gzip压缩工具
      • bz2压缩工具
      • xz压缩工具

    为什么要压缩

    为什么要压缩?文件经过压缩后,其大小会缩小,可以节约服务器带宽资源、内存资源,节约运维成本!

    常见压缩格式

    Linux平台的常见压缩文件格式有以下几种

    • .zip
    • .gz
    • .bz2
    • .xz
    • .tar.gz
    • .tar.bz2
    • .tar.xz

    虽然在Linux中文件后缀并不重要,但是为了便于区分和管理,压缩文件还是最好加上上述的一些后缀名。

    压缩工具

    gzip压缩工具

    gzip不能压缩目录!!

    • 基本使用:压缩/解压缩
    # 创建一个大文件
    [root@localhost tmp]# find /etc/ -type f -exec cat {} >> 1.txt ;
    [root@localhost tmp]# ls -lh 
    总用量 25M
    -rw-r--r--. 1 root root 25M 11月  3 18:53 1.txt
    
    #压缩文件:gzip file
    [root@localhost tmp]# gzip 1.txt 
    [root@localhost tmp]# ls -lh
    总用量 9.3M
    -rw-r--r--. 1 root root 9.3M 11月  3 18:53 1.txt.gz
    
    
    #解压缩文件: gzip -d file.gz
    # gzip -d 等价于 gunzip
    [root@localhost tmp]# gzip -d 1.txt.gz 
    [root@localhost tmp]# ls -lh
    总用量 25M
    -rw-r--r--. 1 root root 25M 11月  3 18:53 1.txt
    
    • 按指定压缩级别进行压缩
    #以1级别进行压缩,默认压缩级别为9;
    # 压缩级别1-9,压缩级别越高,占用CPU资源越高
    # gzip -[1-9] file
    [root@localhost tmp]# gzip -1 1.txt 
    [root@localhost tmp]# ls -lh
    总用量 9.9M
    -rw-r--r--. 1 root root 9.9M 11月  3 18:53 1.txt.gz
    
    • 在压缩的同时,保留原文件(默认不保留)
    [root@localhost tmp]# gzip -c 1.txt > /test/tmp/1.txt.gz
    [root@localhost tmp]# ls -lh
    总用量 34M
    -rw-r--r--. 1 root root  25M 11月  3 18:53 1.txt
    -rw-r--r--. 1 root root 9.3M 11月  3 19:04 1.txt.gz
    
    
    • -c配合-d使用,将压缩文件解压缩的同时保留压缩包
    # 注意最好使用绝对路径表示文件,清楚明白,当然相对路径表示也可以。
    [[root@localhost tmp]# gzip -d -c /test/tmp/1.txt.gz > /test/tmp/2.txt
    
    [root@localhost tmp]# ls -l
    总用量 34684
    -rw-r--r--. 1 root root  9649736 11月  3 19:04 1.txt.gz
    -rw-r--r--. 1 root root 25865478 11月  3 19:22 2.txt
    
    • zcat命令在不打开压缩包的情况下查看包内文件的内容
    # 由于内容太多,这里只显示行数
    [root@localhost tmp]# zcat 1.txt.gz | wc -l
    176350
    

    可以使用file命令查看压缩文件的具体信息!

    [root@localhost tmp]# file 1.txt.gz 
    1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Fri Nov  3 18:53:55 2017, max speed
    
    

    有些压缩文件被命名为不带压缩文件类型后缀的,这种文件无法解压,可以使用file来查看文件类型,然后修改后缀,更好区分!!

    [root@centos7 test]# mv 1.txt.bz2 1.txt
    [root@centos7 test]# file 1.txt 
    1.txt: bzip2 compressed data, block size = 900k
    [root@centos7 test]# mv 1.txt 1.txt.bz2
    

    bz2压缩工具

    压缩率比gzip更高

    未安装使用yum install -y bzip2

    常用使用跟gzip命令类似,且同样不支持压缩目录

    • 基本用法
    # 压缩前
    [root@localhost tmp]# ls -lh
    总用量 25M
    -rw-r--r--. 1 root root 25M 11月  3 19:22 1.txt
    
    # bzip2压缩
    [root@localhost tmp]# bzip2 1.txt 
    # 压缩后,比gzip压缩率高
    [root@localhost tmp]# ls -lh
    总用量 8.2M
    -rw-r--r--. 1 root root 8.1M 11月  3 19:22 1.txt.bz2
    # file命令查看文件格式
    [root@localhost tmp]# file 1.txt.bz2 
    1.txt.bz2: bzip2 compressed data, block size = 900k
    
    
    # bzip2解压缩
    [root@localhost tmp]# bzip2 -d 1.txt.bz2 
    [root@localhost tmp]# ls -lh
    总用量 25M
    -rw-r--r--. 1 root root 25M 11月  3 19:22 1.txt
    
    
    • -c参数在压缩同时保留原文件
    [root@localhost tmp]# bzip2 -c 1.txt > 1.txt.bz2
    [root@localhost tmp]# ls -lh
    总用量 33M
    -rw-r--r--. 1 root root  25M 11月  3 19:22 1.txt
    -rw-r--r--. 1 root root 8.1M 11月  3 20:04 1.txt.bz2
    
    • -d -c参数:在解压同时保留原压缩文件
    [root@localhost tmp]# bzip2 -d -c 1.txt.bz2 > 3.txt
    [root@localhost tmp]# ls -l
    总用量 58816
    -rw-r--r--. 1 root root 25865478 11月  3 19:22 1.txt
    -rw-r--r--. 1 root root  8491810 11月  3 20:04 1.txt.bz2
    -rw-r--r--. 1 root root 25865478 11月  3 20:06 3.txt
    
    • 指定压缩级压缩(一般保存默认即可)
    bzip2同样可以指定压缩级别:1-9,默认级别为6
    [root@localhost tmp]# bzip2 -1 1.txt 
    [root@localhost tmp]# ls -lh
    总用量 8.8M
    -rw-r--r--. 1 root root 8.8M 11月  3 19:22 1.txt.bz2
    
    • 不解压情况下查看压缩文件内文件的内容
    [root@localhost tmp]# bzcat 1.txt.bz2 | wc -l
    176350
    

    xz压缩工具

    级别使用方法等同与上述的gzip和bzip2命令,同样也不能压缩目录!!

    一般情况下,上述3种压缩方式:xz > bz2 > gzip

    具体压缩要看文件内容,也不一定xz的压缩包最小

    • 基本使用
    # 压缩
    [root@localhost tmp]# xz 1.txt 
    [root@localhost tmp]# ls -lh
    总用量 7.0M
    -rw-r--r--. 1 root root 7.0M 11月  3 19:22 1.txt.xz
    
    # 解压缩 xz -d等价于unxz
    [root@localhost tmp]# xz -d 1.txt.xz 
    [root@localhost tmp]# ls -lh
    总用量 25M
    -rw-r--r--. 1 root root 25M 11月  3 19:22 1.txt
    
    
    • 压缩文件同时保留原文件
    [root@localhost tmp]# xz -c 1.txt > 1.txt.xz
    [root@localhost tmp]# ls -lh
    总用量 32M
    -rw-r--r--. 1 root root  25M 11月  3 19:22 1.txt
    -rw-r--r--. 1 root root 7.0M 11月  3 21:02 1.txt.xz
    
    • 解压同时保留压缩文件
    [root@localhost tmp]# xz -d -c 1.txt.xz > ./2.txt
    [root@localhost tmp]# ls -lh
    总用量 57M
    -rw-r--r--. 1 root root  25M 11月  3 19:22 1.txt
    -rw-r--r--. 1 root root 7.0M 11月  3 21:02 1.txt.xz
    -rw-r--r--. 1 root root  25M 11月  3 21:02 2.txt
    
    • 查看压缩文件内的文件内容(不解压前提下)
    [root@localhost tmp]# xzcat 1.txt.xz | wc -l
    176350
    
    • xz同样支持指导压缩级别的压缩
    [root@localhost tmp]# xz -1 2.txt 
    [root@localhost tmp]# ls -lh
    总用量 40M
    -rw-r--r--. 1 root root  25M 11月  3 19:22 1.txt
    -rw-r--r--. 1 root root 7.0M 11月  3 21:02 1.txt.xz
    -rw-r--r--. 1 root root 7.5M 11月  3 21:02 2.txt.xz
    
    

    转载于:https://my.oschina.net/LuCastiel/blog/1560663

  • 相关阅读:
    Effective Java:Ch4_Class:Item14_在public类中应该使用访问方法而不是public域
    [置顶] 学习JDK源码:可进一步优化的代码
    [置顶] 学习JDK源码:编程习惯和设计模式
    如何开展软件架构之概念架构
    POJ 3667 & 1823 Hotel (线段树区间合并)
    CF 161D Distance in Tree【树DP】
    BroadcastReceiver基础总结
    OSI七层模型具体解释
    习惯的力量之四理直气壮的借口?
    堆(stack) 之 c 和 c++模板实现(空类默认成员函数 初谈引用 内联函数)
  • 原文地址:https://www.cnblogs.com/twodog/p/12139029.html
Copyright © 2011-2022 走看看