CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2001
    Posts
    391

    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.
    Last edited by quantass; January 27th, 2003 at 11:19 PM.

  2. #2
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    Thats not a bug, it has to store where each file starts and stops or else it can't deconstruct the zip.

  3. #3
    Join Date
    Dec 2001
    Posts
    391
    > 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...

  4. #4
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    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?

  5. #5
    Join Date
    Dec 2001
    Posts
    391
    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...
    Attached Files Attached Files

  6. #6
    Join Date
    Dec 2001
    Posts
    391
    I decided to explore the deflate() function but am still having some problems. Compressing to the outfile only produces 2 bytes of output. Odd. Here's the small src code.
    Attached Files Attached Files
    Last edited by quantass; January 29th, 2003 at 05:17 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured