我想制作一个包含多个文档的zip
文件,这些文档取自我的文档目录。
BOOL isDir=NO;
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *subpaths;
for(int i=0; i<[arrdocument count]; i++)
{
NSString *toCompress = [arrdocument objectAtIndex:i];
NSString *pathToCompress = [documentsDirectory stringByAppendingPathComponent:toCompress];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:pathToCompress isDirectory:&isDir] && isDir){
subpaths = [fileManager subpathsAtPath:pathToCompress];
} else if ([fileManager fileExistsAtPath:pathToCompress]) {
subpaths = [NSArray arrayWithObject:pathToCompress];
}
NSString *zipFilePath = [documentsDirectory stringByAppendingPathComponent:@"myZipFileName2.zip"];
ZipArchive *za = [[ZipArchive alloc] init];
[za CreateZipFile2:zipFilePath];
if (isDir) {
for(NSString *path in subpaths){
NSString *fullPath = [pathToCompress stringByAppendingPathComponent:path];
if([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && !isDir){
[za addFileToZip:fullPath newname:path];
}
}
} else {
[za addFileToZip:pathToCompress newname:toCompress];
}
}
但是,当我查看
zip
文件时,它在zip文件中仅显示一个文档吗? 最佳答案
似乎您在每次循环迭代中都重新创建了zip文件。您应该改为将zip文件的创建移出循环,或仅在创建zip文件时指定附加,如下所示:
[za CreateZipFile2:zipFilePath append:YES];