2013-05-07 58 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文件夹。它工作。