ZLIB compression Library bug ?
I'm using the ZLIB compression library for my Windows-based app. At the moment Im just trying to get the library to work properly before integration. Currently the problem is with the COMPRESSION2() wrapper. I set the 'level compression' value to 0 (no compression) expecting the input to be simply copied from src to destination unchanged however what actually happens is the resulting destination file is exactly 22 bytes larger than the source. WHats going on? Here's my code:
--
// ZLIB.cpp : Defines the entry point for the console application.
#include "zlib.h"
#include <fstream>
int main(int argc, char *argv[])
{
char *des, *src;
unsigned long desLen, srcLen;
std::ifstream inp("d:\\Test20.exe",std::ios::binary);
std::ofstream oup("d:\\Test20.zip",std::ios::binary);
if (!inp)
{std::cout << "Input error"; return 0;}
srcLen=3000;
desLen=srcLen*1.001+13;
src=new char[srcLen];
des=new char[desLen];
inp.read(src,srcLen);
while(inp.gcount())
{
compress2((Bytef *) des,&desLen,(Bytef *)src,inp.gcount(),0);
oup.write(des,desLen);
desLen=srcLen;
inp.read(src,srcLen);
}
oup.close();
inp.close();
return 0;
}
--
LIBZ.LIB and ZLIB.DLL were scavanged from another drawer to help with compiling but dont suspect them of being damaged.
Using a hex editor I found the output file to be littered with the extra 22 bytes in arbitrary locations. Some cases it just added data and in some cases it wrote inaccurate data.
[1677 byte] By [
quantass] at [2007-11-18 1:38:30]

# 2 Re: ZLIB compression Library bug ?
> Thats not a bug, it has to store where each file starts and stops or else it can't deconstruct the zip.
But even when compression is set to OFF? According to docs compression level 0 == copy data from src to data unaltered. I can understand the markers for when compression is ON though...
# 3 Re: ZLIB compression Library bug ?
Say you copy all the files into one file, how do you extract them? How do you figure where one file stops and the other file starts? The files are not altered, but the header of the zip tells it how many files there are and where they start and stop.
But really, its only 22bytes. Why do you care?
# 4 Re: ZLIB compression Library bug ?
Ohh i see. Just wanted to clear up this mystery. BTW, mwilliamson, do you have any working code lying around that makes use of COMPRESSION2/UNCOMPRESSION of the ZLIB library for Win32? I just cant seem to compress-uncompress properly -- The original file starts off at 10kb, then after compresion is 9kb, then after uncompression it 1kb. Clearly something went wrong. :P
I've attached my tiny source code (MyZLIBSrc.zip) with Visual C++ project files to make things quicker. If you can spot the error please let me know...