iOS zip subdirectory documents folder - ios

I'm using ZipKit for iOS I'm trying to zip a subdirectory of my Documents folder. I've referred to this question but it still doesn't seem to be working. My code is:
ZKFileArchive *archive = [ZKFileArchive archiveWithArchivePath:#"Documents/test.zip"];
NSInteger result = [archive deflateDirectory:#"/Documents/test" relativeToPath:#"/Documents" usingResourceFork:NO];
I have a "test" subdirectory and I want to zip it up into test.zip. The "result" returns 1, but no zip folder appears. Any ideas?

Figured it out. For anyone with the same problem, you just need to set the relative path to the actual documents folder:
NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolder = [documentPath objectAtIndex:0];
ZKFileArchive *archive = [ZKFileArchive archiveWithArchivePath:#"Documents/test.zip"];
NSInteger result = [archive deflateDirectory:#"/Documents/test" relativeToPath:documentFolder usingResourceFork:NO];

Related

Get file path from Document directory - Objective-c iOS

Actually, I'm relying on full file path for security options (for that I need file path for it to be stored in keychain). It means that when I rebuild the app (or when it will get an update when published to AppStore), these features are broken due to bundle ID change in file path.
I've thought of a solution for this issue : get file path only from Documents directory (/Documents/.../.../myFile.pdf) rather than full path (/var/mobile/.../...). Is there a way of doing this ?
Alternatively is there any other solution to my issue ?
Thanks in advance !
Get the File Path from Document Directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",strFileName]];
Thanks to #Larme and #user3182143, I've found a solution : getting Documents path and splitting file path by 2 : one part before Document directory
bundlePath = [documentsDirectory stringByDeletingLastPathComponent];
and the other part with
documentsPath = [documentsDirectory lastPathComponent];
...
filepath = [documentsPath stringByApendingPathComponent:#"myFile.pdf"];
If I need the full path, I'll get it by
fullPath = [bundlePath stringByAppendingPathComponent:filepath];

How to read a file without extension in Objective C

I have a file stored in my iOS App Documents directory. It is a plist file but without .plist file extension. I want to read its content from code but i cant open the file without extension. Is there any way to add plist extension or convert the file into plist, or any way to read content of a file without extension.
This is how my code look like
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"sample.bookmarks"];
filePath = [filePath stringByAppendingPathExtension:#"plist"];
NSDictionary *dict=[NSDictionary dictionaryWithContentsOfFile:filePath];
Thanks in Advance
PS : If I add the .plist extension in finder(by some method) I can open the file and see the content. So I am sure about its type.
It should be possible to load the Plist file whether it has a .plist extension or not. Assuming the Plist filename is sample.bookmarks then the following should load the Plist. I am assuming the root element of the Plist is a dictionary because you state that the Plist can be opened if it has a .plist extension.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"sample.bookmarks"];
NSDictionary *dict=[NSDictionary dictionaryWithContentsOfFile:filePath];
The only change is that I've removed the line that adds a plist extension to the filePath.
The filePath = [filePath stringByAppendingPathExtension:#"plist"]; line will change the filename portion of the filePath to sample.bookmarks.plist. If you changed the extension to an empty string (filePath = [filePath stringByAppendingPathExtension:#""];) then the filename will be changed to sample.bookmarks. which is not the same as sample.bookmarks.
Try with this.
pathToFile = [pathToFile stringByAppendingPathExtension:#"plist"];

my app creates the /var/mobile/ folder but I can't find it in Finder

My Xcode app creates a document and resource path. Please see the console output below when I print out these two path variables:
However, when I try to find this folder in Finder, I'm told the folder doesn't exist.
Why?!
The code for getting the two paths are below:
// Get the path to the resource directory.
NSString* resourcePath= [[NSBundle mainBundle] resourcePath];
std::string *resourcePathCPP = new std::string([resourcePath UTF8String]);
// Get the path to the Documents direcotory
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
std::string *documentsPathCPP = new std::string([documentsPath UTF8String]);
Thanks!
That should be on your iPhone/iPad's file system, not on your Mac. Otherwise Go to folder ... Is the right way to check a directory.

Using directory to store information and keep this information in a App Update

I have an app that stores images from the user in the app/documents folder, I'm doing the following code determine the path to save it:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docs = [paths objectAtIndex:0];
NSString *imageFilename = [[[[NSNumber alloc] initWithInteger:self.nextID] stringValue] stringByAppendingFormat:#".jpg"];
NSString *dirPath = [docs stringByAppendingFormat:#"/Photos/"];
imagePath = [dirPath stringByAppendingString:imageFilename];
it gives me a path similar to (in simulator):
/var/mobile/Applications/C9C43CFD-6A5F-40CF-8BAE-20496B5A544A/Documents/Photos/1.jpg
I save this path to show this image when I need.
My problem is, when I sent an update in the app store, after update the app it cant show the images anymore.
My understand is the Documents folder cant change when update the app in AppStore.
My probably reason for that it the name between Applications folder and Documentar has changed, is it true when update a app version from AppStore?
Does anyone has any idea? is there any way to test this app update local?
Thanks
You need to save the relative path after the Documents directory, not the absolute path. The app's directory can change during an update but the structure inside the app's sandbox won't change.
Also, there is a slightly better way to build your path:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docs = [paths objectAtIndex:0];
NSString *imageFilename = [NSString stringWithFormat:#"%d.jpg", self.nextID];
NSString *dirPath = [docs stringByAppendingPathComponent:#"Photos"];
imagePath = [dirPath stringByAppendingPathComponent:imageFilename];
When the app is updated from the App Store, the contents of the Documents directory are unchanged. The app bundle and all its contents are replaced with the new version, but the Documents folder is a safe place to store content between app versions.

FMDB open database

I'm trying to open a database that I have in my project inside Resources.
The problem is that it seems that its impossible to find the database file!
I tried with the complete path, and it works, but this is not a good solution.
I would like to now how to open it!
I'm using this code:
db = [FMDatabase databaseWithPath:#"bbdd.sql"];
I don't know how to find the other part of the "actual" path.
Do you have a solution for me?
Thanks!!!!
You need to find the full path of the database in the your resource bundle, something like this :
NSString *databasePath = [[NSBundle mainBundle] pathForResource:#"mySQLiteDatabaseFile" ofType:#"sqlite3"];
There's a complete example in this thread Copying data to the Application Data folder on the iPhone
Get Document directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentDirectory stringByAppendingPathComponent:#"Test.db"];
Next
DB = [FMDatabase databaseWithPath:dbPath];

Resources