- OCTO talks ! - http://blog.octo.com/en/ -
Untar on iOS, the pragmatic way
Posted By Mathieu Hausherr On 16/12/2011 @ 15:26 In IT architect's workshop | 5 Comments
Network connections cause latencies on your mobile app. Replace 10 downloads of 1Mb by 1 download of 10Mb is a good improvement to your app.
There’s a well-known unix tool for that: tar.
What is tar? Wikipedia says: “Tar is now commonly used to collect many files into one larger file for distribution or archiving, while preserving file system information such as user and group permissions, dates, and directory structures.” That’s exactly what we need with some extra features.
How to untar an archive file on your iOS device?
A goolge search about untar on iOS provide two solutions:
Is there another way? What if I must push my app in the app store but don’t want to link this 4.7MB of hardly-maintainable code? Let’s code my own light and pragmatic untar implementation.
Libarchive weights 4.7MB but do a lot of things we don’t really need.
We need to untar files but we don’t need:
A tar file is composed of 512 bytes data blocks.
The first block is a header for the first file; the next n blocks are the content of this file and then a new file start with a new header block.
A tar file looks like that: (H is header block, C content block, / partial content block)
At the end of the tar file there is two empty blocks. A new file starts always after an integer number of blocks. Some files haven’t any content block (like folder). Each block has 512 bytes.
A header block comports a lot of datas. Few of these data, and none of the new UStar header data are useful for us.
We just need:
With the file size we know when the header block of the next file starts. And where to strip the content of the last block of the file.
Not so complex isn’t it? I just implemented this code in Objective-C and push it to github: https://github.com/mhausherr/Light-Untar-for-iOS 
This code is under BSD license; you can use it for your own project.
Is it better than libarchive? Light-Untar-for-iOS has only 2 files and 168 lines of code, which include the comment lines with the license text.
To use it, include the .h file and use this NSFileManager method:
[[NSFileManager defaultManager] createFilesAndDirectoriesAtPath:@"/path/to/your/extracted/files/" withTarData:tarData error:&error];
tarData is an NSData. You can create the NSData with this code:
NSData *tarData = [NSData dataWithContentsOfFile:@"/path/to/your/tar/file.tar"];
Libarchive is written directly in C and does the job faster than my Objective-C code.
The question is: what’s the most important thing for you?
If you choose the second one, Light-Untar-for-iOS is for you.
What are major security issues of tar?
Neither of these are implemented in my code, so you can use it safely.
And you know what? It’s an iOS lib. On iOS, applications are sandboxed. The system himself protects this code. We don’t need any other protection.
Mobile development is primarily development on low capacity devices. Size of the build app is also important.
If you have a simple problem to solve, ask you what is the best: A huge framework can do that for you. Do you need really all this framework? Can’t you just implement the requested feature?
Article printed from OCTO talks !: http://blog.octo.com/en/
URL to article: http://blog.octo.com/en/untar-on-ios-the-pragmatic-way/
URLs in this post:
 Tweet: http://twitter.com/share?counturl=http%3A%2F%2Fblog.octo.com%2Fen%2Funtar-on-ios-the-pragmatic-way%2F
 https://github.com/davepeck/iOS-libarchive: https://github.com/davepeck/iOS-libarchive
 Image: http://blog.octo.com/wp-content/uploads/2011/12/Tar-File1.png
 Image: http://blog.octo.com/wp-content/uploads/2011/12/Tar-Block1.png
 https://github.com/mhausherr/Light-Untar-for-iOS: https://github.com/mhausherr/Light-Untar-for-iOS
 http://www.gnu.org/s/tar/manual/html_node/Standard.html: http://www.gnu.org/s/tar/manual/html_node/Standard.html
 http://en.wikipedia.org/wiki/Tar_(file_format: http://en.wikipedia.org/wiki/Tar_(file_format
 http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670: http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670
Copyright © 2010 OCTO talks !. All rights reserved.