【发布时间】:2011-08-21 18:56:16
【问题描述】:
我知道我可以使用attributesOfItemAtPath 来获取文件的修改时间/日期...但是有没有办法设置文件的修改日期/时间?
我查看了How to set the modification time of a file programmatically?,但它似乎并不适用。
我问的原因是我使用http://allseeing-i.com/ASIHTTPRequest/(如下)来获取文件......但是时间戳与服务器上的 Last-Modified 时间不同。我想使用 Last-Modified 标头来设置下载文件的日期/时间,这样我就可以使系统上的下载时间与服务器上的时间相同。
我的代码如下:
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 Modified: %@",lastModified);
因此,字符串lastModified 包含时间/日期戳。有没有办法确保librarySQLGZipPath 的文件现在设置为lastModified 的日期/时间?使用当前方法,文件librarySQLGZipPath 保存了它的下载时间,这对我来说是无效的。
谢谢!
简
稍后编辑:
因为我想在此页面上很好地展示如何为此编写代码,并且仍然想感谢 Steven Kramer 的回答,所以我现在将使用我使用代码得到的答案来编辑问题:
而NSDateFromRFC1123 是在这里找到的例程:http://blog.mro.name/2009/08/nsdateformatter-http-header/(经过一些调整)将 Last-Modified 标头字符串更改为 NSDate 对象:
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");
}
非常感谢史蒂文!
【问题讨论】:
标签: iphone sdk asihttprequest last-modified