【发布时间】:2019-01-07 21:17:08
【问题描述】:
我正在开发一个 zip 提取器应用程序,我遵循 CRD 解释的算法@Here,但我停留在第三步,我无法重命名临时目录中的解压缩文件。 这是我的代码
NSURL *tempDir = [NSURL fileURLWithPath:destinationPath];
NSError *error;
NSURL *tmpDirectory = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:tempDir create:YES error:&error];
if (error) {
return ;
}
tmpDirectory = [tmpDirectory URLByAppendingPathComponent:@"extracts"];
NSLog(@"temp dir %@",tmpDirectory);
NSLog(@"temp path %@",tmpDirectory.path);
[SSZipArchive unzipFileAtPath:zipFilePath toDestination:tmpDirectory.path];
NSArray *dirFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tmpDirectory.path error:nil];
NSLog(@"dir file %@",dirFiles);
for (NSString *string in dirFiles) {
NSArray *dirDestinationFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:destinationPath error:nil];
NSLog(@"dir destination file %@",dirDestinationFiles);
[dirDestinationFiles enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error;
if ([string isEqualToString:obj]) {
NSLog(@"Already present");
BOOL isMoved = [fm moveItemAtPath:tmpDirectory.path toPath:[destinationPath stringByAppendingString:[NSString stringWithFormat:@"/%@-1",string]] error:&error];
if (isMoved) {
NSLog(@"Moved");
}else{
NSLog(@"errorL %@", error);
NSLog(@"Not moved");
}
[fm removeItemAtPath:tmpDirectory.path error:&error];
[self moveFileToTrash:zipFilePath];
[self openExtractedFolderWithZipPath:zipFilePath toDestinationPath:destinationPath];
}
}];
}
任何建议.. 提前致谢!
【问题讨论】:
-
到底发生了什么? ` NSLog(@"errorL %@", error);` 是否输出了一些东西?
tmpDirectory有错误吗? -
@Larme 没有错误输出变得像Cannot make directory /Users/Prem/Downloads: File exists
-
解压缩文件可能已将一个或多个项目放入您的临时目录中,您需要将这些项目中的每一个移动到目录
destinationPath中。您的代码正在尝试复制临时目录本身,而不是其内容。要移动每个项目,您必须处理名称冲突,为此请尝试移动,如果您收到指示名称冲突的错误,请根据您的喜好修改目标名称并重试移动,重复直到成功或达到某个限制尝试次数(由您决定)。 -
@CRD 代码已编辑,如您所见,我可以再创建一个实例( BOOL isMoved = [fm moveItemAtPath:tmpDirectory.path toPath:[destinationPath stringByAppendingString:[NSString stringWithFormat:@"/%@-1 ",string]] error:&error]; ) 这个硬编码我如何动态地制作多个。
标签: objective-c macos cocoa unzip