使用包含文件的第一级文件夹的 NSTask 创建 zip 存档 [英] create zip archive using NSTask containing a first level folder with the files

查看:30
本文介绍了使用包含文件的第一级文件夹的 NSTask 创建 zip 存档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的方法适用于从先前创建和填充的临时目录中压缩文件:

my method works for zipping files from a temporary directory previously created and populated:

NSURL *destURL = self.archiveDestURL;
NSTask *task = [[NSTask alloc] init];
[task setCurrentDirectoryPath:[srcURL path]]; 
[task setLaunchPath:@"/usr/bin/zip"];
NSArray *argsArray = [NSArray arrayWithObjects:@"-r", @"-q", [destURL path], @".", @"-i", @"*", nil];
[task setArguments:argsArray];
[task launch];
[task waitUntilExit];

但我想在解压后得到一个包含文件的文件夹.确定我可以在 tempDir 中创建一个文件夹并将我的文件写入那里,但是让文件夹成为创建的存档中的顶级文件夹的 zip 参数是什么?

but what i'd like to have when unzipped, is a folder with the files. sure i can make a folder in the tempDir and write my files there, but what is the zip argument for having a folder be the top level in the created archive?

我在 man zip 中没有看到这个.

i didn't see this in man zip .

推荐答案

这会对你有所帮助.

NSTask *unzip = [[NSTask alloc] init];
[unzip setLaunchPath:@"/usr/bin/unzip"];
[unzip setArguments:[NSArray arrayWithObjects:@"-u", @"-d", 
                     destination, zipFile, nil]];

NSPipe *aPipe = [[NSPipe alloc] init];
[unzip setStandardOutput:aPipe];

[unzip launch];
[unzip waitUntilExit];

这篇关于使用包含文件的第一级文件夹的 NSTask 创建 zip 存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆