有没有一种实用的方法来压缩 NSData?

发布于 2024-12-20 03:37:14 字数 35 浏览 0 评论 0原文

我没有看到任何有关该主题的文档,但这并不意味着它不存在。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

∞梦里开花 2024-12-27 03:37:14

关注 @Zaph & @Brad Larson 的帖子,下面是 2 个方法 gzipInflategzipDeflate,它们可以很好地压缩/解压缩 NSData。 (代码重新格式化自 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];
}

从日志中:

[data length] (orig):989631
[data length] (gz):  102757
[data length] (ungz):989631

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
醉酒的小男人 2024-12-27 03:37:14

从 iOS 9.0 开始,内置了对更多压缩算法的支持。该库称为 libcompression,支持 LZ4、LZMA、ZLIB 和 LZFSE。

下面是一个使用 libcompression 解压 LZMA 的 Swift 示例。它很冗长,但避免了外部依赖,并且可以隐藏在 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
}

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
}
横笛休吹塞上声 2024-12-27 03:37:14

是的,用zlib压缩数据。

@Brad Larson 在此发布:参见此处并添加了代码。

有一个 CocoaPod 使用 Objective-Zip,作者:flyingdolphinstudio。

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.

暮年慕年 2024-12-27 03:37:14

我制作了一个很好的 Objective-C BZip2 压缩接口,可作为 CocoaPod 使用: https://github.com/blakewatters/BZipCompression

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

温折酒 2024-12-27 03:37:14

Swift 3 准备好 libcompression 的包装器。
https://github.com/mw99/DataCompression

Swift libcompression 包装器作为数据类型的扩展(ZLIB、LZFSE、LZMA、LZ4、deflate、RFC-1950、RFC-1951)

在操场上玩起来很有趣:
游乐场压缩率

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

暖树树初阳… 2024-12-27 03:37:14

在 iOS 13 和 macOS 10.15 或更高版本中,您可以使用 NSData 的新 compressed 方法:

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

不幸的是,此方法尚未移植到 Swift 的原生 Data > 类,但可以通过在上述代码行后面添加 as Data 来将 NSData 简单地转换为 Data

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.

温折酒 2024-12-27 03:37:14

试试这个: https://github.com/mattt/Godzippa 这对我很有帮助。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文