简体   繁体   中英

How to open unzip files in a unique folder each time on iOS

On my iOS application I'm unziping files in "app/temp" folder like this:

NSString *unzipFolder = [[CommonFunctions getCachePath] stringByAppendingPathComponent:@"/temp/"];

and once im done with it im deleting item with:

    [[NSFileManager defaultManager] removeItemAtPath:unzipFolder error:&e];

Issue is that bcz im creating mulital copy of unzip files some of the files Images name are same and displaying wrong images, and i dont find error on why my deleting function does not work!

IS there any way that i can do unziping of folder on diffrent path for each message that open by user? Thanks :)

It sounds like all you're asking is how to generate a unique name for your unzipFolder each time.

Just don't use a hardcoded name. Almost anything will do. For example:

NSString *unzipFolderTemplate = [[CommonFunctions getCachePath] stringByAppendingPathComponent:@"temp.XXXXXX"];
char *template = strdup([template fileSystemRepresentation]);
if (mkdtemp(template)) {
  NSString *unzipFolder = [NSString stringWithFileSystemRepresentation:template
                                                                length:strlen(template)];
  free(template);
  // do the work
  [[NSFileManager defaultManager] removeItemAtPath:unzipFolder error:&e];
}

The nice thing about mkdtemp is that it creates the directory for you, and guarantees there are no race conditions, somehow-left-over directories, or other problems. It's also more secure against, say, someone writing a crack or other jailbreak hack that exploits your code by predicting the path. The downside is, of course, that you have to drop down to C strings (which means an explicit free ). But, as I said, there are many possibilities, and almost anything will do.

Also, note that I'm using @"temp.XXXXXX" , not @"/temp.XXXXXX/" . That's because -[stringByAppendingPathComponent:] already adds any necessary slashes for you (that is, in fact, the whole point of the method), and directory-creation functions don't need a trailing slash, so both of the slashes are unnecessary.

Meanwhile, I'm still a bit confused by what you're trying to do. If you need to keep a unique folder around for each message, and delete the folder when you're done with that message, and you could have multiple messages open at once, you need some way to remember which folder goes with which message.

For that, create an NSMutableDictionary somewhere, and right after the free(template) you'll want to do something like [tempFolderMap addObject:unzipFolder forKey:messageName] . Then, when closing a message, you'll do [tempFolderMap objectForKey:messageName] and use the result to the removeItemAtPath:error: message (and then you can also remove the key from tempFolderMap ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM