Solved

Zlib Compression

Posted on 2004-04-02
5
1,064 Views
Last Modified: 2011-04-14
Hi,

Can someone help me with the following code. Basically my requirement is to create an archive (a zip file) with serveral files.

The following code opens the "testxml2.txt" file and tries to write it to the Test.Zip file. Test.zip file is getting created with 1KB but "write" operation is not working.


#define _WINDOWS
#define ZLIB_DLL

#include "zlib.h"
#include <fstream>
#include <iostream>

using namespace std;

int CompressFile(char type, char *filename, long startFilePos, char *outfilename, char fileAction);

enum {COMPRESS,UNCOMPRESS, NEWFILE, APPENDFILE};

int main(int argc, char *argv[])
{
      
      int error;

      error=CompressFile(COMPRESS, "c:\\testxml2.txt", 0, "Test.zip", NEWFILE);

      cout << "Error  " << error << endl;

      return 0;
}

int CompressFile(char type, char *filename, long startFilePos, char *outfilename, char fileAction)
{
      z_stream stream;
      int err;
      int level;
      ifstream inFile;
      ofstream outFile;
      bool done=false, atEOF=false;
      unsigned char *incData=NULL, *outData=NULL;
      int inSize, outSize, count;
      int BUFFER_SIZE=3000;

      level=3;
    stream.zalloc = Z_NULL;
    stream.zfree  = Z_NULL;
    stream.opaque = Z_NULL;

      // Initalize ZLIB
      if (type==COMPRESS)
            err = deflateInit(&stream, level);
      else
            err = inflateInit(&stream);

    if (err != Z_OK) return err;

      inSize=BUFFER_SIZE;
      outSize=inSize*(float)1.001+14;
      
      if ((incData=new unsigned char[inSize])==NULL)
      {err=Z_MEM_ERROR; goto cleanup;}
      
      if ((outData=new unsigned char[outSize])==NULL)
      {err=Z_MEM_ERROR; goto cleanup;}

      inFile.open(filename, ios::in);
      if (!inFile)
            goto cleanup;

      if (fileAction==NEWFILE)
            outFile.open(outfilename, ios::in | ios::app);
      else
            outFile.open(outfilename);
      if (!outFile)
            goto cleanup;

      if (startFilePos>0)
            inFile.seekg(startFilePos,ios::beg);
      
      stream.avail_in=0;
      stream.next_in=incData;
      stream.avail_out=outSize;
      stream.next_out=outData;
      //do
      //{
       while (stream.avail_in==0 && stream.avail_out==outSize && atEOF==false)
       {

            if (stream.avail_in==0)
            {
                  inFile.read((char *)incData,inSize);
                  cout << incData << endl;
                  stream.next_in=incData;
                  stream.avail_in=inFile.gcount();
                  atEOF=inFile.eof();
            }
              

            // Do some compressing
            if (type==COMPRESS)
                  err = deflate(&stream, Z_NO_FLUSH);
            else
                  err = inflate(&stream, Z_NO_FLUSH);

      
            if (err != Z_OK)
                  goto cleanup;
            
                  count=outSize-stream.avail_out;
                  if (count)
                     outFile.write((char *)outData,count);
                  stream.next_out=outData;
                  stream.avail_out=outSize;

}      

cleanup:

      inFile.close();
      outFile.close();

      if (err!=Z_OK)
            unlink(outfilename);

      if (type==COMPRESS)
            deflateEnd(&stream);
      else
            inflateEnd(&stream);
      
      if (incData!=NULL) delete incData;
      if (outData!=NULL) delete outData;

      return err;
}

Thanks.
0
Comment
Question by:newuser11
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10745250
Your problem is here;

    if (fileAction==NEWFILE)
          outFile.open(outfilename, ios::in | ios::app);

You are opening an ofstream with ios::in. This will not work. Change ios::in to ios::out, and you should be able to run your program (it works at least if I run it under Linux - I don't have zlib for Windows installed).
0
 

Author Comment

by:newuser11
ID: 10758409
Hi khkremer,

When you said you were able to run it. Did it create a .zip file with the contents. For me it creates the zip file and it says it's not a valid archive file. When I try to uncompress it the file got created with 0 bytes.

Thanks.
0
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 250 total points
ID: 10762539
You are right, it's not a valid archive file Are you aware of the example.c file in the zlib distribution? It does contain samples for using the different functions.

You should probably not mix compression and decompression in the same function, this can get pretty confusing. Look at example.c and try to remplement this in your own code.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to copy an image file into clipboard C/C++? 1 250
Dynamically allocate memory 9 75
Error creating a new C++ project in ,net 20 59
Precision Problem in C++ 7 67
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question