c - 为什么我使用 zlib deflate 在我的源文件上出现错误?

标签 c debugging file compression zlib

编辑

  // 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/

相关文章:

debugging - 无法使用 PhantomJS 远程调试器在断点处访问 casper 实例方法

Android:共享下载的文件

c++ - 修复 C++ 中的段错误

Java - 逐行读取 csv 文件 - 遇到奇怪的不存在字符被读取!

c - 大文件内存管理

C -- 通过 const 声明访问非常量

c - 使用链表在C中堆叠

c - 一起使用 C 文件和 Cocoa 类时出现问题

c - 逐位打印c个数字

python - 无法在 emacs 中从 pydbgr 中找到模块