<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iPhone Programming Tutorials &#187; Marin Todorov</title>
	<atom:link href="/author/marin-todorov/feed/" rel="self" type="application/rss+xml" />
	<link>http://icodeblog.com</link>
	<description>iPhone Programming Tutorials</description>
	<lastBuildDate>Thu, 13 Jun 2013 08:14:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Creating a document centric iPhone/iPad application with own file format using ZipArchive</title>
		<link>http://icodeblog.com/2010/04/12/creating-a-document-centric-iphoneipad-application-with-own-file-format-using-ziparchive/</link>
		<comments>http://icodeblog.com/2010/04/12/creating-a-document-centric-iphoneipad-application-with-own-file-format-using-ziparchive/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 13:00:48 +0000</pubDate>
		<dc:creator>Marin Todorov</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[custom]]></category>
		<category><![CDATA[file format]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[ipad]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[NSFileManager]]></category>
		<category><![CDATA[ZipArchive]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1941</guid>
		<description><![CDATA[If I need to predict one thing about where the App Store is heading to, now that the iPad has been released, I would say "It's going to be less about farting apps and much more about productivity apps" Yes - the iPad has already changed the game drastically - with an almost real life size keyboard, large beautiful screen and file sharing direct in iTunes you can achieve much more than before. But hey, iPhone OS 4.0 is just around the corner, and I bet one of those new features will be the same file sharing you get on the iPad.]]></description>
				<content:encoded><![CDATA[<p>If I need to predict one thing about where the App Store is heading to, now that the iPad has been released, I would say &#8220;It&#8217;s going to be less about farting apps and much more about productivity apps&#8221; Yes &#8211; the iPad has already changed the game drastically &#8211; with an almost real life size keyboard, large beautiful screen and file sharing direct in iTunes you can achieve much more than before. But hey, iPhone OS 4.0 is just around the corner, and I bet one of those new features will be the same file sharing you get on the iPad.</p>
<h3>The problem</h3>
<p>Assuming you are already familiar with Objective-C and Cocoa Touch, today I&#8217;ll be discussing how to create a productivity application which can read and write multimedia files &#8211; custom format files which will be a mixture of image and text data. If you look at the most of the Apple&#8217;s productivity applications you&#8217;ll notice they all use bundles as their output format. For those not familiar with the concept &#8211; the bundle is an actual file directory, which then holds different files, but to the user it&#8217;s presented as a single file &#8211; easier to copy around and in general to work with. I am going to be doing the same today by making my example application create different text and image files and then zip them together in a single file &#8211; this way the user could then copy this single file over trough iTunes via the file sharing feature.</p>
<h3>ZipArchive overview</h3>
<p>The star of today&#8217;s article is ZipArchive &#8211; the Objective-C library I&#8217;m going to use for compressing and uncompressing my custom zip files. It is completely free and you can download it from : <a title="ZipArchive download" href="http://code.google.com/p/ziparchive/" target="_blank">http://code.google.com/p/ziparchive/</a></p>
<p>To use ZipArchive I download the library and add it to my Xcode project.</p>
<p><a href="/wp-content/uploads/2010/04/zipFilelist1.png"><img class="alignnone size-full wp-image-1945" src="/wp-content/uploads/2010/04/zipFilelist1.png" alt="" width="248" height="221" /></a></p>
<p>Now that I added the ZipArchive sources I am almost ready to create and extract zip archives. In fact ZipArchive uses the libz framework, so I need to add this framework to my XCode project too. Add-&gt;Existing Frameworks and I choose &#8220;libz.1.2.3.dylib&#8221;</p>
<p><a href="/wp-content/uploads/2010/04/addZlyb1.jpeg"><img class="alignnone size-medium wp-image-1942" src="/wp-content/uploads/2010/04/addZlyb-300x250.jpg" alt="" width="300" height="250" /></a></p>
<p>ZipArchive is very straightforward to use, also because it offers just a handful of methods, let&#8217;s have a look together at what&#8217;s inside:</p>
<p>Creating a ZipArchive instance is super simple.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;ZipArchive.h&quot;</span>
&nbsp;
ZipArchive<span style="color: #002200;">*</span> zip <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ZipArchive alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<h3>Create zip archives</h3>
<p>Call CreateZipFile2: to create an empty zip archive file, or call CreateZipFile:Password: to create an empty zip archive which is password protected (the latter makes creating encrypted files really easy). <strong>NB:</strong> <em>If your application is creating password protected zip files in general it uses encryption, so you would need to tick the encryption checkbox when you submit it to the App Store.</em></p>
<p>Once you create a zip archive you can add as many files as you like by calling addFileToZip:newname: and when you are done call CloseZipFile2. Or as they say &#8220;a line of code is worth thousand words&#8221;:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">ZipArchive <span style="color: #002200;">*</span>zip <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ZipArchive alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip CreateZipFile2<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;archive.zip&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip addFileToZip<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photo1.png&quot;</span> newname<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photo1.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip addFileToZip<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photo2.png&quot;</span> newname<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photo2.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip addFileToZip<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;../../test/IMG_0001.png&quot;</span> newname<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photo3.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip CloseZipFile2<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Now I really don&#8217;t think creating a zip archive gets any simpler than that. All of those methods return a boolean result, which you can check to see if the operation was successful, so you can implement also your error handling code along. To create a password protected file call:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>zip CreateZipFile2<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;archive.zip&quot;</span> Password<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;plaintextpassword&quot;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>(<em>I should really admit that ZipArchive&#8217;s the naming convention is strange: some names are CamelCase: , some camelCase:, some camel:Case: , some camelCase:case:, and sometimes a capital in the middle of the word &#8220;overWrite&#8221; &#8211; and this is a class with only 8 methods :</em>)</p>
<h3>Extracting zip archives</h3>
<p>Extracting files from a zip archive is as simple as creating one. You open an archive using UnzipOpenFile: or UnzipOpenFile:Password: Now pay attention to the result of that operation- if the file exists, but UnzipOpenFile: returns NO, it might mean the archive is password protected. If the archive file has been successfully opened it means ZipArchive can read the contents and you can proceed to extracting the files to a destination of your choice by using UnzipFileTo:overWrite: If you pass YES as the second argument, the extracting operation will overwrite files at the target location.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">ZipArchive <span style="color: #002200;">*</span>zip <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ZipArchive alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>zip UnzipOpenFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;archive.zip&quot;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #002200;">&#91;</span>zip UnzipFileTo<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tempFolder&quot;</span> overWrite<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#91;</span>zip UnzipCloseFile<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>To gain a bit more control over what ZipArchive does you can set a class of yours as a delegate, here is the ZipArchiveDelegate protocol:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@protocol</span> ZipArchiveDelegate <span style="color: #002200;">&amp;</span>lt;NSObject<span style="color: #002200;">&amp;</span>gt;
&nbsp;
@optional
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> ErrorMessage<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> msg;
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> OverWriteOperation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> file;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<div>So as the method names suggest if you want to get more information about the errors happening let your delegate implement ErrorMessage: and if you want to be more flexible which files gets overwritten during archive extraction implement OverWriteOperation: (return YES to overwrite the given file)</div>
<p>Now that you know everything there is to know about ZipArchive, I can start with my super duper productivity application &#8230;</p>
<h3>Creating simple custom file format with ZipArchive</h3>
<p>I&#8217;ll first create a class called CustomFile, which will be my data model &#8211; it will be responsible for reading and writing data to the file system. I&#8217;ll need few ivars and properties for them:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> filePath;
&nbsp;
<span style="color: #11740a; font-style: italic;">//the file contents</span>
&nbsp;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> title;
&nbsp;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> text;
&nbsp;
UIImage<span style="color: #002200;">*</span> photo;</pre></td></tr></table></div>

<p>In filePath I&#8217;ll keep the absolute path to the location the file will be written to or red from. The rest of the ivars will be my file contents &#8211; 2 texts and 1 image. Now let me explain what this custom file format will be about &#8211; I will want to store in my files short articles &#8211; much similar to blog posts &#8211; my files will hold the title, the full text and a photo (if attached) of an article.</p>
<p>Some of you will ask <strong>why</strong> do I need a custom format for my files &#8230; can&#8217;t I just save a text file with the text and the image somewhere on my disk and then save their names in a sqlite database and done ? Nope, think trends, think iPad / iPhone 4.0 and think file sharing: as I said things are moving beyond apps which sole purpose is to amuse their users for about 5 to 10 minutes. If my app saves separate files of text and images, when the user wants to copy them from his iPad to his wife&#8217;s iPad he might miss one of those files and that will ruin the integrity of his document&#8230; So let&#8217;s look further what I have in mind:</p>
<h3>My model&#8217;s initializer:</h3>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithFilePath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>initPath
&nbsp;
<span style="color: #002200;">&#123;</span>
&nbsp;
self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//set the file path</span>
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>initPath <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
self.filePath <span style="color: #002200;">=</span> initPath;
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">return</span> self;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Nothing special, just making sure every instance of the model is related to a file path. So since all the file contents are also class properties, I can use them to  fill in my file&#8217;s instance with content and then I will need a save method to save to the file system. And what I&#8217;d need is create a temp folder, save all my data as files there, and them zip&#8217;em! If you have a look in the code below, now that you know how to use ZipArchive the code is actually very straightforward:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>saveFile
&nbsp;
<span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//create a temp directory</span>
&nbsp;
<span style="color: #400080;">NSFileManager</span><span style="color: #002200;">*</span> fileManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> documentsDir <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>filePath stringByDeletingLastPathComponent<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>tmpPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>documentsDir stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tmp&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>fileManager createDirectoryAtPath<span style="color: #002200;">:</span>tmpPath attributes<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
ZipArchive <span style="color: #002200;">*</span>zip <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ZipArchive alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip CreateZipFile2<span style="color: #002200;">:</span>self.filePath<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//save the texts</span>
&nbsp;
<span style="color: #400080;">NSDictionary</span><span style="color: #002200;">*</span> texts <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>self.title,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;title&quot;</span>,self.text,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span>,<span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> textsFile <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tmpPath stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;texts.plist&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//save the image and add them to the zip file</span>
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self.photo<span style="color: #002200;">!=</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> photoFile <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tmpPath stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photo0.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>imageData <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSData</span> dataWithData<span style="color: #002200;">:</span>UIImagePNGRepresentation<span style="color: #002200;">&#40;</span>self.photo<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>imageData writeToFile<span style="color: #002200;">:</span>photoFile atomically<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip addFileToZip<span style="color: #002200;">:</span>photoFile newname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photo0.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>fileManager removeItemAtPath<span style="color: #002200;">:</span>photoFile error<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#91;</span>texts writeToFile<span style="color: #002200;">:</span>textsFile atomically<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip addFileToZip<span style="color: #002200;">:</span>textsFile newname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;texts.plist&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>fileManager removeItemAtPath<span style="color: #002200;">:</span>textsFile error<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>zip CloseZipFile2<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// error handler here</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#91;</span>fileManager removeItemAtPath<span style="color: #002200;">:</span>tmpPath error<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip release<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>I am just putting the texts in a NSDictionary and saving it as a plist file, and then saving the image as a separate PNG file.  Then I zip everything and remove all traces. Note how I create my temporary folder in the same location where the file is going to be saved (assuming it is indeed writable) Your own document centric application is probably much greater than putting together some texts and an image, but I&#8217;m sure this example already gives you the right direction.</p>
<p>Now that I have already my saving method, it&#8217;s very easy to put together also the one that reads the data from the file system.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>loadFile
&nbsp;
<span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//create a temp directory</span>
&nbsp;
<span style="color: #400080;">NSFileManager</span><span style="color: #002200;">*</span> fileManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> documentsDir <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>filePath stringByDeletingLastPathComponent<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>tmpPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>documentsDir stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tmp&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>fileManager createDirectoryAtPath<span style="color: #002200;">:</span>tmpPath attributes<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
ZipArchive <span style="color: #002200;">*</span>zip <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ZipArchive alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">BOOL</span> result <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
&nbsp;
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>zip UnzipOpenFile<span style="color: #002200;">:</span>filePath<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//zip file is there</span>
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>zip UnzipFileTo<span style="color: #002200;">:</span>tmpPath overWrite<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//unzipped successfully</span>
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Archive unzip Success&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
result<span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
&nbsp;
<span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Failure To Extract Archive, maybe password?&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span>  <span style="color: #002200;">&#123;</span>
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Failure To Open Archive&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>To load the file contents I have very similar approach: I get the directory of the file and create a temp folder in the same place and then I unzip it inside. Spoiler alert: All my documents are going to be saved in the Documents folder which is always writable, so no worries whether this little temp folder of mine can be crated or not. I chose for the Documents folder as this is the folder which you application can share with your computer via iTunes (in OS 3.2+)</p>
<p>Then onwards is also very simple, now the contents are extracted just load them back into the class instance:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>result<span style="color: #002200;">==</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> textFile <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tmpPath stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;texts.plist&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSDictionary</span><span style="color: #002200;">*</span> texts <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithContentsOfFile<span style="color: #002200;">:</span>textFile<span style="color: #002200;">&#93;</span>;
&nbsp;
self.title <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>texts objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;title&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
self.text  <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>texts objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> tmpPhotoPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tmpPath stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photo0.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>fileManager fileExistsAtPath<span style="color: #002200;">:</span>tmpPhotoPath<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
self.photo <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageWithData<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSData</span> dataWithContentsOfFile<span style="color: #002200;">:</span>tmpPhotoPath<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//do cleanup</span>
&nbsp;
<span style="color: #002200;">&#91;</span>fileManager removeItemAtPath<span style="color: #002200;">:</span>tmpPath error<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip release<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">return</span> result;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Now that I have my model working (a simple class to read and write my custom file format) I&#8217;ll create also the GUI of my example application &#8211; I assume you are already familiar with creating UITableControllers and binding GUI elements to your classes so I won&#8217;t go into details about those. I&#8217;ll just quickly make a resume of the app&#8217;s idea and let you dig into the source code, which is available for download at the end of the article.</p>
<p>I&#8217;ll create my main view controller to load the names of all files in my Documents directory which have extension &#8220;.mtt&#8221; &#8211; those are the files my application create and show them in a table:</p>
<p><a href="/wp-content/uploads/2010/04/DocumentsList1.png"><img class="alignnone size-medium wp-image-1949" src="/wp-content/uploads/2010/04/DocumentsList-170x300.png" alt="" width="170" height="300" /></a></p>
<p>My second view controller will be the application work area &#8211; a screen where you can load an image from your photo gallery and enter some texts (again if you are not familiar with the techniques to do all these, read a more introductiory article about iPhone programming)</p>
<p><a href="/wp-content/uploads/2010/04/EditorScreen1.png"><img class="alignnone size-medium wp-image-1950" src="/wp-content/uploads/2010/04/EditorScreen-166x300.png" alt="" width="166" height="300" /></a></p>
<p>So the idea of the demo app is simple, initially shows you a list of the available documents, if you click on one of them, the CustomFile class unarchives it and loads its contents into the second view controller, there you can change the contents and hit Save Document. If you want to create  a new document you just choose the &#8220;New Document &#8230;&#8221; item from the list and this will just load the details view controller empty, so you can edit and save. Just download the source and run the app to get a feeling of the app and make sure to check the source to see how ZipArchive is being used.</p>
<h3>Wrap up</h3>
<p>I hope this article has been useful &#8211; I showcased ZipArchive and also did put some ideas together on how to create more elaborate document centric applications. The custom file format can be developed a lot further and the interface for editing multimedia contents could be improved dramatically on the iPad&#8217;s big screen.</p>
<p>The full source code of the demo application you can <a href="/wp-content/uploads/2010/04/iPhoneCustomFileFormat1.zip">download here</a>.</p>
<p>If you have comments or questions, please write in the comments below or <a title="Icanzil on Twitter" href="http://www.twitter.com/icanzilb">ping me in twitter</a>!</p>
<p>I wish you happy iCoding !</p>
<p>Marin Todorov</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/04/12/creating-a-document-centric-iphoneipad-application-with-own-file-format-using-ziparchive/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
