n~droid generation

Taking ndroid for a ride…

Android File Compression

Android File Compression

Case: In your application you wish to send a file over the internet, either by mail OR Ftp, & you wish to compress the file/s before it is sent. Situation: Adding a single or multiple files to and archive so it is smaller and faster to send from a device and uses less trasport volume over 3/4G. Solution: For the solution we will be using the following library from Apache.org – commons: Full version just like the original: commons-compress-1.4.1-bin.tar – full OR a minified version I have created: commons-compress-1.4.1.tar – minified Or from apache.org‘s website: commons-compress-1.4.1-bin.tar.gz, where you can also find the signatures for the files. We will also be using apache.org‘s utils.IOUtils from the same library. ** If you wish to know how to add the jars to your project please refer to add jar to android project on Eclipse For the task we will be:

  • Getting an ArrayList<String> of, for example selected pictures (you can do this with other file types).
  • Passing on the tarFile’s name
  • A temp location for creating the compressed file.
  • Returning the tar.gz file

**Note – images of *.jpg type are usually quite compressed already so they will only be packed, other types of files will get compressed. Here’s how: This class is called CompressTar Variables the class will need:

Top to bottom:

  1. The list of files to compress
  2. Local variable to hold received file name
  3. Public variable to hold finale tarred file name
  4. Local variable to hold temp created tar file
  5. Variable to hold g_zziped file
  6. OutputStream for adding files to archive
  7. TarArchiveOutputStream to compress the files

This class can have multiple Constructors. We will focus on one that will help us complete the task. Constructor:

 Explanation: As mentioned in the variable list, the constructor gets a full path ArrayList of files, The name of the archived file & a temporary location for writing the compressed file. Then we:

  1. Assign the received name to the local variable _tarFile,
  2. Set the size of _files to the size of the received ArrayList,
  3. & use the .toArray function to convert n ArrayList to n Array

As you might have noticed I left the iteration through the Array _files so we can get the list of file names in our LogCat. Creating the compressed archive:

 Explanation:

  1. Create a new file
  2. create a file OutputStream for writing the file to the archive
  3. create n TarArchiveOutputStream for the tarred file
  4. Then we iterate through the list of recieved files and create TarArchiveEntries from them so they can be archived
  5. We then open the entry’s size and write the file through the TarArchiveOutputStream while copying it with IOUtils
  6. We close all the open streams and assign the archived file name to the local variable

Hope you found this helpful… Please contribute either by Donating with PayPal or Flatter Me a Little… Cheers. Full code can be found here: CompressTar.java Next up I will be adding android create a compressed Tar Gz file from a Directory, & a android add file to Tar Gz file from a Directory

Leave a Reply

Your email address will not be published. Required fields are marked *


two × = 6

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Development & Design: http://nwg.co.il
  • Google+
  • LinkedIn