我正在开发一个iOS应用程序,并试图压缩我在应用程序中创建的文件,是否有任何内置函数能够做到这一点?如何使用Objective C创建zip文件?
回答
亚历克斯指出,我通过指出NSData category贡献的Cocoadev维基的用户回应this question。该类别包括处理NSData实例中的压缩和压缩数据的方法(可以从Zip文件读取或写入一个)。这应该是您实现所描述的压缩文件所需的一切,只要您可以将文件数据提供给NSData实例即可。
有关此类别实例的示例,请参阅我的iPhone应用程序Molecules的源代码。我只使用该方法从gzip文件中提取数据(在SLSMolecule + PDB.m中),但您应该能够从中获得基本概念。
到NSData类别的链接已中断。 – 2013-12-08 12:31:44
@GlennHowes - 它可以在这里的网页存档:http://web.archive.org/web/20100102154917/http://cocoadev.com/index.pl?NSDataCategory,但我编辑我的旧问题,包括整体的类别在这里保存它。 – 2013-12-08 23:43:50
不确定是否有内置函数,但也许可以使用外部库,例如InfoZip。或者,您也可以尝试实施您自己的zip库,我非常确定Zip格式规范可以在互联网上找到。
RWendi
我做它。不是火箭科学,而是一大堆枯燥乏味的东西。 – 2012-12-19 21:29:41
首先你从http://code.google.com/p/objective-zip/downloads/list
在这个例子中下载的Objective-ZIP例如查找和将三个文件夹的Objective-ZIP,MiniZip和zlib拖动到您的项目
进口二类你.M类 “ZipFile.h” 和 “ZipWriteStream.h”
创建压缩我的代码的方法是: -
-(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 我希望这是帮助
我绝对推荐的Objective-邮编。这只是最近搬到https://github.com/flyingdolphinstudio/Objective-Zip:
创建一个ZIP文件:
ZipFile *zipFile= [[ZipFile alloc] initWithFileName:@"test.zip" mode:ZipFileModeCreate];
将文件添加到压缩文件:
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];
- 1. 如何使用C#创建一个zip文件?
- 2. 创建zip文件时使用gulp-vinyl-zip创建TypeError
- 3. 从内存流C创建Zip文件#
- 4. 使用Android创建ZIP文件
- 5. 使用PHP创建一个zip文件
- 6. 使用SharpZipLib创建Zip文件
- 7. 如何在Asp.Net中创建Zip文件?
- 8. Gradle zip插件,如何在zip中创建空文件夹?
- 9. 从zip创建文件 - android
- 10. 如何用Swift和Objective-C创建XML文件?
- 11. 如何使用c#创建.dbf文件?
- 12. 用c文件夹结构创建zip文件#
- 13. Objective-C使用字符串创建文本文件
- 14. 如何使用密码创建zip文件
- 15. 如何使用shell脚本创建zip文件?
- 16. 如何使用Gradle动态创建多个ZIP文件?
- 17. 如何使用Objective-C创建文字XML字符串?
- 18. 使用adm-zip从JSON对象创建zip文件
- 19. 使用perl创建其他zip文件的zip集合
- 20. 创建使用Go的存档Unicode文件名zip存档/ ZIP
- 21. 创建的.zip文件
- 22. C#如何从3个目录内容创建zip文件
- 23. Objective-c:获取文件创建日期
- 24. 在Objective-C中创建文件
- 25. 如何在不创建ZIP文件的情况下在Android中创建ZIP InputStream?
- 26. 从多个文件创建zip文件
- 27. 如何使用SevenZipSharp创建SFX ZIP?
- 28. 如何使用PowerShell创建zip存档?
- 29. 在C中创建一个文件夹的Zip文件夹#
- 30. 创建一个Objective-C文件创建事件流
你需要什么?只是压缩并发送文件的地方?如果是这样,您可以使用已包含的zlib。 – 2008-11-13 08:32:45
请检查您在[这里]答案[1] [1]:http://stackoverflow.com/questions/7386695/how-to-zip-file/18740716#18740716 – Nirmalsinh 2013-09-11 12:09:27