编辑
// 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...可以吗?
如有任何想法或帮助,我们将不胜感激。
谢谢。
最佳答案
您应该以二进制模式而不是文本模式打开文件:
zipFile = fopen( "C:\\LOGS\\test.txt.z", "w+b" )
关于c - 为什么我使用 zlib deflate 在我的源文件上出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3261184/