我正在开发一个 iOS 应用程序,并试图压缩我在应用程序中创建的文件,是否有任何内置函数能够做到这一点?
3 回答
我绝对推荐 Objective-Zip。它最近才转移到https://github.com/flyingdolphinstudio/Objective-Zip:
他们文档中的一些示例:
创建压缩文件:
ZipFile *zipFile= [[ZipFile alloc] initWithFileName:@"test.zip" mode:ZipFileModeCreate];
将文件添加到 zip 文件:
ZipWriteStream *stream= [zipFile writeFileInZipWithName:@"abc.txt" compressionLevel:ZipCompressionLevelBest];
[stream writeData:abcData];
[stream finishedWriting];
从 zip 文件中读取文件:
ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:@"test.zip" mode:ZipFileModeUnzip];
[unzipFile goToFirstFileInZip];
ZipReadStream *read= [unzipFile readCurrentFileInZip];
NSMutableData *data= [[NSMutableData alloc] initWithLength:256];
int bytesRead= [read readDataWithBuffer:data];
[read finishedReading];
正如 Alex 指出的那样,我通过指出Cocoadev wiki 用户贡献的NSData 类别来回答这个问题。该类别包括处理 NSData 实例中的压缩和 gzip 压缩数据的方法(可以从 Zip 文件读取或写入其中)。这应该是实现您描述的文件压缩所需的全部,只要您可以将文件数据提供给 NSData 实例。
有关此类别的实际示例,请参阅我的 iPhone 应用程序Molecules的源代码。我只使用该方法从压缩文件中提取数据(在 SLSMolecule+PDB.m 中),但您应该能够从中获得基本概念。
首先从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 压缩到库文件夹中,我希望这会有所帮助