Monday 21 April 2014

How to Programmatically Zip and Unzip File in Android

How to Zip files.

1) Persmissions in Manifest File.

Crete a sample android activity and add the following permission to application Mainfest.xml file. These persmissions are required to store data to your device storage.

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 
2) In Java File
 
 
public void zip(String[] _files, String zipFileName) {
        try {
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(zipFileName);
            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));
                out.putNextEntry(entry);
                int count;
 
                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }
 
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
~You can use this in your activity~
 
// declare an array for storing the files i.e the path
// of your source files
String[] s = new String[2];
 
// Type the path of the files in here
s[0] = inputPath + "/image.jpg";
s[1] = inputPath + "/textfile.txt"; // /sdcard/ZipDemo/textfile.txt
 
// first parameter is d files second parameter is zip file name
ZipManager zipManager = new ZipManager();
 
// calling the zip function
zipManager.zip(s, inputPath + inputFile);
 
 
 

How to UnZip files.

1) Persmissions in Manifest File.

Crete a sample android activity and add the following permission to application Mainfest.xml file. These persmissions are required to store data to your device storage.

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 
2) In Java File
 
public void unzip(String _zipFile, String _targetLocation) {
 
        //create target location folder if not exist
        dirChecker(_targetLocatioan);
 
        try {
            FileInputStream fin = new FileInputStream(_zipFile);
            ZipInputStream zin = new ZipInputStream(fin);
            ZipEntry ze = null;
            while ((ze = zin.getNextEntry()) != null) {
 
                //create dir if required while unzipping
                if (ze.isDirectory()) {
                    dirChecker(ze.getName());
                } else {
                    FileOutputStream fout = new FileOutputStream(_targetLocation + ze.getName());
                    for (int c = zin.read(); c != -1; c = zin.read()) {
                        fout.write(c);
                    }
 
                    zin.closeEntry();
                    fout.close();
                }
 
            }
            zin.close();
        } catch (Exception e) {
            System.out.println(e);
        }
}
 
 
~You can use this Method in your activity~
 
ZipManager zipManager = new ZipManager();
zipManager.unzip(inputPath + inputFile, outputPath);
 
 
 
 
 
 
 
 
 
 
 


5 comments:

  1. Nice learning tutorial on zipping and unzipping files in android through coding. Being an active member of many android developer forum, I'm continuously looking to enhance tech skills through proper engagement with fellow tech nerds.

    ReplyDelete
    Replies
    1. Kirit Bhayani: How To Programmatically Zip And Unzip File In Android >>>>> Download Now

      >>>>> Download Full

      Kirit Bhayani: How To Programmatically Zip And Unzip File In Android >>>>> Download LINK

      >>>>> Download Now

      Kirit Bhayani: How To Programmatically Zip And Unzip File In Android >>>>> Download Full

      >>>>> Download LINK 5z

      Delete
  2. Hi. I want to zip more then one directory. That mean I have 2 inner directory and 1 parent directory. Now I want to zip the parent directory. How to do this. Please let me any idea to solve this.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Kirit Bhayani: How To Programmatically Zip And Unzip File In Android >>>>> Download Now

    >>>>> Download Full

    Kirit Bhayani: How To Programmatically Zip And Unzip File In Android >>>>> Download LINK

    >>>>> Download Now

    Kirit Bhayani: How To Programmatically Zip And Unzip File In Android >>>>> Download Full

    >>>>> Download LINK gW

    ReplyDelete