2016-09-18 155 views
1

我正在尝试使用zlib进行解压缩。我在zlib网站上查看教程,并通过代码生成不同大小的输出。zlib不同的解压缩大小

int CZLib::Inflate() { 
    int ret; 
    unsigned int have; 
    z_stream zstream; 
    unsigned char in[CHUNK]; 
    unsigned char out[CHUNK]; 

    zstream.zalloc = Z_NULL; 
    zstream.zfree = Z_NULL; 
    zstream.opaque = Z_NULL; 
    zstream.avail_in = 0; 
    zstream.next_in = Z_NULL; 
    ret = inflateInit(&zstream); 
    if (ret != Z_OK) 
     return ret; 

    do { 
     zstream.avail_in = fread(in, 1, CHUNK, fin); 
     if (ferror(fin)) { 
      (void)inflateEnd(&zstream); 
      return Z_ERRNO; 
     } 
     if (zstream.avail_in == 0) break; 
     zstream.next_in = in; 

     do { 
      zstream.avail_out = CHUNK; 
      zstream.next_out = out; 
      ret = inflate(&zstream, Z_NO_FLUSH); 
      assert(ret != Z_STREAM_ERROR); 
      switch (ret) { 
       case Z_NEED_DICT: 
        ret = Z_DATA_ERROR; 
       case Z_DATA_ERROR: 
       case Z_MEM_ERROR: 
        (void)inflateEnd(&zstream); 
        return ret; 
      } 
      have = CHUNK - zstream.avail_out; 
      if (fwrite(out, 1, have, fout) != have || ferror(fout)) { 
       (void)inflateEnd(&zstream); 
       return Z_ERRNO; 
      } 

     } while (zstream.avail_out == 0); 
    } while (ret != Z_STREAM_END); 

    (void)inflateEnd(&zstream); 
    return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR; 
} 

int CZLib::Inflate(const std::string& src) { 
    std::vector<char> output; 

    z_stream zstream; 
    zstream.zalloc = Z_NULL; 
    zstream.zfree = Z_NULL; 
    zstream.opaque = Z_NULL; 
    zstream.avail_in = 0; 
    zstream.next_in = Z_NULL; 

    int ret = inflateInit(&zstream); 
    if (ret != Z_OK) 
     return ret; 

    unsigned char in[CHUNK]; 
    unsigned char out[CHUNK]; 

    int have = 0, nByte = CHUNK, off = 0, remaining = src.size(); 

    if (src.size() < CHUNK) nByte = src.size(); 
    do { 
     memcpy(in, &src[off], nByte); 
     off += nByte; 
     remaining -= nByte; 

     if (nByte > 0) zstream.avail_in = nByte; 

     if (remaining > CHUNK) { nByte = CHUNK; } 
     else { nByte = remaining; } 

     if (zstream.avail_in == 0) break; 
     zstream.next_in = in; 

     do { 
      zstream.avail_out = CHUNK; 
      zstream.next_out = out; 

      ret = inflate(&zstream, Z_NO_FLUSH); 
      have = CHUNK - zstream.avail_out; 
      output.insert(output.end(), out, out + have); 

     } while (zstream.avail_out == 0); 
    } while (ret != Z_STREAM_END); 

    CFile* file = new CFile("in.out", "wb"); 
    file->Write<char>(&output[0], output.size()); 
    delete file; 
    return ret; 
} 

它使用相同的数据。其中一个读取磁盘上的文件,另一个使用内存(缓冲区方法)。 CHUNK大小16384.第一个代码产生524288(0x80000)和其他524800(0x80200)字节。差异是512字节。为什么会发生?

回答

0

在第一个代码示例,你这行

zstream.avail_in = fread(in, 1, CHUNK, fin); 

,然后你必须

if (zstream.avail_in == 0) break; 

停止循环。

在第二个代码示例,您必须在同一行停止循环,但你也有这样一行:

if (nByte > 0) zstream.avail_in = nByte; 
     ^^^^^^^^^ 
     So you only assign to zstream.avail_in when nByte > 0 

    .... 
    .... 

    if (zstream.avail_in == 0) break; 
     ^^^^^^^^^^^^^^^^ 
     Consequently this will not be true when nByte is zero and the 
     code will not exit 

试试这个:

zstream.avail_in = nByte; // Unconditional assignment 

    .... 

    if (zstream.avail_in <= 0) break; // Less or equal to zero 
+0

不,它并没有解决问题。我并排检查了两个版本。所有变量值和循环次数都相同。我认为问题是流或什么..有趣的是,当我在函数中创建文件时,同样的问题仍然存在。这太疯狂了。 – streamer

+0

嗯 - 第二个例子中的逻辑是错误的,但是由于'inflate'的返回值,你可能会跳出外部循环。我不知道它是否重要,但我注意到你的第一个例子叫'inflateEnd',但你的第二个例子不是。 – 4386427

+0

你说得对第二个例子,谢谢。我解决了这个问题。该问题源于未能关闭文件流。我正在使用C风格的文件系统。当文件关闭正确的问题解决。当退出函数并调用析构函数时,文件写入不完整(512字节) – streamer