我有一个压缩文件,想要提取它的内容。我应该采取什么确切的步骤来实现这一目标?是否有在Cocoa框架或Objective-C中解压文件的框架。
如果你使用的是iOS系统或者不想使用NSTask等工具,我推荐使用我的库SSZipArchive。
使用方法:
NSString *path = @"path_to_your_zip_file";
NSString *destination = @"path_to_the_folder_where_you_want_it_unzipped";
[SSZipArchive unzipFileAtPath:path toDestination:destination];
unzip
命令行工具,并使用 NSTask
:- (void) unzip {
NSFileManager* fm = [NSFileManager defaultManager];
NSString* zipPath = @"myFile.zip";
NSString* targetFolder = @"/tmp/unzipped"; //this it the parent folder
//where your zip's content
//goes to (must exist)
//create a new empty folder (unzipping will fail if any
//of the payload files already exist at the target location)
[fm createDirectoryAtPath:targetFolder withIntermediateDirectories:NO
attributes:nil error:NULL];
//now create a unzip-task
NSArray *arguments = [NSArray arrayWithObject:zipPath];
NSTask *unzipTask = [[NSTask alloc] init];
[unzipTask setLaunchPath:@"/usr/bin/unzip"];
[unzipTask setCurrentDirectoryPath:targetFolder];
[unzipTask setArguments:arguments];
[unzipTask launch];
[unzipTask waitUntilExit]; //remove this to start the task concurrently
}
那是一个快速而粗糙的解决方案。在实际生活中,您可能希望进行更多的错误检查,并查看unzip
manpage以获取高级参数。
这是一份类似于Sven Driemecker答案的Swift 4版本。
func unzipFile(at sourcePath: String, to destinationPath: String) -> Bool {
let process = Process.launchedProcess(launchPath: "/usr/bin/unzip", arguments: ["-o", sourcePath, "-d", destinationPath])
process.waitUntilExit()
return process.terminationStatus <= 1
}
return process.terminationStatus == 0
,以不接受警告。Pipe
实例捕获进程的输出。func unzipFile(at sourcePath: String, to destinationPath: String) -> Bool {
let process = Process()
let pipe = Pipe()
process.executableURL = URL(fileURLWithPath: "/usr/bin/unzip")
process.arguments = ["-o", sourcePath, "-d", destinationPath]
process.standardOutput = pipe
do {
try process.run()
} catch {
return false
}
let resultData = pipe.fileHandleForReading.readDataToEndOfFile()
let result = String (data: resultData, encoding: .utf8) ?? ""
print(result)
return process.terminationStatus <= 1
}
如果您只想解压文件,我建议使用NSTask
来调用unzip(1)。在解压之前,将文件复制到您控制的目录(可能在/tmp中)可能是明智的。
NSTemporaryDirectory()
代替/tmp
,但要小心,它的行为有点像/var/tmp
。 它会被清理掉,但不一定会在重新启动时清理掉。 - dreamlax+ (void)unzip {
NSString *targetFolder = @"/tmp/unzipped";
NSString* zipPath = @"/path/to/my.zip";
NSTask *task = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/unzip" arguments:@[@"-o", zipPath, @"-d", targetFolder]];
[task waitUntilExit];
}
-d 指定了目标目录,如果不存在则 unzip 将创建该目录。
-o 告诉 unzip 覆盖现有文件(但不删除过时的文件,所以请注意)。
这没有错误检查等内容,但它是一个简单快速的解决方案。
-openFile:
(NSWorkspace
) 是我所知道的最简单的方法。