i am newbie and first to try unzip file, i want to unzip my database contain file and copy database from it for this i try like as
In my DBManager.m file i write like as
@interface DBManager()
@property (nonatomic, strong) NSString *documentsDirectory;
@property (nonatomic, strong) NSString *databaseFilename;
@property (nonatomic, strong) NSMutableArray *arrResults;
@end
-(instancetype)initWithDatabaseFilename:(NSString *)dbFilename{
self = [super init];
if (self)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
self.documentsDirectory = [paths objectAtIndex:0];
self.databaseFilename = dbFilename;
[self copyDatabaseIntoDocumentsDirectory];
}
return self;
}
for unzip file i write like as
#pragma mark - Private method implementation
-(void)copyDatabaseIntoDocumentsDirectory
{
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *zipFilePath = [documentsDirectory stringByAppendingPathComponent:@"sampledb.zip"];
NSString *output = [documentsDirectory stringByAppendingPathComponent:self.documentsDirectory];
ZipArchive* za = [[ZipArchive alloc] init];
if( [za UnzipOpenFile:zipFilePath] )
{
if( [za UnzipFileTo:output overWrite:YES] != NO )
{
NSLog(@"Archive unzip success");
}
[za UnzipCloseFile];
}
NSString *destinationPath = [self.documentsDirectory stringByAppendingPathComponent:self.databaseFilename];
if (![[NSFileManager defaultManager] fileExistsAtPath:destinationPath])
{
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:self.databaseFilename];
NSError *error;
[[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:destinationPath error:&error];
if (error != nil) {
NSLog(@"%@", [error localizedDescription]);
}
}
}
And in my ViewController class view did load i write code for copy database like as
- (void)viewDidLoad
{
[super viewDidLoad];
self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"sampledb.sql"];
}
But my Database is not copied from zip file Please give me solution for it. Here sampledb.zip is my zip file and sampledb.sql is my database file in my zip file. i use ZipArchive from github to unzip file.
Aucun commentaire:
Enregistrer un commentaire