We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this questionI'm looking for exactly what the title says- a Cocoa library with the ability to list files in a ZIP, and extract single files upon request. These features are 'unzip -l' and 'unzip $archive $filename' commands respectively- I'm looking for a framework/API to do this, something like Java's 'java.util.zip'.
It seems that the logical fallback is to use NSTask to call the command line 'unzip app', and am willing to use this. However, I thought it might be better to look for a non-NSTask solution first.
http://code.google.com/p/objective-zip/
Read file out
ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:@"test.zip" mode:ZipFileModeUnzip];
[unzipFile goToFirstFileInZip];
ZipReadStream *read= [unzipFile readCurrentFileInZip];
NSMutableData *data= [[NSMutableData alloc] initWithLength:256];
int bytesRead= [read readDataWithBuffer:data];
[read finishedReading];
[zipFile close];
List files inside
ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:@"test.zip" mode:ZipFileModeUnzip];
NSArray *infos= [unzipFile listFileInZipInfos];
for (FileInZipInfo *info in infos) {
NSLog(@"- %@ %@ %d (%d)", info.name, info.date, info.size, info.level);
// Locate the file in the zip
[unzipFile locateFileInZip:info.name];
// Expand the file in memory
ZipReadStream *read= [unzipFile readCurrentFileInZip];
NSMutableData *data= [[NSMutableData alloc] initWithLength:256];
int bytesRead= [read readDataWithBuffer:data];
[read finishedReading];
}
[zipFile close];
Note about direcotry structure
Please note that inside the zip files there is no representation of a file-folder hierarchy: it is simply embedded in file names (i.e.: a file with a name like x/y/z/file.txt
). It is up to the program that extracts the files to consider these file names as expressing a structure and rebuild it on the file system (and viceversa during creation). Common zippers/unzippers simply follow this rule.
from manual
See LOZIPFileWrapper:
LOZIPFileWrapper *zipFileWrapper = [[LOZIPFileWrapper alloc] initWithURL:URL password:nil error:NULL];
NSArray *contentsOfZipArchive = [zipFileWrapper contentOfZIPFileIncludingFolders:YES error:NULL];
NSData *content = [zipFileWrapper contentsAtPath:contentsOfZipArchive[0] error:NULL];
You could use Marek Sebera's idea, but you could start by listing the zip file contents:
Idea One:
- just get a list of the raw titles (use the above idea) and have a UITableView
- When a title is selected, get the file's number in the zip from the uitableview, then unarchive it
Example of filenames:
lol.txt
path/hello.doc
path/dir/lol/file.txt
Idea Two:
- unarchive to somewhere (like the library dir)
- when the user finishes, re-compress
精彩评论