在 Objective C for iPhone 中创建 zip 文件
Posted
技术标签:
【中文标题】在 Objective C for iPhone 中创建 zip 文件【英文标题】:Creating zip files in ObjectiveC for iPhone 【发布时间】:2010-01-29 10:41:51 【问题描述】:是否可以在 Objective C 中创建 .zip 文件?
任何可用的库或建议?
【问题讨论】:
复制:create-a-zip-archive-from-a-cocoa-application:***.com/questions/1928162/… 您真的要创建一个 zip 文件还是只是压缩一些数据?您可以使用现有的zlib 库非常轻松地压缩数据。 也是***.com/questions/286496/…的副本 也许可以看看这个创建 ZIP 文件的内置方法:***.com/a/32723162 【参考方案1】:首先你从http://code.google.com/p/objective-zip/downloads/list下载Objective-zip示例
在本例中找到并复制三个文件夹 Objective-Zip、MiniZip 和 ZLib 拖入你的项目中
在你的 .m 类中导入两个类 “ZipFile.h”和 "ZipWriteStream.h"
zip 我的代码的创建方法是:-
-(IBAction)Zip
self.fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory , NSUserDomainMask, YES);
NSString *ZipLibrary = [paths objectAtIndex:0];
NSString *fullPathToFile = [ZipLibrary stringByAppendingPathComponent:@"backUp.zip"];
//[self.fileManager createDirectoryAtPath:fullPathToFile attributes:nil];
//self.documentsDir = [paths objectAtIndex:0];
ZipFile *zipFile = [[ZipFile alloc]initWithFileName:fullPathToFile mode:ZipFileModeCreate];
NSError *error = nil;
self.fileManager = [NSFileManager defaultManager];
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
self.documentsDir = [paths1 objectAtIndex:0];
NSArray *files = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:self.documentsDir error:&error];
//for(NSString *filename in files)
for(int i = 0;i<files.count;i++)
id myArrayElement = [files objectAtIndex:i];
if([myArrayElement rangeOfString:@".png" ].location !=NSNotFound)
NSLog(@"add %@", myArrayElement);
NSString *path = [self.documentsDir stringByAppendingPathComponent:myArrayElement];
NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:&error];
NSDate *Date = [attributes objectForKey:NSFileCreationDate];
ZipWriteStream *streem = [zipFile writeFileInZipWithName:myArrayElement fileDate:Date compressionLevel:ZipCompressionLevelBest];
NSData *data = [NSData dataWithContentsOfFile:path];
// NSLog(@"%d",data);
[streem writeData:data];
[streem finishedWriting];
else if([myArrayElement rangeOfString:@".txt" ].location !=NSNotFound)
NSString *path = [self.documentsDir stringByAppendingPathComponent:myArrayElement];
NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:&error];
NSDate *Date = [attributes objectForKey:NSFileCreationDate];
ZipWriteStream *streem = [zipFile writeFileInZipWithName:myArrayElement fileDate:Date compressionLevel:ZipCompressionLevelBest];
NSData *data = [NSData dataWithContentsOfFile:path];
// NSLog(@"%d",data);
[streem writeData:data];
[streem finishedWriting];
[self testcsv];
[zipFile close];
您的文档目录保存的项目 .png 和 .txt 文件使用 backup.zip 压缩到库文件夹中 我希望这会有所帮助
【讨论】:
【参考方案2】:在有人提到http://code.google.com/p/ziparchive/ 之前。我评估了那个代码,它非常糟糕。我最终将它用于必须做的快速演示黑客,但我永远不会在生产中使用它。 ZipKit http://bitbucket.org/kolpanic/zipkit/wiki/Home 的状态似乎好多了。
【讨论】:
【参考方案3】:NSString *stringPath1 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]];
NSString *FileName=[stringPath1 stringByAppendingPathComponent:@"Your file name"];
NSString *stringPath=[stringPath1 stringByAppendingPathComponent:[@"Your file name" stringByAppendingFormat:@".zip"]];
NSArray *files = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:FileName error:&error];
ZipFile *zipFile = [[ZipFile alloc]initWithFileName:stringPath mode:ZipFileModeCreate];
for(int i = 0;i<files.count;i++)
id myArrayElement = [files objectAtIndex:i];
NSLog(@"add %@", myArrayElement);
NSString *path = [FileName stringByAppendingPathComponent:myArrayElement];
NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:&error];
NSDate *Date = [attributes objectForKey:NSFileCreationDate];
ZipWriteStream *streem = [zipFile writeFileInZipWithName:myArrayElement fileDate:Date compressionLevel:ZipCompressionLevelBest];
NSData *data = [NSData dataWithContentsOfFile:path];
[streem writeData:data];
[streem finishedWriting];
[zipFile close];
【讨论】:
以上是关于在 Objective C for iPhone 中创建 zip 文件的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Objective C 在 iphone 中制作 .zip 文件?
在 iPhone Objective C 中的 UIDatePicker 中禁用过去日期
音频文件可以像照片和电影一样使用objective C保存在iphone中吗?