gpt4 book ai didi

objective-c - NSFileManager createDirectoryAtPath :withIntermediateDirectories: Not creating directories or creating errors

转载 作者:可可西里 更新时间:2023-11-01 05:15:33 25 4
gpt4 key购买 nike

在我的应用中,当用户进行应用内购买时,应用需要下载一个 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 文档,这是应该在日志中输出的内容,但是,没有创建文件夹。

编辑:修复了不使用文档的问题,仍然存在与编辑前谴责的相同问题。

编辑:更新了工作解决方案的代码!

最佳答案

您不应该写入应用程序的 bundle 本身。试试这个:

NSString *appSupportFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [appSupportFolder stringByAppendingPathComponent:info.name];

关于objective-c - NSFileManager createDirectoryAtPath :withIntermediateDirectories: Not creating directories or creating errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8734964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com