在我的应用程序中,当用户购买应用程序时,应用程序需要下载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");
}
以下是日志文件的相关输出片段:
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文档,这是应该在日志中输出的内容,但是,没有创建文件夹。
编辑:修正了不使用文档问题,仍然有与编辑前谴责相同的问题。
编辑:更新了工作解决方案的代码!
答案 0 :(得分:5)
你不应该写应用程序包本身。试试这个:
NSString *appSupportFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [appSupportFolder stringByAppendingPathComponent:info.name];
答案 1 :(得分:3)
我认为您无法直接在WhatsMyStageOn.app/内创建目录,请尝试在Documents目录中创建目录。
NSMutableString *path = [info.name mutableCopy];
path = [[path stringByDeletingLastPathComponent] mutableCopy];
path = [NSMutableString stringWithFormat:@"%@/%@", applicationDocumentsDir, path];
底部有一行删除了Documents中的正确文件路径,但您的原始路径变量指向bundle目录,而不是Documents目录。