QuickLZ -- 一个号称世界压缩速度最快的压缩库

 

QuickLZ 是一个号称世界压缩速度最快的压缩库,并且也是个开源的压缩库,其遵守 GPL 1, 2 或 3协议。

 

在QuickLZ的官网上有个关于QuickLZ的测试:

 

 

Library Level Compressed size Compression Mbyte/s Decompression Mbyte/s
QuickLZ C 1.5.0147.9%308358
QuickLZ C 1.4.0147.9%272332
QuickLZ C 1.4.0242.3%131309
QuickLZ C 1.4.0340.0%31516
QuickLZ C# 1.4.0147.9%133132
QuickLZ Java 1.4.0147.9%12795
LZF 3.1UF54.9%204396
LZF 3.1VF51.9%193384
FastLZ 0.1.0153.0%173442
FastLZ 0.1.0250.7%167406
LZO 1X 2.02148.3%169434
zlib 1.22137.6%55234

 

 

在这里,我也对QuickLZ和zlib进行一个对比的测试,看看是不是真的那么快那么好。

QuickLZ有个前端程序叫qpress,可以在QuickLZ的官网上下载下来进行测试。而zlib没有前端程序,所以要自己写一个。

zlib 的测试程序可以根据zlib 的源码中的一个例子进行改写,如下:

 

  1. #include <zlib.h>   
  2. #include <stdio.h>   
  3. #include <string.h>   
  4. #include <assert.h>   
  5. #include <zconf.h>   
  6. #define CHUNK 16384   
  7. int def(FILE *source, FILE *dest, int level)  
  8. {  
  9.     int ret, flush;  
  10.     unsigned have;  
  11.     z_stream strm;  
  12.     char in[CHUNK];  
  13.     char out[CHUNK];  
  14.     /* allocate deflate state */  
  15.     strm.zalloc = Z_NULL;  
  16.     strm.zfree = Z_NULL;  
  17.     strm.opaque = Z_NULL;  
  18.     ret = deflateInit(&strm, level);  
  19.     if (ret != Z_OK)  
  20.         return ret;  
  21.     /* compress until end of file */  
  22.     do {  
  23.         strm.avail_in = fread(in, 1, CHUNK, source);  
  24.         if (ferror(source)) {  
  25.             (void)deflateEnd(&strm);  
  26.             return Z_ERRNO;  
  27.         }  
  28.         flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;  
  29.         strm.next_in = (Bytef*)in;  
  30.         /* run deflate() on input until output buffer not full, finish 
  31.            compression if all of source has been read in */  
  32.         do {  
  33.             strm.avail_out = CHUNK;  
  34.             strm.next_out = (Bytef*)out;  
  35.             ret = deflate(&strm, flush);    /* no bad return value */  
  36.             assert(ret != Z_STREAM_ERROR);  /* state not clobbered */  
  37.             have = CHUNK - strm.avail_out;  
  38.             if (fwrite(out, 1, have, dest) != have || ferror(dest)) {  
  39.                 (void)deflateEnd(&strm);  
  40.                 return Z_ERRNO;  
  41.             }  
  42.         } while (strm.avail_out == 0);  
  43.         assert(strm.avail_in == 0);     /* all input will be used */  
  44.         /* done when last data in file processed */  
  45.     } while (flush != Z_FINISH);  
  46.     assert(ret == Z_STREAM_END);        /* stream will be complete */  
  47.     /* clean up and return */  
  48.     (void)deflateEnd(&strm);  
  49.     return Z_OK;  
  50. }  
  51. /* Decompress from file source to file dest until stream ends or EOF. 
  52.    inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be 
  53.    allocated for processing, Z_DATA_ERROR if the deflate data is 
  54.    invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and 
  55.    the version of the library linked do not match, or Z_ERRNO if there 
  56.    is an error reading or writing the files. */  
  57. int inf(FILE *source, FILE *dest)  
  58. {  
  59.     int ret;  
  60.     unsigned have;  
  61.     z_stream strm;  
  62.     char in[CHUNK];  
  63.     char out[CHUNK];  
  64.     /* allocate inflate state */  
  65.     strm.zalloc = Z_NULL;  
  66.     strm.zfree = Z_NULL;  
  67.     strm.opaque = Z_NULL;  
  68.     strm.avail_in = 0;  
  69.     strm.next_in = Z_NULL;  
  70.     ret = inflateInit(&strm);  
  71.     if (ret != Z_OK)  
  72.         return ret;  
  73.     /* decompress until deflate stream ends or end of file */  
  74.     do {  
  75.         strm.avail_in = fread(in, 1, CHUNK, source);  
  76.         if (ferror(source)) {  
  77.             (void)inflateEnd(&strm);  
  78.             return Z_ERRNO;  
  79.         }  
  80.         if (strm.avail_in == 0)  
  81.             break;  
  82.         strm.next_in = (Bytef*)in;  
  83.         /* run inflate() on input until output buffer not full */  
  84.         do {  
  85.             strm.avail_out = CHUNK;  
  86.             strm.next_out = (Bytef*)out;  
  87.             ret = inflate(&strm, Z_NO_FLUSH);  
  88.             assert(ret != Z_STREAM_ERROR);  /* state not clobbered */  
  89.             switch (ret) {  
  90.             case Z_NEED_DICT:  
  91.                 ret = Z_DATA_ERROR;     /* and fall through */  
  92.             case Z_DATA_ERROR:  
  93.             case Z_MEM_ERROR:  
  94.                 (void)inflateEnd(&strm);  
  95.                 return ret;  
  96.             }  
  97.             have = CHUNK - strm.avail_out;  
  98.             if (fwrite(out, 1, have, dest) != have || ferror(dest)) {  
  99.                 (void)inflateEnd(&strm);  
  100.                 return Z_ERRNO;  
  101.             }  
  102.         } while (strm.avail_out == 0);  
  103.         /* done when inflate() says it's done */  
  104.     } while (ret != Z_STREAM_END);  
  105.     /* clean up and return */  
  106.     (void)inflateEnd(&strm);  
  107.     return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;  
  108. }  
  109. /* report a zlib or i/o error */  
  110. void zerr(int ret)  
  111. {  
  112.     fputs("zpipe: ", stderr);  
  113.     switch (ret) {  
  114.     case Z_ERRNO:  
  115.         if (ferror(stdin))  
  116.             fputs("error reading stdin/n", stderr);  
  117.         if (ferror(stdout))  
  118.             fputs("error writing stdout/n", stderr);  
  119.         break;  
  120.     case Z_STREAM_ERROR:  
  121.         fputs("invalid compression level/n", stderr);  
  122.         break;  
  123.     case Z_DATA_ERROR:  
  124.         fputs("invalid or incomplete deflate data/n", stderr);  
  125.         break;  
  126.     case Z_MEM_ERROR:  
  127.         fputs("out of memory/n", stderr);  
  128.         break;  
  129.     case Z_VERSION_ERROR:  
  130.         fputs("zlib version mismatch!/n", stderr);  
  131.     }  
  132. }  
  133. int main(int argc, char **argv)  
  134. {  
  135.     int ret;  
  136.     /* do compression if no arguments */  
  137.     if (argc == 1) {  
  138.         //ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION);   
  139.         ret = def(stdin, stdout, Z_BEST_SPEED);  
  140.         if (ret != Z_OK)  
  141.             zerr(ret);  
  142.         return ret;  
  143.     }  
  144.     /* do decompression if -d specified */  
  145.     else if (argc == 2 && strcmp(argv[1], "-d") == 0) {  
  146.         ret = inf(stdin, stdout);  
  147.         if (ret != Z_OK)  
  148.             zerr(ret);  
  149.         return ret;  
  150.     }  
  151.     /* otherwise, report usage */  
  152.     else {  
  153.         fputs("zpipe usage: zpipe [-d] < source > dest/n", stderr);  
  154.     return 1;  
  155.     }  
  156. }  

 

在测试zilib是采用了两个压缩方式,一个是最快的压缩(Z_BEST_SPEED)和一个是默认的压缩(Z_DEFAULT_COMPRESSION),在main函数中可以看到。

 

下面是我在linux下的ntfs分区中,对一个1G的有着fat32文件系统的文件(这个1G的文件是我在windows下用winhex对一个1G的fat32的分区进行拷贝得到的文件)进行压缩后的测试的结果:

QuickLZ采用了稳定的1.4.1版,zlib采用了稳定的1.2.3版。

 

                    quickLZ (最低压缩               zlib(默认压缩                          zlib (最低压缩率)
                    率)version 1.4.1                    率)version 1.2.3
time              Compressed                       real 2m5.507s                      real 1m23.828s
                  1,075,838,976 bytes             user 1m44.079s                 user 1m2.476s
                  in 1 file(s) into                      sys 0m3.164s                      sys 0m2.948s
                 598,244,013 bytes
                 real 0m29.478s
                 user 0m10.209s
                 sys 0m2.484s
压缩后大小        598244013(571M )           484103258(462M )            510911735(488M )

在上面的测试中,我们可以看到,quickLZ的压缩率要比zlib 的低,但压缩率还是可以的,而压缩的速度确实比zlib 的快了很多。quickLZ在最低压缩率的情况下压缩1G的文件,用了29.478秒,压缩到571M,而zlib在默认的压缩率下使用了2分5.507秒,压缩到462M,在zlib在最低压缩率下,也使用了1分23.828秒,压缩到488M。

 

看来QuickLZ的压缩速度确实并非浪得虚名。

 

QuickLZ的官网:http://www.quicklz.com/

zlib的官网:       http://www.zlib.net/

 

转载地址:http://blog.csdn.net/fjb2080/article/details/5355555

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值