Protected zip files are a common way to exchange, store, and transmit enterprise data. ZIP files are often used to bundle several files together for transmission or storage. As an extra layer of security, all such files should always be protected with a strong password.
The following recipe discusses how to create and unzip protected compressed files on iOS.
This recipe uses the bencoding.zip
native module to work with protected zip files. This module and other code assets can be downloaded from the source provided by the book, or individually through the links provided in the See also section at the end of this recipe. Installing this module in your project is straightforward. Simply copy the modules
folder into the root of your project, and then copy the data
folder into the Resources
directory of your project as shown in the following screenshot:
After copying the mentioned folders, you will need to click on your tiapp.xml file in Titanium Studio and add a reference to the bencoding.zip
module as shown in the following screenshot:
Once you have added the modules
and data
folders to your project, you need to create your application namespaces in the app.js
file and use require
to import the module into your code as the following code snippet demonstrates:
//Create our application namespace var my = { zip : require('bencoding.zip') };
This recipe uses a simple UI to illustrate how to create (zip) and unzip protected ZIP files. The code discussed in this section walks through how to construct the recipe's UI shown in the following screenshot:
The following steps outline how to create the recipe's UI:
Ti.UI.Window
is created to attach all UI elements.var win = Ti.UI.createWindow({ backgroundColor: '#fff', title: 'Protected Zip Example', barColor:'#000',layout:'vertical' });
txtPassword Ti.UI.TextField
control. This control will be used to provide the password to create protected ZIP files or to unzip them. The default is set to foo123
. This is also the password for the included sample file.var txtPassword = Ti.UI.createTextField({ value:'foo123',hintText:'Enter Password', height:45, left:5, right:5, passwordMask:true, borderStyle:Ti.UI.INPUT_BORDERSTYLE_ROUNDED }); win.add(txtPassword);
btnZip Ti.UI.Button
is added to the Ti.UI.Window
. This button will be used to demonstrate how to create a protected ZIP file later in the discussion of this recipe.var btnZip = Ti.UI.createButton({ title:'Zip with Password', top:25, height:45, left:5, right:5 }); win.add(btnZip);
btnUnzip Ti.UI.Button
is added to the Ti.UI.Window
. This button will be used to demonstrate how to unzip a protected ZIP file later in the discussion of this recipe.var btnUnzip = Ti.UI.createButton({ title:'Unzip with Password', top:25, height:45, left:5, right:5 }); win.add(btnUnzip);
This section of this recipe demonstrates how to create a protected ZIP file. The following screenshot shows this recipe in action, creating a protected ZIP file and alerting the user of the output file path:
The created ZIP file is executed when the user clicks on the btnZip Ti.UI.Button
and triggers the click
event to be fired. The following code snippet discusses the actions performed after the click
event is fired:
btnZip.addEventListener('click',function(x){
onZipCompleted
callback method. When the zip process is completed, the module will send the results to this callback method:function onZipCompleted(d){
d
method parameter provides the results from the module. The first step in processing the module results is to check the d.success
property to see if the zip process was successful. If so, the user is alerted of the path of the completed ZIP file. Otherwise, the user is alerted to the error generated in creating the file.if(d.success){ alert('Protected zip created at: ' + d.zip); }else{ alert('failed due to: ' + d.message); } };
writeToZip
and inputDirectory
variables are created. The writeToZip
variable contains the path to the taxforms
.ZIP output file in the app's Documents
directory. The inputDirectory
creates a reference to the Resources/data
created during the Getting ready section of this recipe.var writeToZip = Ti.Filesystem.applicationDataDirectory + '/taxforms.zip'; var inputDirectory = Ti.Filesystem.resourcesDirectory + 'data/';
zip
method is called and this method provides the parameters built earlier in the click
event handler. Once completed, the zip
method provides results to the provided onZipCompleted
callback method.my.zip.zip({ zip: writeToZip, password:txtPassword.value, files: [inputDirectory + 'w2.pdf', inputDirectory + 'w4.pdf'], completed:onZipCompleted }); });
This section of the current recipe demonstrates how to unzip a protected ZIP file. The following screenshot shows this recipe in action, unzipping the contents of a protected file into the app's Documents
directory:
The unzip example is executed when the user taps on the btnUnzip Ti.UI.Button
and triggers the click
event to be fired. The following code snippet discusses the actions performed after the click
event is fired.
btnUnzip.addEventListener('click',function(x){
onUnzipCompleted
callback method. When the unzip process is completed, the module will send the results to this callback method.function onUnzipCompleted(e){
d
method parameter provides the results from the module. The first step in processing the module results is to check the d.success
property to see if the unzip process was successful. If so, the user is alerted of the directory path of the unzipped file. Otherwise, the user is alerted to the error generating the file.if(e.success){ alert('Unzipped to ' + e.outputDirectory); }else{ alert('failed due to: ' + e.message); } };
outputDirectory
and zipFileName
variables are created next. The outputDirectory
variable contains the path to the output directory in the app's Documents
directory. The zipFileName
creates a reference to the Resources/data/taxform.zip
file created during the Getting ready section of this recipe.var outputDirectory = Ti.Filesystem.applicationDataDirectory; var zipFileName = Ti.Filesystem.resourcesDirectory + 'data/taxforms.zip';
unzip
method is called and this method provides the parameters built earlier in the click
event handler. Once completed, the unzip
method provides results to the provided onUnzipCompleted
callback method.my.zip.unzip({ outputDirectory:outputDirectory, zip:zipFileName, overwrite:true, completed:onUnzipCompleted }); });
benCoding.Zip
native module. To read the documentation and to learn more about this project, please visit https://github.com/benbahrenburg/Zipper.3.145.125.169