Ios 如何检查是否存在相同的文件
我目前正在使用此代码复制我的SQLite数据库,但它目前只检查文件是否存在。。。我想更改它,以检查文件是否完全相同,例如,我担心如果数据库损坏或没有完全复制,应用程序将失去功能,解决此问题的唯一方法是删除应用程序并重新下载 那么,如果两个文件完全相等,我如何比较呢Ios 如何检查是否存在相同的文件,ios,Ios,我目前正在使用此代码复制我的SQLite数据库,但它目前只检查文件是否存在。。。我想更改它,以检查文件是否完全相同,例如,我担心如果数据库损坏或没有完全复制,应用程序将失去功能,解决此问题的唯一方法是删除应用程序并重新下载 那么,如果两个文件完全相等,我如何比较呢 - (void) copyDatabaseIfNeeded { //Using NSFileManager we can perform many file system operations. NSFileMan
- (void) copyDatabaseIfNeeded {
//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
//NSLog(@"%d",success);
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database01.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
- (NSString *) getDBPath
{
//Search for standard documents using NSSearchPathForDirectoriesInDomains
//First Param = Searching the documents directory
//Second Param = Searching the Users directory and not the System
//Expand any tildes and identify home directories.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
//NSLog(@"dbpath : %@",documentsDir);
return [documentsDir stringByAppendingPathComponent:@"database01.sqlite"];
}
编辑-忘记这个答案-使用Ayan的答案 首先比较文件大小。如果大小不同,则知道文件不同。这是一个简单而快速的检查 如果大小相同,则需要逐字节比较文件。一种低效的方法是将两个文件加载到
NSData
对象中,并查看它们是否相等。只有当文件总是足够小,可以放入内存时,这才有效
更好的方法是以流的形式打开这两个文件,并将它们分块读取。比较每个区块(每个区块2公里),直到两个区块不同,或者你到达最后。编辑-忘记这个答案-使用Ayan提供的答案 首先比较文件大小。如果大小不同,则知道文件不同。这是一个简单而快速的检查 如果大小相同,则需要逐字节比较文件。一种低效的方法是将两个文件加载到
NSData
对象中,并查看它们是否相等。只有当文件总是足够小,可以放入内存时,这才有效
更好的方法是以流的形式打开这两个文件,并将它们分块读取。比较每个数据块(比如每个2k),直到两个数据块不同或结束。为此,可以使用
NSFileManager的contentsEqualAtPath:andPath:
方法。
使用如下代码:
......
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database01.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
success = [fileManager contentsEqualAtPath:defaultDBPath andPath:dbPath]; //verify if file size and content matches
if(!success) {
//report error
}
}
.......
它应该可以为您提供帮助。为此,您可以使用NSFileManager
的contentsEqualAtPath:andPath:
方法。
使用如下代码:
......
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database01.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
success = [fileManager contentsEqualAtPath:defaultDBPath andPath:dbPath]; //verify if file size and content matches
if(!success) {
//report error
}
}
.......
它应该能帮你解决问题。哇-我以前从未注意过这种方法。这比我的答案要好得多。哇,我以前从来没有注意过这个方法。这比我的答案要好得多。