copy sqlite DB From Inbox to documents directory - ios

when open sqlite file from mail it come to folder named inbox in my documents directory
and i use this code to copy this file to document directory
- (void) copyAdd
{
NSString *docsDir;
NSArray *dirPaths;
NSString *databasePath;
NSFileManager *filemgr = [NSFileManager defaultManager];
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString* inboxPath = [docsDir stringByAppendingPathComponent:#"Inbox"];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: #"CAT.sqlite"]];
if(![filemgr fileExistsAtPath:databasePath]){
[filemgr copyItemAtPath:inboxPath toPath:databasePath error:nil];
NSLog( #"copy create database");
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"sucess" message:#"COPY CAT DB" delegate:self cancelButtonTitle:#"dismis" otherButtonTitles:nil, nil];
[alert show];
}else{
NSLog( #"Failed to open/create database");
}
it create folder with name CAT.sqlite and inside this folder i found the dataBase CAT.sqlite
how can i copy the file named CAT.sqlite from Inbox folder to document directory without create folder with the same name .

this is my code that solve the problem the only missing thing is to specify the file name that i need to copy from the inbox folder not the all folder but CAT.sqlite folder
- (void) copyAdd
{
NSString *docsDir;
NSArray *dirPaths;
NSString *databasePath;
NSFileManager *filemgr = [NSFileManager defaultManager];
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString* inboxPath = [docsDir stringByAppendingPathComponent:#"Inbox"];
NSString *filePath = [inboxPath stringByAppendingPathComponent:#"CAT.sqlite"];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: #"CAT.sqlite"]];
if(![filemgr fileExistsAtPath:databasePath]){
// NSLog(#"dbPAth : %#",dbPAth);
[filemgr copyItemAtPath:filePath toPath:databasePath error:nil];
NSLog( #"copy create database");
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"sucess" message:#"COPY CAT DB" delegate:self cancelButtonTitle:#"dismis" otherButtonTitles:nil, nil];
[alert show];
}else{
NSLog( #"Failed to open/create database");
}
}

Related

How can I backup and restore .zip file on iCloud Drive?

I want to store .zip files on iCloud. Anybody help me how to upload and restore? I have read the Apple guideline for iCloud backup and now I have created one folder with some files and two folders with multiple images and then generate zip file of that folder.
First Save your File and Folder on Local Storage.
Then Create this File & Folder's Zip File.
And Last Upload Your zip file on iCloud.
For get this file doing reverse process.
- (void)viewDidLoad {
[super viewDidLoad];
[self CreatFileAndFolder];
}
Create Folder on local and save file in this folder that you want upload on iCloud Drive.
-(void)CreatFileAndFolder{
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"/meetInChat"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
NSString *stringToWrite = #"1\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n41\n2\n3\n4";
NSString *exportPath = [dataPath stringByAppendingString:#"/mytext.txt"];
[stringToWrite writeToFile:exportPath atomically:YES encoding:NSUTF8StringEncoding error:&error];
}
Create Action that First Create Zip file your Folder and then Upload your Zip file on iCloud Drive.
-(IBAction) iCloudSyncing:(id)sender
{
[self zipFolder];
//--------------------------Zip Folder Upload on iCloud-----------------------------//
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *zipFilePath = [documentsDirectory stringByAppendingPathComponent:#"meetInChat.zip"];
NSLog(#"FilePath=>%#",zipFilePath);
NSURL *u = [[NSURL alloc] initFileURLWithPath:zipFilePath];
NSData *data = [[NSData alloc] initWithContentsOfURL:u];
NSURL *ubiq = [[NSFileManager defaultManager]URLForUbiquityContainerIdentifier:nil];
NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:#"Documents"]URLByAppendingPathComponent:#"meetInChat.zip"];
Mydoc = [[MyDocument alloc] initWithFileURL:ubiquitousPackage];
Mydoc.zipDataContent = data;
[Mydoc saveToURL:[Mydoc fileURL] forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success)
{
if (success)
{
NSLog(#"PictureZip: Synced with icloud");
[[NSUbiquitousKeyValueStore defaultStore]setData:data forKey:#"meetInChat"];
}
else
NSLog(#"PictureZip: Syncing FAILED with icloud");
}];
}
Create Zip file from your folder
-(BOOL)zipFolder
{
//--------------------------Create Zip Folder -----------------------------//
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
BOOL isDir=NO;
NSArray *subpaths = nil;
NSString *exportPath = [docDirectory stringByAppendingString:#"/meetInChat"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:exportPath isDirectory:&isDir] && isDir){
subpaths = [fileManager subpathsAtPath:exportPath];
}
NSString *meetInChatPath = [docDirectory stringByAppendingString:[NSString stringWithFormat:#"/%#.zip",#"meetInChat"]];
ZipArchive *archiver = [[ZipArchive alloc] init];
[archiver CreateZipFile2:meetInChatPath];
if (isDir) {
for(NSString *path in subpaths){
NSString *fullPath = [exportPath stringByAppendingPathComponent:path];
if([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && !isDir){
[archiver addFileToZip:fullPath newname:path];
}
}
} else {
[archiver addFileToZip:exportPath newname:#"meetInChat"];
}
BOOL successCompressing = [archiver CloseZipFile2];
if(successCompressing)
return YES;
else
return NO;
}
Here Get ZipFile back From iCloud Drive then Doing reverse process Unzip your file and get data back.
- (IBAction)GetData:(id)sender {
//--------------------------Get data back from iCloud -----------------------------//
id token = [[NSFileManager defaultManager] ubiquityIdentityToken];
if (token == nil)
{
NSLog(#"ICloud Is not LogIn");
}
else
{
NSLog(#"ICloud Is LogIn");
NSData *dataFile = [[NSUbiquitousKeyValueStore defaultStore]dataForKey:#"meetInChat"];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* fileName = [NSString stringWithFormat:#"meetInChat.zip"];
NSString* fileAtPath = [documentsDirectory stringByAppendingPathComponent:fileName];
[dataFile writeToFile:fileAtPath atomically:NO];
}
}

Unzipping images to NSArray ios

I have a zip file of images on my backend server. I want to download the zip and unarchive the content (images) to NSArray. Is there a way to do it?
Thank you)
You could to use SSZipArchive in order to work with zip files.
Below line would unzip and assign images to a NSArray
// Unzipping
NSString *zipPath = #"path_to_your_zip_file";
NSString *destinationPath = #"path_to_the_folder_where_you_want_it_unzipped";
[SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];
// Zipping
NSString *zippedPath = #"path_where_you_want_the_file_created";
NSArray *inputPaths = [NSArray arrayWithObjects:
[[NSBundle mainBundle] pathForResource:#"photo1" ofType:#"jpg"],
[[NSBundle mainBundle] pathForResource:#"photo2" ofType:#"jpg"]
nil];
[SSZipArchive createZipFileAtPath:zippedPath withFilesAtPaths:inputPaths];
To delete a file:
- (void)deleteFile:(NSString *)fileName
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:fileName];
NSError *error;
BOOL success = [fileManager removeItemAtPath:filePath error:&error];
if (success) {
UIAlertView *removeSuccessFulAlert=[[UIAlertView alloc]initWithTitle:#"Congratulation:" message:#"Successfully removed" delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[removeSuccessFulAlert show];
}
else
{
NSLog(#"Could not delete file -:%# ",[error localizedDescription]);
}
}

How to create zip of folder and file in IOS?

I can able to zip the multiple pdf files and individual folder . But failed to create zip folder of a file and folder which i marked in my screenshot.here is my code . In the If condition i write the code for creating zip file for a folder and in else i write code for files . If i select one file and folder , I need to create zip file of both folder and file in single zip file .
if([[[[filePathsArray objectAtIndex:i]objectForKey:#"PdfName"] pathExtension]isEqualToString:#""]){
// For Folder - It creates a zip file in document directory .
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
BOOL isDir=NO;
NSArray *directoryContents = [[NSFileManager defaultManager] directoryContentsAtPath: docDirectory];
NSString *exportPath = [[mailArray objectAtIndex:i]objectForKey:#"PdfPath"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:exportPath isDirectory:&isDir] && isDir){
directoryContents = [fileManager subpathsAtPath:exportPath];
}
archivePath = [exportPath stringByAppendingString:[NSString stringWithFormat:#"%#.zip", [[filePathsArray objectAtIndex:i]objectForKey:#"PdfName"]]];
ZipArchive *archiver = [[ZipArchive alloc] init];
[archiver CreateZipFile2:archivePath];
for(NSString *path in directoryContents)
{
NSString *longPath = [exportPath stringByAppendingPathComponent:path];
if([fileManager fileExistsAtPath:longPath isDirectory:&isDir] && !isDir)
{
NSString * extension = #"pdf";
if ([[longPath pathExtension]isEqualToString:#""]||[[[longPath pathExtension]lowercaseString]isEqualToString:extension]) {
[archiver addFileToZip:longPath newname:path];
}
}
}
BOOL successCompressing = [archiver CloseZipFile2];
if(successCompressing)
NSLog(#"Success");
else
NSLog(#"Fail");
NSString *path = archivePath;
[pdfData appendData:[NSMutableData dataWithContentsOfFile: path]];
}
else{
// For Single or Multiple pdf files - It creates a zip file in document directory .
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
isFolder = YES;
archivePath = [docDirectory stringByAppendingString:#"/pdfMarkup.zip"];
ZipArchive *archiver = [[ZipArchive alloc] init];
[archiver CreateZipFile2:archivePath];
for (int i =0; i<[mailArray count]; i++)
{
[archiver addFileToZip:[[mailArray objectAtIndex:i]objectForKey:#"PdfPath"] newname:[[[mailArray objectAtIndex:i]objectForKey:#"PdfName"] stringByReplacingOccurrencesOfString:#"/" withString:#""]];
}
BOOL successCompressing = [archiver CloseZipFile2];
if(successCompressing)
{
NSLog(#"Zipp successfull");
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"message:#"Cannot zip Docs Folder"delegate:nilcancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
NSString *path = archivePath;
[pdfData appendData:[NSMutableData dataWithContentsOfFile: path]];
}

FMDatabase: connect DB made in advance

I made dbfile sample.db by terminal in advance, and added the file to the project.
But the App create a dbfile from scratch, instead of refering to the db added.
How do I need to fix to refer the db added?
NSArray* paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString* dir = [paths objectAtIndex:0];
FMDatabase* db = [FMDatabase databaseWithPath:[dir stringByAppendingPathComponent:samble.db]];
[db open];
Check for the existence of the database in Documents folder before you try to open it, and if it's not there, then copy the version from the bundle to the Documents folder before subsequently opening it from the Documents folder.
So, first, because you have your blank database sitting in the Documents folder, remove it (by uninstalling the app and then re-installing). And then you can do something like the following
BOOL success;
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* dir = [paths objectAtIndex:0];
NSString* documentsPath = [dir stringByAppendingPathComponent:#"samble.db"];
NSFileManager* fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:documentsPath]) {
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"samble" ofType:#"db"];
NSAssert(bundlePath, #"%s: database not found in bundle", __FUNCTION__);
NSError *copyError;
success = [fileManager copyItemAtPath:bundlePath toPath:documentsPath error:&copyError];
NSAssert(success, #"%s: copyItemAtPath failed: %#", __FUNCTION__, copyError);
}
FMDatabase* db = [FMDatabase databaseWithPath:documentsPath];
NSAssert(db, #"%s: databaseWithPath failed", __FUNCTION__);
success = [db open];
NSAssert(success, #"%s: open failed", __FUNCTION__);

ios fmdb load db from file

I use sqlite and fmdb wrapper. My code for load db is this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *path = [docsPath stringByAppendingPathComponent:#"biscuit.sqlite"];
FMDatabase *db = [FMDatabase databaseWithPath:path];
I add biscuit.sqlite to my project, but this code create new file instead open existing one. How can I load this file?
If you only want to read data from your database, try to use the code below:
NSString *path = [[NSBundle mainBundle] pathForResource:#"biscuit" ofType:#"sqlite"];
If you need read/write access, you have to copy your database to a writable user folder first.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:#"biscuit.sqlite"];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if (!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"biscuit.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success) {
NSLog(#"Failed to create writable DB. Error '%#'.", [error localizedDescription]);
} else {
NSLog(#"DB copied.");
}
}else {
NSLog(#"DB exists, no need to copy.");
}

Resources