2013-05-07 37 views
1

我在需要壓縮的文檔目錄中有一個文件夾。我能夠壓縮普通文件,但我無法壓縮文件夾。如何在iPhone中使用zipArchive壓縮文件夾?

我提到以下鏈接 How to zip folders in iPhone SDK? 但這裏的文件夾中的文件已壓縮separately.I想壓縮,而不必應付探索的內容(文件/文件夾)的目錄中,並荏苒整個文件夾他們一個接一個。

是否有可能使用ZipArchive庫做到這一點。如果有人可以通過發佈必要的代碼來解釋?

謝謝。

回答

4

你不能做到這一點與ZipArchive但SSZipArchive看看它有一個+ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath; 方法你可以使用。

+0

嘿即時通訊能夠使用這個庫來壓縮文件夾,但是當文件夾的文件夾內僅解壓縮的內容(文件/ subfolers)帶回。該文件夾壓縮不會回來..例如,如果bcd是文件夾A內的文件,並且我壓縮文件夾A,則文件夾A在解壓縮後不會被取回。這是否合理? – 2013-05-08 17:45:47

+0

@ Mr.匿名有一個拉動請求,以提高回購的文件夾壓縮功能,https://github.com/soffes/ssziparchive/pull/49 – 2013-05-08 17:50:23

0

您只需在將示例中的所有文件添加到zip之前獲取dir的內容即可。逐個添加它們在代碼中是一樣的,所以只需獲取一個列表然後遍歷列表。

NSString *bundleRoot = [[NSBundle mainBundle] bundlePath]; 
NSFileManager *fm = [NSFileManager defaultManager]; 
NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil]; 

如果您正在尋找特定文件,你也可以使用一個謂詞過濾結果

NSPredicate *filter = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"]; 
NSArray *pngs = [dirContents filteredArrayUsingPredicate:filter]; 
+0

這不會產生ZIP文件的目錄 – ngb 2013-12-04 04:49:18

2
// Path to store Zip  

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString* dPath = [paths objectAtIndex:0]; 

NSString* zipfile = [dPath stringByAppendingPathComponent:@"test.zip"] ; 

// File Tobe Added in Zip 

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"GetAllCardList" ofType:@"xml"]; 

NSString *fileName = @"MyFile"; // Your New ZipFile Name 

ZipArchive* zip = [[ZipArchive alloc] init]; 
if([zip CreateZipFile2:zipfile]) 
{ 
    NSLog(@"Zip File Created"); 
    if([zip addFileToZip:filePath newname:[NSString stringWithFormat:@"%@.%@",fileName,[[filePath lastPathComponent] pathExtension]]]) 
    { 
     NSLog(@"File Added to zip"); 
    } 
} 
+0

這不會回答這個問題。他想壓縮一個文件夾,而不是單個文件 – ngb 2013-12-04 04:48:45

1

嘗試SSZipArchive

+ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath keepParentDirectory:(BOOL)keepParentDirector; 

我用

let success = SSZipArchive.createZipFileAtPath(zipFullFilename, withContentsOfDirectory:dataDir, keepParentDirectory:true) 

創建ZIP文件夾。它工作。

相關問題