Có lớp nào cho phép nén dữ liệu bằng Zlib hay đang sử dụng zlib.dylib trực tiếp khả năng duy nhất tôi có?Làm thế nào tôi có thể nén dữ liệu bằng Zlib mà không cần sử dụng trực tiếp zlib.dylib?
Trả lời
Nén NSData + là dễ sử dụng triển khai danh mục NSData.
Cách sử dụng:
NSData* compressed = [myData zlibDeflate];
NSData* originalData = [compressed zlibInflate];
Điều này, có thể: How to gzip Data in Memory Using Objective-C.
Cảm ơn bạn. Nó chứa mã mà tôi thực sự cần. Tôi không thấy một phần để lưu dữ liệu trong một tập tin, nhưng đó không phải là khó khăn để viết. – kiamlaluno
Như thay thế, đó cũng là objective-zip, đó là, "thư viện Cocoa nhỏ/Objective-C mà kết thúc tốt đẹp ZLib và MiniZip theo cách thân thiện với đối tượng. "
Viết một tập tin vào một ".zip" lưu trữ là đơn giản như thực hiện đoạn mã sau:
ZipWriteStream *stream = [zipFile writeFileInZipWithName:@"abc.txt" compressionLevel:ZipCompressionLevelBest];
[stream writeData:abcData];
[stream finishedWriting];
Thư viện cũng cho phép đọc nội dung của một ".zip" tập tin, và liệt kê các tập tin Nó chứa.
Liệt kê nội dung của tệp ".zip" được thực hiện từ mã tương tự như mã sau.
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];
}
Đây là những gì làm việc cho tôi: 1) ZLib dựa Objective-Zip mới vị trí: https://github.com/gianlucabertani/Objective-Zip
Podfile:
pod 'objective-zip', '~> 1.0'
nhanh Ví dụ:
#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) Thư viện dựa trên zlib khác cũng hoạt động tốt. https://github.com/ZipArchive/ZipArchive
lưu ý: đôi khi nó là cần thiết để thêm libz.tbd (tên mới của zlib.dylib) để "Liên kết Binary Với Libraries"
nhanh Ví dụ:
#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]];
}
}
Mã tôi tìm thấy trong liên kết mà bạn báo cáo hoàn chỉnh hơn, vì nó cũng bao gồm cả phần để tính tổng kiểm tra CRC32. – kiamlaluno
Tuyệt vời thanx. Ngoài ra, nó có thể được yêu cầu thay thế 'inflateInit (& strm)' trong '-zlibInflate' thành' inflateInit2 (& strm, 16 + MAX_WBITS) 'nếu bạn nhận được' Z_DATA_ERROR' (-3) trong khi cố gắng giải nén các tập tin được tạo bởi tiện ích bên ngoài. –
URL không tìm thấy, vui lòng cập nhật liên kết! Vẫn có liên quan ... –