#include <iostream>
using namespace std;
#include "zlib.h"
#define MaxBufferSize 1024*10
int main()
{
int i;
FILE* File_src;
FILE* File_tmp;
FILE* File_dest;
unsigned long len_src;
unsigned long len_tmp;
unsigned long len_dest;
unsigned char* buffer_src=new unsigned char[MaxBufferSize];
unsigned char* buffer_tmp=new unsigned char[MaxBufferSize];
unsigned char* buffer_dest=new unsigned char[MaxBufferSize];
//r 以只读方式打开文件,该文件必须存在
File_src=fopen("C:\\Users\\238\\Desktop\\a12.txt","r");
//把文件的内容存到 buffer_src 中 返回读取元素的个数
len_src=fread(buffer_src,sizeof(char),MaxBufferSize-1,File_src);
for(int i=0;i<len_src;i++)
{
cout<<buffer_src[i];
}
cout<<endl;
//函数将 source 缓冲区中的内容压缩到 dest 缓冲区 从右向左
compress(buffer_tmp,&len_tmp,buffer_src,len_src);
//w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。
File_tmp=fopen("C:\\Users\\238\\Desktop\\a.txt","w");
//把压缩后的内容写到File_tmp文件中
fwrite(buffer_tmp,sizeof(char),len_tmp,File_tmp);
for(int i=0;i<len_tmp;i++)
{
cout<<buffer_tmp[i];
}
cout<<endl;
//从右向左
uncompress(buffer_dest,&len_dest,buffer_tmp,len_tmp);
File_tmp=fopen("C:\\Users\\238\\Desktop\\a.txt","r");
File_dest=fopen("C:\\Users\\238\\Desktop\\dest.txt","w");
fwrite(buffer_dest,sizeof(char),len_dest,File_dest);
for(int i=0;i<len_dest;i++)
{
cout<<buffer_dest[i];
}
cout<<endl;
return 0;
}
compress 与 uncompress
最新推荐文章于 2023-01-12 20:45:02 发布