– (NSURL *)convertTxtEncoding:(NSURL *)fileUrl
{
if ([FileManagementAPI getFileSize:[fileUrl path]] > 1024*1024.0f)
{
return fileUrl;
}
NSString *tmpFilePath = [NSString stringWithFormat:@"%@/tmp/%@", NSHomeDirectory(), [fileUrl lastPathComponent]];
NSLog(@"tmpFilePath=%@", tmpFilePath);
NSURL *tmpFileUrl = [NSURL fileURLWithPath:tmpFilePath];
NSStringEncoding encode;
NSString *contentStr = [NSString stringWithContentsOfURL:fileUrl usedEncoding:&encode error:NULL];
if (contentStr)
{
[contentStr writeToURL:tmpFileUrl atomically:YES encoding:NSUTF16StringEncoding error:NULL];
return tmpFileUrl;
}
else
{
NSStringEncoding convertEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
contentStr = [NSString stringWithContentsOfURL:fileUrl encoding:convertEncoding error:NULL];
if (contentStr)
{
[contentStr writeToURL:tmpFileUrl atomically:YES encoding:NSUTF16StringEncoding error:NULL];
return tmpFileUrl;
}
else
{
return fileUrl;
}
}
}
//顯示的時候調用這個
NSURL *url = [self.documentURLArray objectAtIndex:index];
if ([url pathExtension] && ([[[url pathExtension] lowercaseString] isEqualToString:@"txt"] || [[[url pathExtension] lowercaseString] isEqualToString:@"text"]))
{
return [self convertTxtEncoding:url];
}
else
{
return url;
}