• Advertisement
Sign in to follow this  

deflate/inflate with zlib 1.2.2

This topic is 4738 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi all. Im trying to figure out how this deflate/inflate and streaming works in the zlib library. And I'm having a little trouble. Now this is the extent of my understanding: z_stream is the main streaming object, like a FILE* I suppose. There are 4 main fields in it that need to be filled and watched. next_in: is what you set to point to the starting input of your input data. next_out: is a buffer where processed data is stored. avail_in: is how many bytes you are inflating/deflating. avail_out: is how many bytes you can hold in the buffer. If the above is correct, then why does the following code not work?
unsigned char text[] = "My name is nothing and I have absolutely nothing to do";

void main()
{
    unsigned char buffer[128];
    z_stream str;
    memset(&str, 0, sizeof(z_stream));

    str.next_in = text;
    str.avail_in = strlen((const char*)text) + 1;

    str.next_out = buffer;
    str.avail_out = 128;

    deflate( &str, Z_FINISH );

    cout<< str.total_out<< endl;
    cout<< str.total_in<< endl;

    deflateEnd(&str);
}

I know that nothing is happening because total_out and total_in are both zero, and they shouldnt be zero right? Thanks for any help.

Share this post


Link to post
Share on other sites
Advertisement
Here's my code for compressing a buffer with zlib. It calls the LPAppend function pointer to append compressed data (write it to a file, or add it to a memory buffer):

bool ZLibCompress(unsigned char* pBuffer, unsigned int dwLen, LPAppend pfnAppend, void* pContext, int nCompression)
{
z_stream theStream;
unsigned char byBuffer[1024];
int nRet, nFuncRet;

// Begin compression //
memset(&theStream,0,sizeof(theStream));
if(deflateInit(&theStream,nCompression) != Z_OK)
return false;

// Flush stream //
theStream.next_in = pBuffer;
theStream.avail_in = dwLen;
do {
theStream.next_out = byBuffer;
theStream.avail_out = 1024;
nRet = deflate(&theStream,Z_FINISH);
if((nRet != Z_OK) && (nRet != Z_STREAM_END))
{
deflateEnd(&theStream);
return false;
}
nFuncRet = pfnAppend(byBuffer,1024-theStream.avail_out,pContext);
if(nFuncRet == RET_ERROR)
{
deflateEnd(&theStream);
return false;
}
} while((nRet != Z_STREAM_END) && (nFuncRet != RET_STOP));

// Finish up //
deflateEnd(&theStream);
return true;
}


Your problem is that you never call deflateInit().

Share this post


Link to post
Share on other sites
Hello, does anyone of you know, how to compress more than 1 file into a .gz for example?
And how to compress them without changing their or the .gz file names?

I made a small program for compressing a text file (Package.txt) into Package.gz i then opened the package, but it had a file without extension inside it (just "Package"). And after some changes it compressed as i wanted, but the compressed file's name was now Package.txt.gz.

Ok, i thought it doesn't matter and then tried to write a picture into my Package.txt.gz file. I couldn't do that correctly either: the picture was written into Package.txt in the .gz file. Yes, i think that ZLib source code isn't enough for me..

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include "zlib.h"

#include <fcntl.h>
#include <io.h>
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)

using namespace std;

int main(int argc, char *argv[])
{
int len=0;
char buf[50000];
gzFile out;

FILE *in = fopen("Package.txt","rb");
if(!in){
printf("\nPackage.txt doesn't exist, sorry..\n");
exit(1);
}

out = gzopen("Package.txt.gz", "wb9");
if(!out){
printf("\nCan't write to Package.gz, sorry..\n");
exit(1);
}

for (;;) {
len = (int)fread(&buf, 1, sizeof(buf), in);
if (ferror(in)) {
printf("\nCouldn't read file Package.txt\n");
exit(2);
}
printf("Length %d Buffer> %s\n",len,buf);

if (len == 0) break;

if (!gzwrite(out, buf, (unsigned)len)) printf("\nWriting to Package.gz failed..\n");
}

in = fopen("ice.JPG","rb");
if(!in){
printf("\nThe file ice.JPG doesn't exist..\n");
exit(1);
}

for (;;) {
len = (int)fread(&buf, 1, sizeof(buf), in);
if (ferror(in)) {
printf("\nFailed to open file ice.JPG..\n");
exit(2);
}

if (len == 0) break;

if (!gzwrite(out, buf, (unsigned)len)) printf("\nFailed to write to Package.gz..\n");
}

fclose(in);
if (gzclose(out) != Z_OK) printf("\nCouldn't close Package.gz..\n");

system("PAUSE");
return EXIT_SUCCESS;
}

All suggestions are welcome, Thank You..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement