7

我知道我可以attributesOfItemAtPath用来获取文件的修改时间/日期...但是有没有办法设置文件的修改日期/时间?

我看过如何以编程方式设置文件的修改时间?但它似乎并不适用。

我问的原因是我使用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");
}

非常感谢史蒂文!

4

1 回答 1

9

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

于 2011-05-09T15:24:55.740 回答