zoukankan      html  css  js  c++  java
  • use zlib lib to compress or decompress file

    If you want to compress or decompress file when writing C++ code,you can choose zlib library,that's quite easy.

    1,Download zlib library's code form website:http://zlib.net/

    2,Decompress the file you've downloaded,and then go to the directory "zlib-1.2.11zlib-1.2.11contribvstudiovc14", open the zlibvc.sln in it with your vs.

    3,Now you have opened the project of zlib,you need to compile it next.rebuild the project named "zlibvc",there are errors:

    1>  The system cannot find the path specified.

    1>  'bld_ml64.bat' is not recognized as an internal or external command,

    1>  operable program or batch file.

    1>C:Program Files (x86)MSBuildMicrosoft.Cppv4.0V140Microsoft.CppCommon.targets(123,5): error MSB3073: The command "cd ....contribmasmx64

    1>C:Program Files (x86)MSBuildMicrosoft.Cppv4.0V140Microsoft.CppCommon.targets(123,5): error MSB3073: bld_ml64.bat

    1>C:Program Files (x86)MSBuildMicrosoft.Cppv4.0V140Microsoft.CppCommon.targets(123,5): error MSB3073: :VCEnd" exited with code 9009.

    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    4,you need to reset the Properties of the project,select Build Event,then select Pre-Build Event,then delete the content of Command Line on it.rewrite the command line with your zlib's path.for example:

    C:
    cd C:UserssonneDesktopzlib-1.2.11zlib-1.2.11contribmasmx64
    bld_ml64.bat

    so 'C:UserssonneDesktopzlib-1.2.11' is the path where I put my zlib downloaded form website before.

    5,compile again,rebuild your project,now succeed.

    After compiling zlib library,now need to use the dll.

    1,put the zlibwapi.dll and zlibwapi.lib in your project.

    2,you also need to include the zlib.h and zconf.h.

    3,add a line in cpp file:

        #pragma comment(lib,"zlibwapi.lib")

    4,now you can use zlib APIs.

    here is zlib usage examples: https://zlib.net/zlib_how.html

    the manual is also useful: https://zlib.net/manual.html

    5, my test code here,shows how to compress and decompress by zlib:

    #include <stdio.h>
    #include <string.h>
    #include <assert.h>
    #include "zlib.h"
    
    #define CHUNK 16384
    #pragma comment(lib,"zlibwapi.lib")
    
    int def(FILE *source, FILE *dest, int level)
    {
        int ret, flush;
        unsigned have;
        z_stream strm;
        unsigned char in[CHUNK];
        unsigned 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 = 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 = 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;
    }
    
    int inf(FILE *source, FILE *dest)
    {
        int ret;
        unsigned have;
        z_stream strm;
        unsigned char in[CHUNK];
        unsigned 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 = in;
            /* run inflate() on input until output buffer not full */
            do {
                strm.avail_out = CHUNK;
                strm.next_out = 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;
    }
    
    int main() {
        FILE       *fp_src; 
        FILE       *fp_dst;
    
        //compress a file
        //fp_src = fopen( "test.txt", "r" );  
        //fp_dst = fopen( "myzip.zip", "w" );  
        //int ret = def(fp_src,fp_dst,9);
    
        //decompress a file compressed
        fp_src = fopen( "myzip.zip", "r" );
        fp_dst  = fopen( "test2.txt", "w");
        int ret = inf(fp_src, fp_dst);
    
        return 0;
    }
  • 相关阅读:
    Linux下使用ps命令来查看Oracle相关的进程
    虚继承、虚基类
    C++/C小知识点(2)
    C++中的运行中动态类型识别RTTI
    什么应该放在头文件?何为外连接?
    Placement new、operator new、new operator 完全释疑
    二叉搜索树
    Linux C编程学习资料,学习过程
    独立对象(非附属对象)大小
    编程珠玑第一章、第二章
  • 原文地址:https://www.cnblogs.com/rixiang/p/7838833.html
Copyright © 2011-2022 走看看