区别在于不是调用contentsOfDirectoryAtPath方法,而是调用subpathsAtPath方法,这会列出sourcePath下的所有文件和子目录,然后在下面的循环里,将文件写入压缩文件,不处理文件夹。注意newname要直接用subPath,这样会自动在压缩文件中保留子目录下的文件完整路径
NSArray *subPaths = [fileManager subpathsAtPath:sourcePath];// 关键是subpathsAtPath方法
for(NSString *subPath in subPaths){
NSString *fullPath = [sourcePath stringByAppendingPathComponent:subPath];
BOOL isDir;
if([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && !isDir)// 只处理文件
{
[zipArchive addFileToZip:fullPath newname:subPath];
}
}
本文介绍了一种使用subpathsAtPath方法而非contentsOfDirectoryAtPath来列出指定路径下所有文件及子目录的方法,并提供了如何仅将文件而非文件夹添加到压缩文件中的示例代码。
662

被折叠的 条评论
为什么被折叠?



