头文件
//压缩
int CompressFile(const char* source, const char* dest);
//解压缩
int UnCompressFile(const char* source, const char* dest);
源文件
#include <stdio.h>
#include "zlib-1.2.13/zlib.h"
typedef unsigned char byte;
//压缩
int CompressFile(const char* source, const char* dest)
{
int func_ret = 0;
FILE *source_fp = fopen(source, "rb");
if (source_fp == NULL)
{
return -1;
}
FILE *dest_fp = fopen(dest, "wb");
if (dest_fp == NULL)
{
fclose(source_fp);
return -1;
}
while (true)
{
byte *buf = new byte[1024 * 1024];
int fread_ret = fread(buf, sizeof(byte), 1024 * 1024, source_fp);
if (fread_ret == 0)
{
break;
}
uLong out_buf_long = 1024 * 1024;
byte *out_buf = new byte[out_buf_long];
if (compress(out_buf, &out_buf_long, buf, fread_ret) != Z_OK)
{
func_ret = -1;