Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSFileManager createDirectoryAtPath:withIntermediateDirectories: Not creating directories or creating errors

In my app, when a user makes an in app purchase, the application needs to download and unzip a zip file to the app's documents folder. The zip file downloads and can be unzipped. I'm using Objective Zip to unzip the archive. The problem is that when trying to create the folder paths for each file, the folders are never created, and there is no error.

Here is some sample code for the section where this is happening:

// 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");
}

Here is relevant snippet of output from the log file:

    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)

Reading the NSFileManager documentation, this is what should output in the log, but, No folders are created.

EDIT: Fixed not using documents issue, still have same problem as decried before edit.

EDIT: Updated code to working solution!

like image 626
Brandon Mcq Avatar asked Jan 04 '12 22:01

Brandon Mcq


2 Answers

You shouldn't be writing to the app's bundle itself. Try this:

NSString *appSupportFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [appSupportFolder stringByAppendingPathComponent:info.name];
like image 82
Andrew Madsen Avatar answered Sep 18 '22 23:09

Andrew Madsen


I don't think you're able to create directories directly inside of WhatsMyStageOn.app/, try creating them inside of your Documents directory.

NSMutableString *path = [info.name mutableCopy];
path = [[path stringByDeletingLastPathComponent] mutableCopy];
path = [NSMutableString  stringWithFormat:@"%@/%@", applicationDocumentsDir, path];

You have a line at the bottom that deletes the correct file path inside of Documents, but your original path variable points at the bundle directory, not the Documents directory.

like image 42
afrederick Avatar answered Sep 20 '22 23:09

afrederick