Are any archive file formats natively supported in IOS? - objective-c

Generating a UIWebView with on-board resources is easy enough with the following code.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
However, what I need to do is move one step past simply having pre-generated data on the device, to actually downloading the data from a server to store locally. A lot of that I can handle myself with a reasonable degree of ease, but the question becomes: 'what archive formats does IOS natively support?'
I know there are add-ons that let me support ZIP or PKZip, but if possible it would be preferable to restrict to Apple's built-in tools.

No.
Apparently no native archive format support exists.

If we interpret "archive" generously, as a way to store heterogeneous data in a single file without necessarily compressing it, then you could consider property lists and SQLite databases as archive formats. And, yes, the word "archive" is sometimes used that way, as in .tar format.

Related

Improving text file reading speed in Xcode?

I'm currently working on a project that involves a lot of text file reading. I need to get the contents of the files into NSStrings so I can go and manipulate them further. What I'm using:
NSString *file = [[NSBundle mainBundle] pathForResource:fileName ofType:#"txt"];
NSString *fileContents = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
It works well enough, but as my project grows so does the number of text files. I'm starting to think there should be a more efficient way of searching through the text files included in the project.
In many cases, I know exactly what file I want to search. I have the files organized into group folders based on when they need to be read. Is there any way to narrow the range of the initial file search? It seems like searching through the entire application bundle every time is pointless when I know where the files I need are on a more specific level.
Is there some way to search through groups as opposed to bundles? Can I somehow exclude certain files from the search? From what I can tell, defining custom bundles for this context would not be an appropriate use of the NSBundle functionality.
Have you looked at the method -[NSBundle pathForResource:ofType:inDirectory:]?
I had almost similar kind of situation, for more optimization, i had kept all resource file inside dictionary with some key ,
you may try following
1 -- in our application you may have some kind of pattern or where you could find major group,
2 -- have multiple dictionary each for each group, and inside group, you could store file name,
3 -- As ken suggested to go with the -[NSBundle pathForResource:ofType:inDirectory:]

Resource-only NSBundle: is this kosher?

In my iOS app, I'm downloading content from the web into my /Library/Caches directory. I'd like to represent this directory as an NSBundle for better compatibility with some of the external APIs we're using. (That way, we can simply change [[NSBundle mainBundle] pathForResource...] to [myBundle pathForResource...] whenever it appears.)
The following seems to work fine:
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString* cachesDirectory = [paths objectAtIndex:0];
NSBundle* bundle = [NSBundle bundleWithPath:cachesDirectory];
Better yet, the bundle reflects any changes I make to the /Library/Caches directory. However, I'm concerned because the caches directory is technically not a bundle per Apple's docs. That is to say:
It's not a "directory with a standardized hierarchical structure that holds executable code and the resources used by that code", since there's no code.
It's neither an Application, Framework, nor a Plug-In bundle.
It's most like an Application bundle, but it doesn't contain the required Info.plist or executable.
I could find no mention anywhere of this sort of dynamically-created, resource-only bundle. Is this okay to do?
The /Library/Caches directory will lack some of the standard files which are required in a bundle, like a Contents/ directory or a Contents/Info.plist file, so it may not behave properly when treated as one. Proceed with caution.
Yes, it's absolutely okay to have a resources only bundle. Some of the verbage that you quote pre-exists iOS. In OS X you can dynamically load executable code, that's specifically excluded in iOS.
Localization is an example of resource only bundles.
Edit:
The Bundle Programming Guide says:
Although document formats can leverage the bundle structure to
organize their contents, documents are generally not considered
bundles in the purest sense. A document that is implemented as a
directory and treated as an opaque type is considered to be a document
package, regardless of its internal format. For more information about
document packages, see “Document Packages.”
which says:
There are several ways to access the contents of a document package.
Because a document package is a directory, you can access the
document's contents using any appropriate file-system routines. If you
use a bundle structure for your document package, you can also use the
NSBundle or CFBundleRef routines. Use of a bundle structure is
especially appropriate for documents that store multiple
localizations.
also note that Apple has been telegraphing that it is minimizing the use of "path"/NSString APIs in favor of URL APIs, though existing path APIs will no doubt continue for many more major OS releases.

How to compress a directory with NSData Category?

I've been playing with an app and I wanted to add the ability to compress a directory and it's children. I found the CocoaDev category often mentioned on here but eventually settled on the category put together for Molecules. My problem is less with the compression category and more with converting a directory into a valid NSData object. I want people to be able to deflate the file with any app out there. I have looked into NSFileManager and serializing the directory contents and compressing that, but I suspect this would prohibit the archive from being deflatable.
Where am I going wrong? Would NSData not be sufficient?
sounds like zip -r (more arguments here) may work for you.

Hiding (or encrypting) app resources?

I've developing a Cocoa app that has certain resources (images) which I wish to protect, but still display. Normally one would just place these in the resources folder, but storing there makes it quite easy to grab and use. Is there any way to keep these images hidden, but still access them within the app?
Simple solution:
Merge all files into one big data-file, optionally using 'salts'.
Then retrieve specific files with something like this:
NSData *dataFile = [NSData dataWithContentsOfFile:filePath];
NSData *theFile = [dataFile subdataWithRange: NSMakeRange(startPos,endPos)];
This does not really protect the files,
but prevents people simply dragging out the resources.
At least, the data-file is unusable, certainly with salts.
Another solution:
Create NSData object for every resource.
Add all objects to a NSMutableArray.
Convert the array to one big NSData object.
Write the NSData object to a file.
And add it to the resources folder.
Your app can then read the data-file.
And retrieve the array with the resources.
// Convert array to data
NSData* data=[NSKeyedArchiver archivedDataWithRootObject:theArray];
Use NSKeyedUnarchiver to retrieve the array again.
In order for you to protect the images in one big file, you can just dump the image data to a NSData object sequentially.
If you want, you can use either salts, as previously mentioned, or you can use AES encryption method, as shown here.
Then, you will have to either save the image files structurally (using an NSArray or similar) or record the image offsets so you can retrieve the image data blocks correctly.
This has some drawbacks, specially if your images change over time. That way you will have to monitor those changes and re-structure the file accordingly.
On other option is for you to simply mask the image files by changing name/extension to one of your choice. This will leave some users away from touch.
Finally, you can search for some archiving frameworks using zip like functions and keep the images there (as Blizzard uses in their MPQ format). This will be the best option (since it provides you with encryption methods and it abstracts you of the mechanisms of encryption and archiving) but it may not be easy to find such a framework.
Why do you want to protect the images? It goes without saying that anything you display can be recorded with a screenshot, so if you're trying to protect the images from the person viewing them, there isn't much point.
If you still want to protect them (say, some images should only be available to certain people), encrypting them on disk might be an option. I'm not an Objective-C guy, but this1 seems like a good place to look.

Accessing data files in Objective-C or cocoa

I am fairly new to a objective-c or in whole mac/iphone development. My question is how can I acces a data files(text files or flat files) in objective-c? For example I have a sample.txt file, how can I open this files and access its data? Do I need to use a class? And I heard about dictionary, is this term related to my problems?
Please kindly redirect me to a good site.
Thanks alot.
sasayins.
You can use regular fopen and fread to access the contents of a file. Alternatively, you can use NSString if your file contains only text or NSData for non-text data.
NSString *myString = [NSString stringWithContentsOfFile:#"/path/to/file"];
NSData *myData = [NSData dataWithContentsOfFile:#"/path/to/file"];
Edit
#"/path/to/file" a constant “Objective-C” style string. It is different to a regular C string (i.e. without the # prepended) because it behaves like an object; you can send it messages, and it is able to be stored in NSArrays etc. From a Mac Programmer's point of view, these Objective-C strings can be treated just like NSString objects.
The Mac OS X filesystem layout typically looks like this:
/System contains system files similar to C:\windows\
/Library contains libraries, similar to C:\windows\system32\
/Users similar to Windows' C:\Documents and Settings\
/Applications Mac's version of C:\Program Files\
/Developer Where Xcode, SDKs, and other developer tools live.
If your username on your Mac is "smith", then your Home directory is /Users/smith. If you have a file in your Documents folder of your Home directory called data.txt, then you can use the following code to access it (but I wouldn't recommend hard-coding paths like this)
NSString *myString = [NSString stringWithContentsOfFile:#"/Users/smith/Documents/data.txt"];
There are various functions available for reliably obtaining your home directory and other directories of particular interest. The NSString documentation explains the various methods available for manipulating strings containing paths.