使用ZipArchive在NSDocumentDirectory中的Zip子目录/子文件夹

时间:2012-10-18 12:59:09

标签: ios zip

如何使用NSDocumentDirectory压缩ZipArchive的子目录?

1 个答案:

答案 0 :(得分:0)

BOOL isDir=NO;
NSArray *subpaths;
NSFileManager *fileManager = [NSFileManager defaultManager];    
if ([fileManager fileExistsAtPath:dataPath isDirectory:&isDir] && isDir)
{
    subpaths = [fileManager subpathsAtPath:dataPath];
}

NSString *archivePath = [dataPath stringByAppendingString:@".zip"];

ZipArchive *archiver = [[ZipArchive alloc] init];
[archiver CreateZipFile2:archivePath];

for(NSString *path in subpaths)
{
    NSString *longPath = [dataPath stringByAppendingPathComponent:path];

    if ([fileManager fileExistsAtPath:longPath isDirectory:&isDir] && !isDir)
    {
        [archiver addFileToZip:longPath newname:path];      
    }

}

BOOL successCompressing = [archiver CloseZipFile2];
if(successCompressing)
    NSLog(@"Successfully Zipped");
else
    NSLog(@"Fail");