Re: Zlib compression
Re: Zlib compression
- Subject: Re: Zlib compression
- From: Don Murta <email@hidden>
- Date: Sat, 21 Feb 2004 17:34:59 -0700
Hello Andrew,
I've got two functions here that compress and decompress NSData
objects that may be of some use to you, though they go though a
different part of the zlib api than your using. Basically it encodes
the size of the original object as the first 4 bytes of the compressed
object, then compressed the data. Decompression is done in the reverse
fashion. Hope that helps some and makes some compression trivial for
you to implement.
Don
(sorry Andrew for the duplicates, forgot to cc to the list)
#import <zlib.h>
//
// DMCompress
// Compress the data of a NSData object.
// Needs a buffer 0.1% + 12 bytes larger than the source file.
// Encode the original size of the data as the first 4 bytes.
//
NSData* DMCompress( NSData *data ) {
NSData *result = NULL;
if( data ) {
uLong srcLength = [data length];
uLongf buffLength = srcLength * 1.001 + 12;
NSMutableData *compData = [[NSMutableData alloc]
initWithCapacity:buffLength+sizeof(uLong)];
[compData appendBytes:&srcLength length:sizeof(uLong)];
[compData increaseLengthBy:buffLength];
int error = compress( [compData mutableBytes] + sizeof(uLong),
&buffLength,
[*data bytes], srcLength );
switch( error ) {
case Z_OK:
[compData setLength:buffLength+sizeof(uLong)];
result = [compData copy];
break;
default:
NSCAssert( YES, @"Error compressing: Memory Error!" );
break;
}
[compData release];
}
return [result autorelease];
}
//
// DMDecompress
// Decompress a NSData's data.
// Decompress into a buffer the size in the first 4 bytes of of the
object, see above.
//
NSData* DMDecompress( NSData *data ) {
NSData *result = NULL;
if( data ) {
uLongf destLen;
[data getBytes:&destLen length:sizeof(uLong)];
NSMutableData *decompData = [[NSMutableData alloc]
initWithLength:destLen];
int error = uncompress( [decompData mutableBytes], &destLen,
[data bytes]+sizeof(uLong), [data length]-sizeof(uLong) );
switch( error ) {
case Z_OK:
result = [decompData copy];
break;
case Z_DATA_ERROR:
NSCAssert( YES, @"Error decompressing: Corrupt Data!" );
break;
default:
NSCAssert( YES, @"Error decompressing: Memory Error!" );
break;
}
[decompData release];
}
return [result autorelease];
}
On 21-Feb-04, at 3:34 PM, Andrew Merenbach wrote:
>
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
_______________________________________________
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.