- 论坛徽章:
- 1
|
代码一
- #include "windows.h"
- #include "stdio.h"
- #include "zlib.h"
- #define ZIPCPZ_BUFFER 32768
- static char tmp_in[ZIPCPZ_BUFFER];
- static char tmp_out[ZIPCPZ_BUFFER];
- static z_stream stream;
- static int in_length=0;
- static int out_length=0;
- int ZIPCpzInit(int Level=Z_DEFAULT_COMPRESSION)
- {
- int err;
- stream.next_in = (Bytef*)tmp_in;
- stream.avail_in = (uInt)in_length;
- stream.next_out = (Bytef*)tmp_out;
- stream.avail_out = ZIPCPZ_BUFFER;
- stream.zalloc = (alloc_func)0;
- stream.zfree = (free_func)0;
- stream.opaque = (voidpf)0;
- err = deflateInit(&stream, Level);
- if (err != Z_OK) return err;
- return Z_OK;
- }
- int ZIPCpz (LPCSTR src,DWORD src_length,FILE *fp,int bFlush=0)
- {
- int err;
- DWORD offset=0;
- if( src_length >; 0 ) //make sure input stream is available
- while(1)
- {
- int rLen,wLen;
- if( src_length + in_length >;= ZIPCPZ_BUFFER )
- {
- //buffer a block of input stream to tmp_in if it's big enough
- rLen=ZIPCPZ_BUFFER-in_length;
- memcpy(&tmp_in[in_length],src+offset,rLen);
- src_length -= rLen;
- in_length += rLen;
- offset+=rLen;
- { //zip tmp_in
- stream.next_in = (Bytef*)tmp_in;
- stream.avail_in = (uInt)in_length;
- stream.next_out = (Bytef*)(tmp_out+out_length);
- stream.avail_out = ZIPCPZ_BUFFER-out_length;
- while(1)
- {
- wLen=ZIPCPZ_BUFFER-out_length;
- err = deflate(&stream, Z_NO_FLUSH);
- wLen -= stream.avail_out;
- if(err ==Z_OK)
- {
- if( (stream.avail_out!=0) ) //cpz finished
- {
- out_length+=wLen;
- break;
- }
- else
- { // out buffer full
- fwrite(tmp_out,1,ZIPCPZ_BUFFER,fp);
- out_length=0;
- stream.next_out = (Bytef*)tmp_out;
- stream.avail_out = ZIPCPZ_BUFFER;
- }
- }
- else
- {
- //error ~~~~~~~~~~~
- printf("Error!\n");
- printf("Error no %d\n",err);
- }
- }
- in_length=0;
- }
- }
- else
- {
- // buffer whole input stream to tmp_in if it's not big enough
- // then wait for next call to fill tmp_in
- memcpy(&tmp_in[in_length],src+offset,src_length);
- in_length+=src_length;
- break;
- }
- }//write ok;
- if(bFlush)
- {
- if(in_length>;0)
- {
- stream.next_in = (Bytef*)tmp_in;
- stream.avail_in = (uInt)in_length;
- stream.next_out = (Bytef*)(tmp_out+out_length);
- stream.avail_out = ZIPCPZ_BUFFER-out_length;
- while(1)
- {
- DWORD wLen=ZIPCPZ_BUFFER-out_length;
- err = deflate(&stream, Z_FINISH);
- wLen -= stream.avail_out;
- if(err ==Z_STREAM_END)
- {
- if( (stream.avail_out!=0) ) //cpz finished
- {
- out_length+=wLen;
- break;
- }
- }
- else
- {
- if( err== Z_OK)
- {
- // out buffer full
- fwrite(tmp_out,1,ZIPCPZ_BUFFER,fp);
- out_length=0;
- stream.next_out = (Bytef*)tmp_out;
- stream.avail_out = ZIPCPZ_BUFFER;
- }
- else
- {
- //error ~~~~~~~~~~~
- printf("Error while end z file!\n");
- printf("Error no %d\n",err);
- break;
- }
- }
- }
- }
- fwrite(tmp_out,1,out_length,fp);
- in_length=0;
- out_length=0;
- }
- return Z_OK;
- }
- int ZIPCpzFinal()
- {
- int err;
- err = deflateEnd(&stream);
- return err;
- }
- int ZEXPORT uncompress (
- Bytef *dest,
- uLongf *destLen,
- const Bytef *source,
- uLong sourceLen)
- {
- z_stream stream;
- int err;
- stream.next_in = (Bytef*)source;
- stream.avail_in = (uInt)sourceLen;
- stream.next_out = dest;
- stream.avail_out = (uInt)*destLen;
- if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
- stream.zalloc = (alloc_func)0;
- stream.zfree = (free_func)0;
- err = inflateInit(&stream);
- if (err != Z_OK) return err;
- err = inflate(&stream, Z_FINISH);
- if (err != Z_STREAM_END) {
- inflateEnd(&stream);
- return err == Z_OK ? Z_BUF_ERROR : err;
- }
- *destLen = stream.total_out;
- err = inflateEnd(&stream);
- return err;
- }
- void main()
- {
- printf("Zlib Version %s !\n",zlibVersion());
- /*char *pByte=new char[1024*1024];
- for(int i=0;i<1024*1024;i++)
- {
- pByte[i]=i;
- }
- ZIPCpz(pByte,1024*1024,"b.z");*/
- long length;
- FILE* fp=fopen("a.bin","rb");
- fseek(fp,0,SEEK_END);
- length=ftell(fp);
- fseek(fp,0,SEEK_SET);
- char *pByte=new char[length];
- FILE* fp1=fopen("b.z","wb");
- fread(pByte,1,length,fp);
- ZIPCpzInit();
- ZIPCpz(pByte,length>;>;1,fp1);
- ZIPCpz(pByte+(length>;>;1),length-(length>;>;1),fp1);
- ZIPCpz(0,0,fp1,1);
- fclose(fp1);
- ZIPCpzFinal();
- fclose(fp);
- printf("Add ok!\n");
- delete pByte;
- FILE* fp2=fopen("b.z","rb");
- fseek(fp2,0,SEEK_END);
- length=ftell(fp2);
- fseek(fp2,0,SEEK_SET);
- pByte=new char[length];
- fread(pByte,length,1,fp2);
- fclose(fp2);
- fp2=fopen("c.bin","wb");
- LPSTR pByte1=new char[1024*1024*25];
- DWORD length1=1024*1024*25;
- uncompress((Bytef*)pByte1,&length1,(Bytef*)pByte,length);
- fwrite(pByte1,1,length1,fp2);
- fclose(fp2);
- }
复制代码 |
|