我正在開發一個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