• 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;
    }
  • 相关阅读:
    过滤器
    自定义指令
    Window setTimeout() 方法
    Window setInterval()方法
    ThingJS官方案例(五):物联网室内3D定位导航,上下楼怎么办?
    基于WebGL的虚拟太阳系漫游技术实现 ThingJS 科幻片
    ThingJS官方案例(四):快速应用3D场景下的模拟导航能力
    ThingJS官方示例(三):3D标记“Marker”跳跃、闪烁和发光动画效果
    ThingJS官方示例(二):利用电子标注判断物联网设备的位置
    ThingJS 官方示例(一):禁区告警的3D电子围栏可视化
  • 原文地址:https://www.cnblogs.com/rixiang/p/7838833.html
Copyright © 2020-2023  润新知