编辑
// open output file for writing
if ( ( outfilefd = fopen( file_name, "w+t" ) ) == NULL )
{
fprintf(stderr, "Unable to create file\n");
exit(1);
}
写入文件,然后需要压缩。
打开.z文件,然后调用def()
FILE *zipFile;
if ( ( zipFile = fopen( "C:\\LOGS\\test.txt.z", "w+t" ) ) == NULL )
{
fprintf(stderr, "Unable to create file\n");
exit(1);
}
int ret = def(outfilefd, zipFile, Z_DEFAULT_COMPRESSION);
if (ret != Z_OK)
printf("ZLIB Error");
使用def(),直接来自site:
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);
int g = ferror(source);//<---------------- EROR HERE returning 32?
if (ferror(source)) {
(void)deflateEnd(&strm);
return Z_ERRNO;
}
zipFile
不为空,strm.avail_in
= 16343,in
有数据但ferror(source)
返回32?
编辑 - 同样strm.avail_in
= 16343引起了我的注意,CHUNK
= 16384 ....是吗?
感谢任何想法或帮助。
谢谢。
答案 0 :(得分:2)
您应该以二进制模式而不是文本模式打开文件:
zipFile = fopen( "C:\\LOGS\\test.txt.z", "w+b" )