Server Help

Non-Subspace Related Coding - Creating a ZIP archive and adding files to it

Solo Ace - Tue Mar 21, 2006 4:20 pm
Post subject: Creating a ZIP archive and adding files to it
Before I'm wasting more time and energy on this I'll just ask here.

I want my (C++) program to create a ZIP and/or CAB archive file and put a bunch of files in it.
The files have to be readable by Windows Explorer and some automated system somewhere else.

I'm trying to figure out how to use Zlib, but I didn't get much further than
Code: Show/Hide
      z_stream zstm;
      memset(&zstm, 0, sizeof(z_stream));
      deflateInit(&zstm, Z_DEFAULT_COMPRESSION);


and I have no idea if this is the right direction I'm going.

I see all these great bloated libraries for file compression on the web!

What should I do?
Give me some examples or something, please.

I refuse to system() or CreateProcess() a 7zip exe! (The thought of that might even make some of you vomit icon_smile.gif ).
Cyan~Fire - Tue Mar 21, 2006 5:53 pm
Post subject:
For zlib, read the comments in zlib.h. That's how I learned, it tells you exactly what to do.

Here's an example from my code both for deflation/inflation (it doesn't use the zlib header though):
Code: Show/Hide
int deflate_file(Bytef *in, int length, FILE *out)
{
   z_stream strm;   //see zlib.h
   Bytef compressed[PERL_BUFF];   //This holds our compressed data until we write it.
   int code;   //holds return values from zlib functions

// Initialize the stream

   strm.zalloc = (alloc_func)Z_NULL;
   strm.zfree = (free_func)Z_NULL;
   strm.next_in = in;
   strm.avail_in = length;

   code = deflateInit2(&strm, -1, Z_DEFLATED, -15, 9, Z_DEFAULT_STRATEGY);

// Deflate it

   while (code == Z_OK)
   {
      strm.next_out = compressed;
      strm.avail_out = PERL_BUFF;
      code = deflate(&strm, Z_FINISH);
      if (code == Z_OK || code == Z_STREAM_END)
         fwrite(compressed, sizeof(Bytef), PERL_BUFF - strm.avail_out, out);
   }

// Cleanup & Return

   deflateEnd(&strm);

   return code;
}

int inflate_file(Bytef *in, int length, FILE *out)
{
   z_stream strm;
   Bytef uncompressed[PERL_BUFF];   //buffer for decompressed data
   int code;   //return from zlib funcs

// Initialize the stream

   strm.zalloc = (alloc_func)Z_NULL;
   strm.zfree = (free_func)Z_NULL;
   strm.next_in = in;
   strm.avail_in = length;
   strm.avail_out = 0;   //for the while condition check, not necessary
   code = inflateInit2(&strm, -15);

//   inflate it

   while (code == Z_OK && strm.avail_out == 0)
   {
      strm.next_out = uncompressed;
      strm.avail_out = sizeof(uncompressed);
      code = inflate(&strm, Z_SYNC_FLUSH);
      switch (code)
      {
      case Z_OK:
         fwrite(uncompressed, sizeof(Bytef),
            sizeof(uncompressed),   //assume uncompressed is full, so write it all
            out);
         break;
      case Z_STREAM_END:
         fwrite(uncompressed, sizeof(Bytef),
            sizeof(uncompressed) - strm.avail_out,   //can't assume that now
            out);
         break;
      }
   }

//   Cleanup & Return

   inflateEnd(&strm);

   return code;
}


For a nice file, though, you might want to use the built-in gz functions.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group