Working with protected ZIP files on iOS

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.

Getting ready

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:

Getting ready

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:

Getting ready

How to do it...

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')
};

Creating the recipe UI

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:

Creating the recipe UI

The following steps outline how to create the recipe's UI:

  1. First, a 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'
    });
  2. Next add the 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);
  3. Then the 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);
  4. Next the 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);

Creating a password-protected ZIP file

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:

Creating a password-protected ZIP file

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){
  1. The first step in the zip process is to create the onZipCompleted callback method. When the zip process is completed, the module will send the results to this callback method:
      function onZipCompleted(d){
  2. The 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);
        }
      };
  3. Next the 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/';
  4. Finally the 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
      });
    });

    Tip

    The files parameter provides an array of files using the inputDirectory variable. In this example, the files included are the w2.pdf and w4.pdf files included in the Resources/data folder created during the Getting ready section of this recipe.

Unzipping a protected ZIP file

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:

Unzipping a protected ZIP file

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){
  1. The first step in the unzip process is to create the onUnzipCompleted callback method. When the unzip process is completed, the module will send the results to this callback method.
      function onUnzipCompleted(e){
  2. The 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);
        }
      };
  3. The 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';
  4. Finally, the 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
      });
    });

    Note

    All files contained within the zipFileName ZIP file will be unzipped to the root of the directory provided in the outputDirectory parameter.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.125.169