Talking about compression in linux

 1. Compression uses and techniques

  1.1 Why compression is needed:

        ①Have you ever had a file that is too large to be sent out by normal email (many emails have a limit of about 25MB per letter!)?

        ②Have you ever wanted to back up some important data, but the amount of these data is too large, consuming a lot of disk space for you?

         。。。。。。

        At this time, the easy-to-use " file compression" technology can come in handy!

  1.2 Compression principle:

           We all know that 1 byte = 8 bits, and how does the computer achieve memory storage of file data?

    Suppose a byte can be seen as the right side ---------->       □□□□□□□□

    Since 1 byte = 8 bits, there will be 8 spaces in each byte, and each space can be 0, 1, here is just a brief introduction!

 

    Suppose to record the number "1", consider the so-called binary bit of the computer, so that 1 will occupy 1 bit on the far right, and the other 7 bits will be automatically filled with 0! Take a closer look, in fact, in such an example, the 7 bits should be "empty"! However, in order to meet the current access of our operating system data, we will convert the data to byte type to record! And some smart computer engineers use some complex calculation methods to "throw" out the unused space to make the space occupied by the files smaller! This is the technique of compression!

 

    To put it simply, you can think of it as, in fact, there is quite a lot of "space" in the file, which is not completely filled, and the "compression" technology is to fill up these "spaces" so that the entire file occupies The capacity drops! However, these "compressed files" cannot be directly used by our operating's called " decompression "! As for the size of the disk space occupied by the compressed and compressed files, it can be called the " compression ratio "

2. Common compression instructions in Linux systems

  2.1 Instruction introduction:

      In the Linux environment, the file extensions of compressed files are mostly: " *.tar, *.tar.gz, *.tgz, *.gz, *.Z, *.bz2, *.xz "

           The common compression instructions on Linux are gzip, bzip2 and the latest xz. As for compress, it is out of fashion. In order to support the common zip in windows, in fact, Linux has already had zip instructions.

    However, these commands usually only compress and decompress one file, so wouldn't it be annoying to have to compress and decompress a bunch of files each time? At this point, the so-called "package software, tar" is very important! The function of packaging is to pack the multiple files you specify into one file, and it does not have the effect of compression, so that the package files can be compressed uniformly, and there is no need to compress the files one by one.

  2.2 Compression instructions

    Description of gzip command parameters:

    Options and parameters: 
      -c : Output compressed data to the screen, which can be processed through data stream redirection; 
      -d : Decompression parameters; 
      -t : It can be used to check the consistency of a compressed file~ have a look Whether the file has errors; 
      -v : It can display information such as the compression ratio of the original file/compressed file; 
      -# : # is the meaning of the number, representing the compression level, -1 is the fastest, but the compression ratio is the worst, -9 is the slowest , but the compression ratio is the best! Default is -6 

2.3 gzip command case : 2.3.1 find the largest file in /etc/ directory ls -lraS /etc/ | tail -n 10 2.3.2 and copy services to /tmp
      
          

      
cp /etc/services .

      2.3.3 Compress services in /tmp with  gzip -v services Description: After compression, the services.gz file is obtained, and the original file does not exist
                

            2.3.4 Comparing compressed and uncompressed files                ll /etc/services /tmp/services*

           

      2.3.5     Since the original file of services is a text file, we can try to use zcat/zmore/zless to read it! zmore services.gz

          

 

      2.3.6 Decompress services.gz in /tmp gzip -dv services.gz         Description: The services.gz file will be deleted after decompression

 

          

 

      2.3.7 Compress the unpacked services with the best compression ratio and keep the original file       gzip -9 -cv services > services.gz

         Compression level description: gzip provides compression levels from 1 to 9, and the compression strength increases in turn

          

      

      2.3.8       In the re-created services.gz, find out which lines the keyword http is in?      zgrep -n 'http' services.gz

         

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Foreword:

  If gzip was created to replace compress and provide better compression ratios, then bzip2 was created to replace gzip and provide better compression ratios. bzip2 is really a good thing to use ~ the compression ratio of this thing is even better than gzip ~ as for the usage of bzip2 is almost the same as gzip! Check out the usage below!

 

2.4 bzip2案 例(bzip2, bzcat/bzmore/bzless/bzgrep

    2.4.1 Description of bzip2 command parameter options  

  选项与参数:
  -c :将压缩的过程产生的资料输出到萤幕上!
  -d :解压缩的参数
  -k :保留原始档案,而不会删除原始的档案喔!
  -z :压缩的参数(预设值,可以不加)
  -v :可以显示出原档案/压缩档案的压缩比等资讯;
  -# :与gzip 同样的,都是在计算压缩比的参数, -9 最佳, -1 最快!

    2.4.2
将刚刚gzip范例留下来的/tmp/services以bzip2压缩 bzip2 -v services
      
    
    2.4.3 此时你会发现bzip2比gzip指令压缩强度要好 ls -l services*
      


    2.4.4 读取范例/tmp/services.bz2 文件 bzcat services.bz2

    2.4.5 将范例中/tmp/services.bz2文件解压缩 bzip2 -d services.bz2
        

    2.4.6 解开的services用最佳的压缩比压缩,并保留原本的档案 bzip2 -9 -c services > services.bz2
      
    
    说明:
     看上面的范例,你会发现到bzip2 连选项与参数都跟gzip 一模一样!只是副档名由.gz 变成.bz2 而已!其他的用法都大同小异,所以就不一一介绍了!你也可以发现到bzip2 的压缩率确实比gzip 要好些!不过,对于大容量档案来说,bzip2 压缩时间会花比较久喔!至少比gzip 要久的多!这没办法~要有更多可用容量,就得要花费相对应的时间!还OK 啊!

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

前言:

虽然bzip2 已经具有很棒的压缩比,不过显然某些自由软体开发者还不满足,因此后来还推出了xz 这个压缩比更高的软体!这个软体的用法也跟gzip/bzip2 几乎一模一样!请看下面案例!


2.5 xz案例( xzcat/xzmore/xzless/xzgrep
 
 2.5.1 xz选项和说明
    
选项与参数:
    -d :就是解压缩啊!
    -t :测试压缩档的完整性,看有没有错误
    -l :列出压缩档的相关资讯
    -k :保留原本的档案不删除~
    -c :同样的,就是将资料由萤幕上输出的意思!
    -# :同样的,也有较佳的压缩比的意思!

  2.5.2 将刚刚由bzip2所遗留下来的/tmp/services透过xz来压缩       xz -v services  (压缩比例以下效果可见,容量又进一步下降的更多)
      


  2.5.3
列出这个压缩档的资讯,然后读出这个压缩档的内容 xz -l services.xz

  2.5.4 查看压缩后的压缩档内容   xzcat services.xz(指令都很相似,就不一一截图了)
  
  2.5.5 解压缩
xz -d services.xz

  2.5.6
保留原档案的档名,并且建立压缩档! xz -k services



压缩指令总结:
以下是一组时间数据:
『 time [gzip|bzip2|xz] -c services > services.[gz|bz2|xz] 』去执行运算结果,结果发现这三个指令的执行时间依序是: 0.019 s, 0.042s, 0.261s, 看最后一个数字!差了10 倍的时间

通过案例我们发现压缩比例越高时间就越久,虽然xz压缩强度比gizp要高很多,但是xz花的时间实在是太久了,所以如果你不觉得时间成本是你的考量那么使用xz会更好,如果时间是你的重要成本考量,恐怕gzip是比较适合的压缩软体!


如有疑问请说明!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325310239&siteId=291194637