开发者

create zip archive using NSTask containing a first level folder with the files

开发者 https://www.devze.com 2023-02-18 08:47 出处:网络
my method works for zipping files from a temporary directory previously created and populated: NSURL *destURL = self.archiveDestURL;

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];

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 wri开发者_运维技巧te my files there, but what is the zip argument for having a folder be the top level in the created archive?

i didn't see this in man zip .


This will help you.

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];


instead of using NSTask, you could incorporate compress functionality into your code. there are several options.

  1. ZipBrowser from apple.com
  2. adding a category to NSData, as in here
  3. a similar question. How can I create a zip file by using Objective C?
0

精彩评论

暂无评论...
验证码 换一张
取 消