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

 

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

 

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

 

 

Library Level Compressed size Compression Mbyte/s Decompression Mbyte/s
QuickLZ C 1.5.0 1 47.9% 308 358
QuickLZ C 1.4.0 1 47.9% 272 332
QuickLZ C 1.4.0 2 42.3% 131 309
QuickLZ C 1.4.0 3 40.0% 31 516
QuickLZ C# 1.4.0 1 47.9% 133 132
QuickLZ Java 1.4.0 1 47.9% 127 95
LZF 3.1 UF 54.9% 204 396
LZF 3.1 VF 51.9% 193 384
FastLZ 0.1.0 1 53.0% 173 442
FastLZ 0.1.0 2 50.7% 167 406
LZO 1X 2.02 1 48.3% 169 434
zlib 1.22 1 37.6% 55 234

 

 

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

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

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

 

#include <zlib.h> #include <stdio.h> #include <string.h> #include <assert.h> #include <zconf.h> #define CHUNK 16384 int def(FILE *source, FILE *dest, int level) { int ret, flush; unsigned have; z_stream strm; char in[CHUNK]; char out[CHUNK]; /* allocate deflate state */ strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; ret = deflateInit(&strm, level); if (ret != Z_OK) return ret; /* compress until end of file */ do { strm.avail_in = fread(in, 1, CHUNK, source); if (ferror(source)) { (void)deflateEnd(&strm); return Z_ERRNO; } flush = feof(source) ? Z_FINISH : Z_NO_FLUSH; strm.next_in = (Bytef*)in; /* run deflate() on input until output buffer not full, finish compression if all of source has been read in */ do { strm.avail_out = CHUNK; strm.next_out = (Bytef*)out; ret = deflate(&strm, flush); /* no bad return value */ assert(ret != Z_STREAM_ERROR); /* state not clobbered */ have = CHUNK - strm.avail_out; if (fwrite(out, 1, have, dest) != have || ferror(dest)) { (void)deflateEnd(&strm); return Z_ERRNO; } } while (strm.avail_out == 0); assert(strm.avail_in == 0); /* all input will be used */ /* done when last data in file processed */ } while (flush != Z_FINISH); assert(ret == Z_STREAM_END); /* stream will be complete */ /* clean up and return */ (void)deflateEnd(&strm); return Z_OK; } /* Decompress from file source to file dest until stream ends or EOF. inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be allocated for processing, Z_DATA_ERROR if the deflate data is invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and the version of the library linked do not match, or Z_ERRNO if there is an error reading or writing the files. */ int inf(FILE *source, FILE *dest) { int ret; unsigned have; z_stream strm; char in[CHUNK]; char out[CHUNK]; /* allocate inflate state */ strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; strm.avail_in = 0; strm.next_in = Z_NULL; ret = inflateInit(&strm); if (ret != Z_OK) return ret; /* decompress until deflate stream ends or end of file */ do { strm.avail_in = fread(in, 1, CHUNK, source); if (ferror(source)) { (void)inflateEnd(&strm); return Z_ERRNO; } if (strm.avail_in == 0) break; strm.next_in = (Bytef*)in; /* run inflate() on input until output buffer not full */ do { strm.avail_out = CHUNK; strm.next_out = (Bytef*)out; ret = inflate(&strm, Z_NO_FLUSH); assert(ret != Z_STREAM_ERROR); /* state not clobbered */ switch (ret) { case Z_NEED_DICT: ret = Z_DATA_ERROR; /* and fall through */ case Z_DATA_ERROR: case Z_MEM_ERROR: (void)inflateEnd(&strm); return ret; } have = CHUNK - strm.avail_out; if (fwrite(out, 1, have, dest) != have || ferror(dest)) { (void)inflateEnd(&strm); return Z_ERRNO; } } while (strm.avail_out == 0); /* done when inflate() says it's done */ } while (ret != Z_STREAM_END); /* clean up and return */ (void)inflateEnd(&strm); return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR; } /* report a zlib or i/o error */ void zerr(int ret) { fputs("zpipe: ", stderr); switch (ret) { case Z_ERRNO: if (ferror(stdin)) fputs("error reading stdin/n", stderr); if (ferror(stdout)) fputs("error writing stdout/n", stderr); break; case Z_STREAM_ERROR: fputs("invalid compression level/n", stderr); break; case Z_DATA_ERROR: fputs("invalid or incomplete deflate data/n", stderr); break; case Z_MEM_ERROR: fputs("out of memory/n", stderr); break; case Z_VERSION_ERROR: fputs("zlib version mismatch!/n", stderr); } } int main(int argc, char **argv) { int ret; /* do compression if no arguments */ if (argc == 1) { //ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION); ret = def(stdin, stdout, Z_BEST_SPEED); if (ret != Z_OK) zerr(ret); return ret; } /* do decompression if -d specified */ else if (argc == 2 && strcmp(argv[1], "-d") == 0) { ret = inf(stdin, stdout); if (ret != Z_OK) zerr(ret); return ret; } /* otherwise, report usage */ else { fputs("zpipe usage: zpipe [-d] < source > dest/n", stderr); return 1; } }

 

在测试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/

 

 

 

 

 

 

 

 

你可能感兴趣的:(Stream,File,测试,null,input,compression)