I am using a library called objective zip to extract a file.
the relevant code:
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:requestFinishedPresentationPath mode:ZipFileModeUnzip];
NSArray *infos= [unzipFile listFileInZipInfos];
for (FileInZipInfo *info in infos) {
NSLog(@"- %@ %@ %d (%d)", info.name, info.date, info.size, info.level);
[unzipFile locateFileInZip:info.name];
// Expand the file in memory
ZipReadStream *read= [unzipFile readCurrentFileInZip];
NSMutableData *data = [[NSMutableData alloc] initWithLength:256];
int bytesRead = [read readDataWithBuffer:data];
[data writeToFile:[documentsDir stringByAppendingPathComponent:info.name] atomically:NO];
NSLog([documentsDir stringByAppendingPathComponent:info.name]);
[read finishedReading];
}
[unzipFile close];
the first nslog outputs the following:
- _some-path_/modules/reference-popup/ref-popup.js 2012-08-21 08:49:36 +0000 109 (-1)
the second nslog outputs the follows:
/Users/_USER_/Library/Application Support/iPhone Simulator/5.1/Applications/F2649DB7-7806-4C49-8DF9-BD939B2A9D5A/Documents/_some-path_/modules/slide-popup/slide-popup.css
Basically i think my read and data objects are not talking to each other… when I navigate to the right directory in my browser, all it does is show the two files, which are supposed to be directories (they are the most top-level directories) but the browser says they were written out as 1kb files. I try and view the file and its a 256 byte file… as per the buffer in the code.
What should I do instead?
To expand a zip (with all the files into a directory properly)?
Calling readDataWithBuffer will fill up the buffer (up to its length) and then pause the stream. You should actually put it in a loop and stop when bytesRead is 0. This is to allow expanding files bigger than available memory (think of a video, for example).
Also, consider that zip files have no directory structure: directories are simply embedded as part of the zipped file names. It’s up to you to recreate the correct path. Usually you have some base directory, append the zipped file name and create the file with the resulting complete path.
In the Objective-Zip wiki, at the end, you can find a section titled Memory Management where a sample loop is present and commented. A more completed example is the following:
Hope this helps.