1.VS2019工程配置:
$(ProjectDir);$(ProjectDir)zlib-1.2.13;$(ProjectDir)zlib-1.2.13\contrib\minizip;
2.工程添加项目文件:
3.使用代码案例:
#include <zip.h>
static int GZipCompress(unsigned char* dst, unsigned long* dstLen, const unsigned char* src, unsigned long srcLen)
{
int ret = Z_ERRNO;
z_stream stream = { 0 };
stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
stream.opaque = (voidpf)0;
ret = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS | 16, 8, Z_DEFAULT_STRATEGY);
if (ret != Z_OK)
{
return ret;
}
stream.next_in = (Bytef*)src;
stream.avail_in = srcLen;
stream.next_out = (Bytef*)dst;
stream.avail_out = (*dstLen);
while ((stream.avail_in > 0) && (stream.total_out < (*dstLen)))
{
ret = deflate(&stream, Z_NO_FLUSH);
if (ret != Z_OK)
{
return ret;
}
}
if (stream.avail_in > 0)
{
return Z_DATA_ERROR;
}
for (; ((ret = deflate(&stream, Z_FINISH)) != Z_STREAM_END);)
{
if (ret != Z_OK)
{
return ret;
}
}
if ((ret = deflateEnd(&stream)) != Z_OK)
{
return ret;
}
(*dstLen) = stream.total_out;
return ret;
}
static int GZipDecompress(unsigned char* dst, unsigned long* dstLen, const unsigned char* src, unsigned long srcLen) {
int ret = Z_ERRNO;
z_stream stream = { 0 };
stream.zalloc = NULL;
stream.zfree = NULL;
stream.opaque = NULL;
stream.avail_in = srcLen;
stream.avail_out = (*dstLen);
stream.next_in = (Bytef*)src;
stream.next_out = (Bytef*)dst;
ret = inflateInit2(&stream, MAX_WBITS + 16);
if (ret == Z_OK)
{
ret = inflate(&stream, Z_FINISH);
if (ret == Z_STREAM_END)
{
(*dstLen) = stream.total_out;
ret = Z_OK;
}
}
inflateEnd(&stream);
return ret;
}