Mike Guest
|
Posted: Sun Aug 03, 2003 8:29 pm Post subject: ZLIB compress() problems... (newbie) |
|
|
Hi,
This may a c/c++ problem rather than a zlib problem, but hopefully
many here will have used zlib and can tell me if I>m OT.
I>m writing a compressed SWF file (Flash). In this format, the first 8
bytes should be uncompressed, the rest compressed with zlib. The first
8 bytes indicate whether compression is used, the version num, and the
uncompressed file size.
If I write my file uncompressed, with appropriate header, it plays
back fine. If I try to make a compressed version, it doesn>t.
Here>s the relevant code (using vc++):
///////////////////////////////////
std::ofstream fileout(fileName.GetBuffer(0),std::ios::binary);
fileName.ReleaseBuffer();
std::ostringstream f(std::ios::binary);
..
..
..
..
// compressed file - write start of header to fileout
fileout << "CWS";
UBYTE version = 6;
fileout << version;
UDWORD filesize = f.str().size();
int minbits = (int)ceil(((fbase_max(GetBitSize(size.GetX2()),
GetBitSize(size.GetY2())+1)) * 4 + 5) / 8.0);
UDWORD size_adjust = 12+minbits;
filesize += size_adjust;
WRITE_UDWORD2(filesize, fileout)
// compress f with zlib, write to fileout
unsigned long csize = filesize+(UDWORD)(filesize/100)+12; // satisfy
min size requirements for compression buffer
unsigned char *compression_buffer = (unsigned char*)malloc(csize);
if(compress(compression_buffer, &csize, (const unsigned char*)&f,
filesize/*, Z_BEST_COMPRESSION*/) == Z_OK){
fileout.write((char*)compression_buffer,csize);
}
else
{
//throw
TRACE ("Compression didn>t work\n");
}
free(compression_buffer);
///////////////////////////////////
Is this the wrong use of compress() with a std::ostringstream?
Compress2() doesn>t work either. Or have I missed something?
Thanks for any suggestions.
M |
|