【发布时间】:2012-02-02 19:58:44
【问题描述】:
在我的应用中,当用户进行应用内购买时,应用需要下载一个 zip 文件并将其解压缩到应用的文档文件夹中。 zip 文件下载并可以解压缩。我正在使用Objective Zip 解压缩存档。问题是当尝试为每个文件创建文件夹路径时,文件夹永远不会创建,并且没有错误。
这是发生这种情况的部分的一些示例代码:
// Create file manager
NSFileManager *fileMgr = [NSFileManager defaultManager];
//Unzip
NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
ZipFile *unzipFile = [[ZipFile alloc] initWithFileName:[applicationDocumentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",@"Mid America Oireachtas 2011"]] 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:info.length];
int bytesRead = [read readDataWithBuffer:data];
[read finishedReading];
NSString *appSupportFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *pathfull = [appSupportFolder stringByAppendingPathComponent:info.name];
NSString *path = [[pathfull stringByDeletingLastPathComponent] copy];
NSError *errorw;
NSRange range = [path rangeOfString:@"__MACOSX"];
if (range.location == NSNotFound) {
NSLog(@"last: %@", [path lastPathComponent]);
if ([fileMgr createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&errorw]) {
NSLog(@"Create Folder: %@", path);
NSLog(@"Directory Win: %@", errorw);
if (![[pathfull pathExtension] isEqualToString:@""] && ![[[pathfull lastPathComponent] substringToIndex:1] isEqualToString:@"." ]) {
[data writeToFile:pathfull atomically:NO];
}
}
else {
//NSLog(@"Create Folder: %@", path);
NSLog(@"Directroy Fail: %@", errorw);
}
}
}
[unzipFile close];
//delete zip
// For error information
NSError *error;
if ([fileMgr removeItemAtPath:[applicationDocumentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",@"Mid America Oireachtas 2011"]] error:&error] == YES) {
NSLog(@"File Deleted");
}
//delete zip
// For error information
NSError *error;
if ([fileMgr removeItemAtPath:[applicationDocumentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",@"My Zip"]] error:&error] == YES) {
NSLog(@"File Deleted");
}
这里是日志文件输出的相关sn-p:
2012-01-04 17:12:51.509 WhatsMyStageOn[3232:15503] Create Folder: /Users/Brandon/Library/Application Support/iPhone Simulator/5.0/Applications/436C2C49-D79B-4CDA-B0AA-15BB98F6F75E/WhatsMyStageOn.app/
2012-01-04 17:12:51.510 WhatsMyStageOn[3232:15503] Directory Win: (null)
2012-01-04 17:12:51.510 WhatsMyStageOn[3232:15503] Create Folder: /Users/Brandon/Library/Application Support/iPhone Simulator/5.0/Applications/436C2C49-D79B-4CDA-B0AA-15BB98F6F75E/WhatsMyStageOn.app/Documents/Mid America Oireachtas 2011
2012-01-04 17:12:51.510 WhatsMyStageOn[3232:15503] Directory Win: (null)
2012-01-04 17:12:51.510 WhatsMyStageOn[3232:15503] Create Folder: /Users/Brandon/Library/Application Support/iPhone Simulator/5.0/Applications/436C2C49-D79B-4CDA-B0AA-15BB98F6F75E/WhatsMyStageOn.app/Documents
2012-01-04 17:12:51.510 WhatsMyStageOn[3232:15503] Directory Win: (null)
2012-01-04 17:12:51.511 WhatsMyStageOn[3232:15503] Create Folder: /Users/Brandon/Library/Application Support/iPhone Simulator/5.0/Applications/436C2C49-D79B-4CDA-B0AA-15BB98F6F75E/WhatsMyStageOn.app/Documents/__MACOSX
2012-01-04 17:12:51.511 WhatsMyStageOn[3232:15503] Directory Win: (null)
2012-01-04 17:12:51.511 WhatsMyStageOn[3232:15503] Create Folder: /Users/Brandon/Library/Application Support/iPhone Simulator/5.0/Applications/436C2C49-D79B-4CDA-B0AA-15BB98F6F75E/WhatsMyStageOn.app/Documents/__MACOSX/Mid America Oireachtas 2011
2012-01-04 17:12:51.511 WhatsMyStageOn[3232:15503] Directory Win: (null)
2012-01-04 17:12:51.511 WhatsMyStageOn[3232:15503] Create Folder: /Users/Brandon/Library/Application Support/iPhone Simulator/5.0/Applications/436C2C49-D79B-4CDA-B0AA-15BB98F6F75E/WhatsMyStageOn.app/Documents/Mid America Oireachtas 2011
2012-01-04 17:12:51.512 WhatsMyStageOn[3232:15503] Directory Win: (null)
2012-01-04 17:12:51.544 WhatsMyStageOn[3232:15503] Create Folder: /Users/Brandon/Library/Application Support/iPhone Simulator/5.0/Applications/436C2C49-D79B-4CDA-B0AA-15BB98F6F75E/WhatsMyStageOn.app/Documents/Mid America Oireachtas 2011/Attractions
2012-01-04 17:12:51.544 WhatsMyStageOn[3232:15503] Directory Win: (null)
阅读 NSFileManager 文档,这是应该在日志中输出的内容,但是,没有创建文件夹。
编辑:修复了不使用文档的问题,仍然存在与编辑前相同的问题。
编辑:将代码更新为工作解决方案!
【问题讨论】:
标签: objective-c ios cocoa nsfilemanager