如何使用 Zlib 压缩数据,而不直接使用 zlib.dylib?

发布于 2024-08-15 01:54:32 字数 55 浏览 4 评论 0原文

是否有一个类允许使用 Zlib 压缩数据,或者直接使用 zlib.dylib 是我唯一的可能性?

Is there a class that allows compressing data using Zlib, or is using zlib.dylib directly the only possibility I have?

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

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

发布评论

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

评论(3

德意的啸 2024-08-22 01:54:32

NSData+Compression 是一个易于使用的 NSData 类别实现。

用法:

NSData* compressed = [myData zlibDeflate];
NSData* originalData = [compressed zlibInflate];

NSData+Compression is an easy to use NSData category implementation.

Usage:

NSData* compressed = [myData zlibDeflate];
NSData* originalData = [compressed zlibInflate];
野侃 2024-08-22 01:54:32

这对我有用:
1)基于ZLib的Objective-Zip新位置:https://github.com/gianlucabertani/Objective-Zip< /a>

Podfile:

pod 'objective-zip', '~> 1.0'

简单示例:

#import "ViewController.h"
#import "Objective-Zip.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *docsDir;
    NSArray *dirPaths;
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSString *path = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"test.zip"]];

    OZZipFile *zipFile= [[OZZipFile alloc] initWithFileName:path
                                                       mode:OZZipFileModeCreate];
    NSString *str = @"Hello world";
    OZZipWriteStream *stream= [zipFile writeFileInZipWithName:@"file.txt"
                                             compressionLevel:OZZipCompressionLevelBest];
    [stream writeData:[str dataUsingEncoding:NSUTF8StringEncoding]];
    [stream finishedWriting];
    [zipFile close];
}

2)其他基于 zlib 的库也工作得很好。 https://github.com/ZipArchive/ZipArchive

注意:有时需要添加 libz.tbd ( zlib.dylib 的新名称)到“链接二进制文件与库”

简单示例:

#import "SSZipArchive.h"
...
- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *docsDir;
    NSArray *dirPaths;
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSError *error;        
    NSString *str = @"Hello world";
    NSString *fileName = [docsDir stringByAppendingPathComponent:@"test.txt"];
    BOOL succeed = [str writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (succeed){
        NSString *path = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"test.zip"]];
        [SSZipArchive createZipFileAtPath:path withFilesAtPaths:@[fileName]];
    }
}

Here is what worked for me:
1) ZLib based Objective-Zip new location: https://github.com/gianlucabertani/Objective-Zip

Podfile:

pod 'objective-zip', '~> 1.0'

Quick example:

#import "ViewController.h"
#import "Objective-Zip.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *docsDir;
    NSArray *dirPaths;
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSString *path = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"test.zip"]];

    OZZipFile *zipFile= [[OZZipFile alloc] initWithFileName:path
                                                       mode:OZZipFileModeCreate];
    NSString *str = @"Hello world";
    OZZipWriteStream *stream= [zipFile writeFileInZipWithName:@"file.txt"
                                             compressionLevel:OZZipCompressionLevelBest];
    [stream writeData:[str dataUsingEncoding:NSUTF8StringEncoding]];
    [stream finishedWriting];
    [zipFile close];
}

2) Other zlib based library worked fine too. https://github.com/ZipArchive/ZipArchive

note: sometimes it's needed to add libz.tbd (new name of zlib.dylib) to "Link Binary With Libraries"

Quick example:

#import "SSZipArchive.h"
...
- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *docsDir;
    NSArray *dirPaths;
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSError *error;        
    NSString *str = @"Hello world";
    NSString *fileName = [docsDir stringByAppendingPathComponent:@"test.txt"];
    BOOL succeed = [str writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (succeed){
        NSString *path = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"test.zip"]];
        [SSZipArchive createZipFileAtPath:path withFilesAtPaths:@[fileName]];
    }
}
愁以何悠 2024-08-22 01:54:32

作为替代方案,还有 objective-zip,即“一个小 Cocoa/以面向对象的友好方式包装 ZLib 和 MiniZip 的 Objective-C 库。”

将文件写入“.zip”存档非常简单,只需执行以下代码即可:

ZipWriteStream *stream = [zipFile writeFileInZipWithName:@"abc.txt" compressionLevel:ZipCompressionLevelBest];
[stream writeData:abcData];
[stream finishedWriting];

该库还允许读取“.zip”文件的内容,并枚举它包含的文件。

列出“.zip”文件的内容是通过类似于以下代码的代码完成的。

ZipFile *unzipFile = [[ZipFile alloc] initWithFileName:@"test.zip" mode:ZipFileModeUnzip];
NSArray *infos = [unzipFile listFileInZipInfos];

for (FileInZipInfo *info in infos) {
  NSLog(@"- %@ %@ %d (%d)", info.name, info.date, info.size, info.level);

  // Locate the file in the zip
  [unzipFile locateFileInZip:info.name];

  // Expand the file in memory
  ZipReadStream *read = [unzipFile readCurrentFileInZip];
  NSMutableData *data = [[NSMutableData alloc] initWithLength:256];
  int bytesRead = [read readDataWithBuffer:data];
  [read finishedReading];
}

As alternative, there is also objective-zip, which is, "a small Cocoa/Objective-C library that wraps ZLib and MiniZip in an object-oriented friendly way."

Writing a file into a ".zip" archive is simple as executing the following code:

ZipWriteStream *stream = [zipFile writeFileInZipWithName:@"abc.txt" compressionLevel:ZipCompressionLevelBest];
[stream writeData:abcData];
[stream finishedWriting];

The library allows also to read the content of a ".zip" file, and to enumerate the files it contains.

Listing the content of a ".zip" file is done from code similar to the following one.

ZipFile *unzipFile = [[ZipFile alloc] initWithFileName:@"test.zip" mode:ZipFileModeUnzip];
NSArray *infos = [unzipFile listFileInZipInfos];

for (FileInZipInfo *info in infos) {
  NSLog(@"- %@ %@ %d (%d)", info.name, info.date, info.size, info.level);

  // Locate the file in the zip
  [unzipFile locateFileInZip:info.name];

  // Expand the file in memory
  ZipReadStream *read = [unzipFile readCurrentFileInZip];
  NSMutableData *data = [[NSMutableData alloc] initWithLength:256];
  int bytesRead = [read readDataWithBuffer:data];
  [read finishedReading];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文