Two Classes for Doing Gzip in Memory

Environment: OS independent

Now, many applications need API to do Gzip in memory. The Zlib form, http://www.gzip.org/zlib, provides Gzip and unGzip function, but they are for files. It's very inconvenient to compress and decompress small data that could be compressed in memory. I ported the Gzip and unGzip functions to two template classes, CA2GZIP and CGZIP2A, to do Gzip in memory; they also allocate and deallocate memory automatically. It works with zlib from http://www.gzip.org/zlib. The following is some code for how to use them.

#include "GZipHelper.h"
  void main()
  {
    char plainText[]="Plain text here";
      CA2GZIP gzip(plainText,strlen(plainText));
                                   // do compressing here;
      LPGZIP pgzip=gzip.pgzip;     // pgzip is zipped data pointer,
                                   // you can use it directly
      int len=gzip.Length;         // Length is length of zipped
                                   // data;

      CGZIP2A plain(pgzip,len);    // do uncompressing here

      char *pplain=plain.psz;      // psz is plain data pointer
      int  aLen=plain.Length;      // Length is length of unzipped
                                   // data.
  }

Downloads

Download demo project - 131 Kb


Comments

  • Decompress multiple buffers

    Posted by Legacy on 12/19/2003 12:00am

    Originally posted by: Makrem

    I ask if it is possible to modify the code of compress/decompress gzip in memory to accept multiple bufers composing a big file.

    What I want excatly is to decompress a gzip file buffer by buffer using ZLIB in memory routines.

    Reply
  • Wrong When Decompress the large file-data

    Posted by Legacy on 11/03/2003 12:00am

    Originally posted by: suxioajun

    I compressed one bitmap with this,but when I compressed with this,I found some data to be lost....faint!

    Reply
  • Best Compression and new code

    Posted by Legacy on 07/15/2003 12:00am

    Originally posted by: Philippe Boucher

    Hi

    The new code provide on the web page you link is working correctly for huge data. I found that speed of compression is a LOT increased by using this setting (with best compression) :
    CA2GZIPT<65536, Z_BEST_COMPRESSION, Z_DEFAULT_STRATEGY> gzip( (char*)debut, ramFile.GetLength() );

    The 65536 seems to helps

    Reply
  • New bug-fixed package: http://www.geocities.com/gaursoft/

    Posted by Legacy on 07/05/2003 12:00am

    Originally posted by: Gaur

    Reply
  • Problem with Large Files

    Posted by Legacy on 07/03/2003 12:00am

    Originally posted by: Lee W. Spencer

    First off, thanks for the code. I was needing to do just this very thing only a day or so before you posted GZipHelper. I was very much not looking forward to becoming that intimate with zlib.

    Secondly, I have a problem when trying to unzip a very large text(xml) file with CGZIP2A; My original zipped up data is 1.5MB, which compresses greatly to a whopping 25kB. When trying to unzip using CGZIP2A, I only get the first 644465 bytes of the original text.

    Using the following code snippet...
    (BigExample.gz is available at http://tf7.com/tf7/download/BigExample.gz)

    ************************

    FILE *InputFile = fopen("C:\\Temp\\BigExample.gz", "rb");
    lSizeofZipData = 8714;
    fread(pGZipData, 1, lSizeofZipData, InputFile);
    fclose(InputFile);

    // unzip the data
    CGZIP2A GZip((LPGZIP)pGZipData, lSizeofZipData);
    int UnzippedLen=GZip.Length;

    ******************************
    UnzippledLen comes back as 1261568 and should be 2760000, thus truncating the output.

    Any info appreciated.

    Reply
  • Can't delete the header, if you delete it ,I won't be a gzip format.

    Posted by Legacy on 07/02/2003 12:00am

    Originally posted by: gaur

    Reply
  • 10 byte header can be removed

    Posted by Legacy on 07/01/2003 12:00am

    Originally posted by: jlowrie

    The first ten bytes seem to be some sort of header
    that can be prepended upon decompression, saving a considerable amount for short buffers (as mentioned by Juan in an earlier post)

    This sequence is:

    {31,-117,8,0,0,0,0,0,0,11}

    Reply
  • well done!!!

    Posted by Legacy on 06/27/2003 12:00am

    Originally posted by: Sunia

    df sdfas dfasdf

    Reply
  • Can this work for Binary Data? LIke a DLL...

    Posted by Legacy on 06/26/2003 12:00am

    Originally posted by: San

    Can you please tell me how to compress a Binary buffer into the memory? I tried this but when I unzip the content is not same.

    Please help...

    Reply
  • in memory compress does not compress strings well

    Posted by Legacy on 06/25/2003 12:00am

    Originally posted by: Juan

    Normally when u use zlib library to compress strings in memory, the compressed result is much longer than the orginal string since no compress pattern can be developed there.
    I just wonder if this solution has the same issue?
    Thanks

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds