组合文件夹和文件,如zip

时间:2013-10-01 22:32:18

标签: xcode macos cocoa attributes zip

如何将文件夹和文件组合成一个像zip文件这样的对象呢?

假设您要创建一个与任何压缩工具相同的应用。您选择一个文件夹,它将其中的所有文件及其子文件夹组合到一个对象中。

您必须将文件夹结构和每个文件信息保存为某个值。但你会在哪里做到这一点。在完成的对象属性?

这方面的一些起点很棒,非常感谢!

非常感谢!

2 个答案:

答案 0 :(得分:1)

由于tar命令行工具可以按照您的意愿执行,并且您可以使用NSTask类运行任何命令行工具,这将是我指示您的方向。

答案 1 :(得分:1)

以下是实现相同的方法: -

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/bash"];
[task setArguments:[NSArray arrayWithObjects: @"-c", @"tar -xzf yourFile",nil]];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

NSData *data;
data = [file readDataToEndOfFile];
NSString *response = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"%@",response);