我正在使用一个名为objective zip的库来提取文件。
相关代码:
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];
第一个nslog输出以下内容:
- _some-path_/modules/reference-popup/ref-popup.js 2012-08-21 08:49:36 +0000 109 (-1)
第二个nslog输出如下:
/Users/_USER_/Library/Application Support/iPhone Simulator/5.1/Applications/F2649DB7-7806-4C49-8DF9-BD939B2A9D5A/Documents/_some-path_/modules/slide-popup/slide-popup.css
基本上我认为我的读取和数据对象并没有相互通信...当我在浏览器中导航到正确的目录时,它所做的就是显示两个文件,它们应该是目录(它们是大多数顶级目录)但浏览器说它们被写成1kb文件。我尝试查看文件及其256字节文件...根据代码中的缓冲区。
我该怎么做?
扩展zip(将所有文件正确放入目录中)?
答案 0 :(得分:10)
调用readDataWithBuffer将填充缓冲区(达到其长度),然后暂停流。你应该把它放在循环中并在bytesRead为0时停止。这是为了允许扩展文件大于可用内存(例如,想想视频)。
另外,请考虑zip文件没有目录结构:目录只是作为压缩文件名的一部分嵌入。由您来重建正确的路径。通常您有一些基本目录,附加压缩文件名并使用生成的完整路径创建文件。
在Objective-Zip wiki中,最后,您可以找到标题为“内存管理”的部分,其中存在样本循环并进行评论。更完整的示例如下:
// Open zip descriptor
ZipFile *zip= [[ZipFile alloc] initWithFileName:zipPath mode:ZipFileModeUnzip];
NSMutableData *buffer= [[NSMutableData alloc] initWithLength:BUFFER_SIZE];
// Loop on file list
NSArray *zipContentList= [zip listFileInZipInfos];
for (FileInZipInfo *fileInZipInfo in zipContentList) {
// Check if it's a directory
if ([fileInZipInfo.name hasSuffix:@"/"]) {
NSString *dirPath= [documentsPath stringByAppendingPathComponent:fileInZipInfo.name];
[[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:NULL];
continue;
}
// Create file
NSString *filePath= [documentsPath stringByAppendingPathComponent:fileInZipInfo.name];
[[NSFileManager defaultManager] createFileAtPath:filePath contents:[NSData data] attributes:nil];
NSFileHandle *file= [NSFileHandle fileHandleForWritingAtPath:filePath];
// Seek file in zip
[zip locateFileInZip:fileInZipInfo.name];
ZipReadStream *readStream= [zip readCurrentFileInZip];
// Reset buffer
[buffer setLength:BUFFER_SIZE];
// Loop on read stream
int totalBytesRead= 0;
do {
int bytesRead= [readStream readDataWithBuffer:buffer];
if (bytesRead > 0) {
// Write data
[buffer setLength:bytesRead];
[file writeData:buffer];
totalBytesRead += bytesRead;
} else
break;
} while (YES);
// Close file
[file closeFile];
[readStream finishedReading];
}
// Close zip and release buffer
[buffer release];
[zip close];
[zip release];
希望这有帮助。