-
Notifications
You must be signed in to change notification settings - Fork 199
Recipes
Glen Low edited this page Aug 14, 2014
·
17 revisions
Replacing an entry in an existing zip file:
ZZMutableArchive* archive = [ZZMutableArchive archiveWithContentsOfURL:
[NSURL fileURLWithPath:@"/tmp/old.zip"]];
NSMutableArray* entries = [archive.entries mutableCopy];
[entries replaceObjectAtIndex:replacingIndex
withObject:
[ZZArchiveEntry archiveEntryWithFileName:@"replacement.text"
compress:YES
dataBlock:^(NSError** error)
{
return [@"see you again, world"
dataUsingEncoding:NSUTF8StringEncoding];
}]];
[archive updateEntries:entries
error:nil];
Writing a new zip file containing a directory:
ZZMutableArchive* newArchive = [ZZMutableArchive archiveWithContentsOfURL:
[NSURL fileURLWithPath:@"/tmp/new.zip"]];
[newArchive updateEntries:
@[
[ZZArchiveEntry archiveEntryWithDirectoryName:@"folder/"],
[ZZArchiveEntry archiveEntryWithFileName:@"folder/first.text"
compress:YES
dataBlock:^(NSError** error)
{
return [@"hello, world" dataUsingEncoding:NSUTF8StringEncoding];
}]
]
error:nil];
Unzip an existing zip file to the file system:
NSFileManager* fileManager = [NSFileManager defaultManager];
NSURL* path = [NSURL fileURLWithPath:@"/tmp/old"];
ZZArchive* archive = [ZZArchive archiveWithContentsOfURL:[NSURL fileURLWithPath:@"/tmp/old.zip"]];
for (ZZArchiveEntry* entry in archive.entries)
{
NSURL* targetPath = [path URLByAppendingPathComponent:entry.fileName];
if (entry.fileMode & S_IFDIR)
// check if directory bit is set
[fileManager createDirectoryAtURL:targetPath
withIntermediateDirectories:YES
attributes:nil
error:nil];
else
{
// Some archives don't have a separate entry for each directory and just
// include the directory's name in the filename. Make sure that directory exists
// before writing a file into it.
[fileManager createDirectoryAtURL:[targetPath URLByDeletingLastPathComponent]
withIntermediateDirectories:YES
attributes:nil
error:nil];
[[entry newDataWithError:nil] writeToURL:targetPath
atomically:NO];
}
}