Skip to content

Instantly share code, notes, and snippets.

@Nyx0uf
Created January 16, 2012 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nyx0uf/1622210 to your computer and use it in GitHub Desktop.
Save Nyx0uf/1622210 to your computer and use it in GitHub Desktop.
Fast gzip deflate for small files
-(NSData*)gzDeflate
{
/// Max decompressed data size : 128Kb
/// Feel free to increase it to suit your needs
unsigned char outt[131072];
z_stream strm = (z_stream){.zalloc = Z_NULL, .zfree = Z_NULL, .opaque = Z_NULL, .avail_in = [self length], .next_in = (Bytef*)[self bytes], .avail_out = 131072, .next_out = outt};
if (inflateInit2(&strm, (15 + 16)) != Z_OK)
return nil;
int ret = inflate(&strm, Z_NO_FLUSH);
if (ret != Z_STREAM_END && ret != Z_OK)
{
inflateEnd(&strm);
return nil;
}
return [NSData dataWithBytes:outt length:strm.total_out];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment