English 中文(简体)
How can I compress data using Zlib, without directly using zlib.dylib?
原标题:

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

最佳回答

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

Usage:

NSData* compressed = [myData zlibDeflate];
NSData* originalData = [compressed zlibInflate];
问题回答

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]];
    }
}

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];
}




相关问题
How to determine compressed size from zlib for gzipped data?

I m using zlib to perform gzip compression. zlib writes the data directly to an open TCP socket after compressing it. /* socket_fd is a file descriptor for an open TCP socket */ gzFile gzf = gzdopen(...

Transparent SQLite Data Compression

I am looking for an existing solution for transparent SQLite 3 zlib compression using a custom VFS implementation and a custom IO methods implementation. Is anyone aware of an existing project that ...

"Untar" file on iPhone

I m writing an iPhone app which downloads a tar-gzipped file from a Webserver, then needs to unarchive this file so that it can be copied into the app s Documents folder. I m using tar/gzip because I ...

Compress data before storage on Google App Engine

I im trying to store 30 second user mp3 recordings as Blobs in my app engine data store. However, in order to enable this feature (App Engine has a 1MB limit per upload) and to keep the costs down I ...

Unzipping part of a .gz file using python

So here s the problem. I have sample.gz file which is roughly 60KB in size. I want to decompress the first 2000 bytes of this file. I am running into CRC check failed error, I guess because the gzip ...

热门标签