0

如何压缩NSDocumentDirectoryusing的子目录ZipArchive

4

1 回答 1

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");
于 2013-01-24T12:20:32.730 回答