开发者

How to set attributesOfItemAtPath (such as modification date/time) on iPhone

开发者 https://www.devze.com 2023-03-04 12:38 出处:网络
I know I can use attributesOfItemAtPath to get modification time/date of file among other things... but is there a way to SET the modification date/time of a file?

I know I can use attributesOfItemAtPath to get modification time/date of file among other things... but is there a way to SET the modification date/time of a file?

I have looked at How to set the modification time of a file programmatically? but it does not seem to apply.

The reason I ask is I use http://allseeing-i.com/ASIHTTPRequest/ (as below) to fetch a file... however the timestamp is not kept the same as the Last-Modified time on the server. I want to use the Last-Modified header to set the date/time of the downloaded file so i can keep the downloaded time on my system the same as it is on the server.

My code follows:

ASIHTTPRequest *requestFeed = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:kGZipSQLURL]];
[requestFeed setNumberOfTimesToRetryOnTimeout:2];
[requestFeed setDownloadDestinationPath:librarySQLGZipPath];
[requestFeed setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[requestFeed setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[requestFeed setSecondsToCache:60*60*24*30];
[requestFeed setDownloadCache:[ASIDownloadCache sharedCache]];
[requestFeed startSynchronous];
NSString *lastModified = [[requestFeed responseHeaders] objectForKey:@"Last-Modified"];
NSLog(@"Last 开发者_JS百科Modified: %@",lastModified);

So, the string lastModified holds the time/date stamp. Is there a way to ensure that the file at librarySQLGZipPath will now be set to the date/time in lastModified? Using the current method, the file librarySQLGZipPath holds the time it was downloaded, which to me is invalid.

Thanks!

Jann

EDIT LATER:

Because I wanted to nicely put on this page how to do the code for this and still wanted to give Steven Kramer credit for answering I am now gonna edit the question with the answer I got using code:

whereas NSDateFromRFC1123 is a routine found here: http://blog.mro.name/2009/08/nsdateformatter-http-header/ (with some adjustments) to change the Last-Modified header string into a NSDate object:

NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:[self NSDateFromRFC1123:lastModified]  ,NSFileModificationDate, nil];
NSError *errorFile;
if ([NSFileManager defaultManager] setAttributes:attrs ofItemAtPath:librarySQLGZipPath error: &errorFile])
{
NSLog(@"Set timestamp of file");
}
else {
NSLog(@"COULD NOT set timestamp of file");
}

Thanks so much Steven!


Use -[NSFileManager setAttributes:(NSDictionary *)attributes ofItemAtPath:(NSString *)path error:(NSError **)error

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号