Titanium Appcelerator and Zipfile module

I was working on an Titanium Appcelerator project yesterday where I needed to download a zip file to a mobile device, then extract its contents. After doing a little research, I found the Titanium Zipfile module out on Github. Ultimately, this is exactly what I needed, but there were some stumbling blocks along the way so I thought I’d document them (and the solutions) here.

First off, the latest Zipfile module (as of this writing) is version 0.1.21. This version is not available as a binary download from the Github repository. Fortunately, this post led me to a download for it.

I found this nice piece of code Dan Tamas posted in an answer to a question about downloading image files. I promptly copied that off to a FileManager.js file to ‘require’ into the app.

As I started testing, it wasn’t working and I didn’t know whether the file wasn’t getting downloaded or wasn’t getting extracted. By default, my simulator run configuration was setup for INFO. Turning to DEBUG revealed that it wasn’t able to locate the zip file. After checking that paths were being set correctly, I found this note at the Github project’s issues tab that applies to the 1.8x SDK codestream.

Replacing the following example code from the documentation:

  1. zipfile.extract(  
  2.   Ti.Filesystem.applicationDataDirectory+‘/Archive.zip’,  
  3.   Ti.Filesystem.applicationDataDirectory);

with this:

  1. var appDir = Ti.Network.decodeURIComponent(  
  2.   Ti.Filesystem.applicationDataDirectory).replace(  
  3.   ‘file://localhost’, );  
  4. zipfile.extract(appDir+‘Archive.zip’, appDir);

did in fact solve the issue and everything works great from there.