Zlib compression
Zlib compression
- Subject: Zlib compression
- From: Andrew Merenbach <email@hidden>
- Date: Sat, 21 Feb 2004 14:34:19 -0800
I wish to use zlib to compress a file, and have code below for a small
file (mbox) that exists. The problem is that the program, when run
from Xcode, completes the task but crashes itself; the data written,
additionally, does not appear valid. I could use gzwrite(), but wish
to make an NSData wrapper (the one already referenced in the archives
appears to be offline). Any help would be greatly appreciated, as this
problem has had me banging my head off and on for the past few months.
Cheers,
Andrew
/* begin code snippet */
- (BOOL)compressFile {
NSData *data = [NSData
dataWithContentsOfFile:@"/Users/andrew/mbox"];
z_streamp astream = malloc(sizeof(z_streamp));
int result, zresult;
NSMutableData *outData;
astream->zalloc = Z_NULL;
astream->zfree = Z_NULL;
astream->opaque = Z_NULL;
astream->next_in = (unsigned char *)[data bytes];
astream->avail_in = [data length];
result = deflateInit (astream, Z_DEFAULT_COMPRESSION); //
Z_BEST_COMPRESSION
if (result != Z_OK) return NO;
outData = [NSMutableData data];
zresult = Z_OK;
while (zresult == Z_OK) {
zresult = deflate (astream, Z_FINISH);
[outData appendBytes:(astream->next_out)
length:(astream->avail_out)];
}
if (zresult != Z_STREAM_END) NSLog(@"zres = %i",zresult);
result = deflateEnd(astream);
if (result != Z_OK) return NO;
[outData writeToFile:@"/Users/andrew/mbox.gz" atomically:YES];
free(astream);
return YES;
}
/* end code snippet */
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.