Search This Blog

Saturday, 14 April 2012

Zipping File and Folder in android

While we are attaching any folder/file to mail or other attachment so we really need to attach folder with more than one sub folder. So we need to compress it so called zipping . It necessary in Mobile OS as in Windows. So for this android provide ZipOutPutStream and ZipEntry. ZipOutPutStream read complete folder and then ZipEntry zip all the file inside folder to a new compress folder. Zipping File is very easy.If you want dynamically select which file/folder to zipped then see this

import android.util.Log; 
public class Compress { 
  private static final int BUFFER = 2048; 
  private String[] _files; 
  private String _zipFile; 
  public Compress(String[] files, String zipFile) { 
    _files = files; 
    _zipFile = zipFile; 
  public void zip() { 
    try  { 
      BufferedInputStream origin = null; 
      FileOutputStream dest = new FileOutputStream(_zipFile); 
      ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); 
      byte data[] = new byte[BUFFER]; 
      for(int i=0; i < _files.length; i++) { 
        Log.v("Compress", "Adding: " + _files[i]); 
        FileInputStream fi = new FileInputStream(_files[i]); 
        origin = new BufferedInputStream(fi, BUFFER); 
        ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1)); 
        int count; 
        while ((count =, 0, BUFFER)) != -1) { 
          out.write(data, 0, count); 
    } catch(Exception e) { 

In constructor we have two string parameter.first pass array of file inside a folder .Then file path to which zipped folder will save if it is not present then this code will create new zip folder.


  1. I want to read a pdf file in Android . But can not find any library API for this task . Can you write a article on this topic ? It is essential for me ....... Please help ........All of yours article are very nice ..............

    1. Thanks for appreciation..Reading PDF file in android, is complex task in android.So many tutorial are present on this but none of them are tackle this issue efficiently. For this we need to implement ndk native code like C, C++ . and required time. I will start working on that when i finished task, i will provoide it


Feedback always help in improvement. If you have any query suggestion feel free to comment and Keep visiting my blog to encourage me to blogging

Android News and source code