13

I haven't seen any documentation on the topic, but that doesn't mean it doesn't exist.

eric.mitchell
  • 8,501
  • 12
  • 50
  • 91
  • what type of compression? straight zip? You could take the bytes from it and use the zip library – DanZimm Dec 08 '11 at 01:23
  • 4
    This question is absolutely equivalent to "is it possible to compress data?" and the answer is obviously "yes". – Lily Ballard Dec 08 '11 at 01:26
  • possible duplicate of [Compression API on the iPhone](http://stackoverflow.com/questions/230984/compression-api-on-the-iphone) – Brad Larson Dec 09 '11 at 17:25

7 Answers7

23

Following @Zaph & @Brad Larson's posts, below are the 2 methods gzipInflate and gzipDeflate that work just fine to compress/decompress NSData. (code reformatted from cocoadev.com/wiki/NSDataCategory

#import "zlib.h"
// don't forget to add libz.1.2.x.dylib into your project

- (NSData *)gzipInflate:(NSData*)data
{
    if ([data length] == 0) return data;

    unsigned full_length = [data length];
    unsigned half_length = [data length] / 2;

    NSMutableData *decompressed = [NSMutableData dataWithLength: full_length + half_length];
    BOOL done = NO;
    int status;

    z_stream strm;
    strm.next_in = (Bytef *)[data bytes];
    strm.avail_in = [data length];
    strm.total_out = 0;
    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;

    if (inflateInit2(&strm, (15+32)) != Z_OK) return nil;
    while (!done)
    {
        // Make sure we have enough room and reset the lengths.
        if (strm.total_out >= [decompressed length])
            [decompressed increaseLengthBy: half_length];
        strm.next_out = [decompressed mutableBytes] + strm.total_out;
        strm.avail_out = [decompressed length] - strm.total_out;

        // Inflate another chunk.
        status = inflate (&strm, Z_SYNC_FLUSH);
        if (status == Z_STREAM_END) done = YES;
        else if (status != Z_OK) break;
    }
    if (inflateEnd (&strm) != Z_OK) return nil;

    // Set real length.
    if (done)
    {
        [decompressed setLength: strm.total_out];
        return [NSData dataWithData: decompressed];
    }
    else return nil;
}

- (NSData *)gzipDeflate:(NSData*)data
{
    if ([data length] == 0) return data;

    z_stream strm;

    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
    strm.opaque = Z_NULL;
    strm.total_out = 0;
    strm.next_in=(Bytef *)[data bytes];
    strm.avail_in = [data length];

    // Compresssion Levels:
    //   Z_NO_COMPRESSION
    //   Z_BEST_SPEED
    //   Z_BEST_COMPRESSION
    //   Z_DEFAULT_COMPRESSION

    if (deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 8, Z_DEFAULT_STRATEGY) != Z_OK) return nil;

    NSMutableData *compressed = [NSMutableData dataWithLength:16384];  // 16K chunks for expansion

    do {

        if (strm.total_out >= [compressed length])
            [compressed increaseLengthBy: 16384];

        strm.next_out = [compressed mutableBytes] + strm.total_out;
        strm.avail_out = [compressed length] - strm.total_out;

        deflate(&strm, Z_FINISH);  

    } while (strm.avail_out == 0);

    deflateEnd(&strm);

    [compressed setLength: strm.total_out];
    return [NSData dataWithData:compressed];
}

From the log:

[data length] (orig):989631
[data length] (gz):  102757
[data length] (ungz):989631
lucasart
  • 1,633
  • 1
  • 20
  • 29
  • @Rickay if wanting to use bzip2 instead of gzip, see [this post](http://stackoverflow.com/questions/9577735/how-compress-data-in-memory-buffer-by-using-libbz2-library-in-c-program/11390277#11390277). – lucasart Jul 09 '12 at 14:48
  • Looks very nice, but when compiling, Xcode says _BZ2_bzCompressEnd and friends are undefined symbols for armv7. – eric.mitchell Jul 09 '12 at 18:36
  • Not sure what's wrong with the setup here. Besides adding `#import "bzlib.h"`, making sure that the lib `libbz2.1.x.dylib ` is added in Build Phases -> Link Binary with Libaries, it should compile just fine. – lucasart Jul 10 '12 at 03:49
  • Oh... add "-lbz2" into `Build Settings` -> `Other Linker Flags`. That should do it. – lucasart Jul 10 '12 at 03:51
  • 2
    Good job put it together. In my case, All I needed to add libz.dylib. – user523234 Jul 16 '12 at 16:49
  • I used this to compress NSDATA but i m getting log – Vipin Vijay Feb 24 '13 at 04:55
  • Please help- I trying to save an `MPMediaItem` onto a server by compressing it with zlib (your above code). However the length only reduces from 15313956 to 14805754. It needs to compress alot more. I changed the MPMediaItem to NSData with help of the code on this link- http://stackoverflow.com/questions/18824983/nsdata-from-mpmediaitem#comment27770517_18826374 – Badi8beach Aug 31 '14 at 07:36
  • I have spent alot of time trying to figure this out. Based on my understanding it should work. Also, the linker does complain that `lseek` from `gzlib.c` and few others like `read` and `write` is invalid. But from the blogs I went through, this shouldn't be an issue. Any help on this well greatly appreciated. – Badi8beach Aug 31 '14 at 07:39
  • Note there is an updated inflate method for better 64 bit support in this answer: http://stackoverflow.com/a/17822217/449161 – Ben Flynn Dec 15 '14 at 20:40
  • Hi..i am using Gzip for my large string data compression....is there any performance issue with Gzip? is Zip better then Gzip? – H Raval Mar 07 '17 at 11:40
12

Starting with iOS 9.0, there is built-in support for a few more compression algorithms. The library is called libcompression and supports LZ4, LZMA, ZLIB and LZFSE.

Here’s a Swift example of using libcompression to decompress LZMA. It’s verbose, but avoids external dependencies and could be hidden in an extension on NSData.

import Compression

let streamPtr = UnsafeMutablePointer<compression_stream>.alloc(1)
var stream = streamPtr.memory
var status: compression_status

status = compression_stream_init(&stream, COMPRESSION_STREAM_DECODE, COMPRESSION_LZMA)
stream.src_ptr = UnsafePointer<UInt8>(compressedData.bytes)
stream.src_size = compressedData.length

let dstBufferSize: size_t = 4096
let dstBufferPtr = UnsafeMutablePointer<UInt8>.alloc(dstBufferSize)
stream.dst_ptr = dstBufferPtr
stream.dst_size = dstBufferSize

let decompressedData = NSMutableData()

repeat {
    status = compression_stream_process(&stream, 0)
    switch status {
    case COMPRESSION_STATUS_OK:
        if stream.dst_size == 0 {
            decompressedData.appendBytes(dstBufferPtr, length: dstBufferSize)
            stream.dst_ptr = dstBufferPtr
            stream.dst_size = dstBufferSize
        }
    case COMPRESSION_STATUS_END:
        if stream.dst_ptr > dstBufferPtr {
            decompressedData.appendBytes(dstBufferPtr, length: stream.dst_ptr - dstBufferPtr)
        }
    default:
        break
    }
}
while status == COMPRESSION_STATUS_OK

compression_stream_destroy(&stream)

if status == COMPRESSION_STATUS_END {
    // Decompression succeeded, do something with decompressedData
}
else {
    // Decompression failed
}
jbg
  • 3,578
  • 1
  • 18
  • 28
  • 2
    A Swift Extension on NSData can be found here: https://github.com/leemorgan/NSData-Compression – Klaas Jun 25 '16 at 11:37
5

Yes, compress the data with zlib.

@Brad Larson posted on this: see here and added the code as well.

There is a CocoaPod which uses Objective-Zip by flyingdolphinstudio.

Community
  • 1
  • 1
zaph
  • 108,117
  • 19
  • 176
  • 215
  • 1
    How did anyone manage to read that NSDataCategory page (http://cocoadev.com/wiki/NSDataCategory)? Do they format at all this code? – jkcl Jul 05 '12 at 16:45
3

I have made a nice Objective-C BZip2 compression interface available as a CocoaPod: https://github.com/blakewatters/BZipCompression

Blake Watters
  • 6,527
  • 1
  • 41
  • 33
2

Swift 3 ready wrapper around libcompression. https://github.com/mw99/DataCompression

Swift libcompression wrapper as an extension for the Data type (ZLIB, LZFSE, LZMA, LZ4, deflate, RFC-1950, RFC-1951)

Interesting to play with in the playground: playground compression rates

LimeRed
  • 998
  • 10
  • 15
1

In iOS 13 and macOS 10.15 or newer you can use the new compressed method of NSData:

let compressedData = try? NSData(data: data).compressed(using: .zlib)

Unfortunately this method hasn't been ported to Swift's native Data class, but NSData can be simply casted to Data by adding as Data after the above code line.

Ely
  • 5,159
  • 1
  • 34
  • 51
  • if the data is for an image it won't be compressed using this method – JAHelia Mar 07 '20 at 11:17
  • 1
    @JAHelia Could be one of those Apples' undocumented "protect the developer against it's own bad ideas" features. – Ely Mar 07 '20 at 11:35
0

Try this: https://github.com/mattt/Godzippa It was helpful for me.

Nuzhdin Vladimir
  • 1,373
  • 17
  • 29