Mar 26, 2012

Unzip zip file using java.util.zip

Example to unzip /mnt/sdcard/test/test.zip file to /mnt/sdcard/test/unzip/ folder.

Unzip zip file using java.util.zip

package com.AndroidUnZip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;

public class AndroidUnZipActivity extends Activity {

String extStorageDirectory;
final String testDir = "/test";
final String unzipDir = "/unzip/";
final String orgZipFile = "test.zip";
String srcFile;
String targetDir;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

CheckExtStorageDirectory();
unzip();
Toast.makeText(AndroidUnZipActivity.this,
"Unzip finished!",
Toast.LENGTH_LONG).show();


}

private void CheckExtStorageDirectory(){
extStorageDirectory = Environment.getExternalStorageDirectory().toString();
srcFile = extStorageDirectory + testDir + "/" + orgZipFile;
targetDir = extStorageDirectory + testDir + unzipDir;
Toast.makeText(AndroidUnZipActivity.this,
srcFile + "\n\n" + targetDir,
Toast.LENGTH_LONG).show();

}

private void unzip(){

final int BUFFER_SIZE = 4096;
BufferedOutputStream bufferedOutputStream = null;
FileInputStream fileInputStream;

try {
fileInputStream = new FileInputStream(srcFile);

ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
ZipEntry zipEntry;

while ((zipEntry = zipInputStream.getNextEntry()) != null){
String zipEntryName = zipEntry.getName();
File file = new File(targetDir + zipEntryName);

if (file.exists()){

}else{
if(zipEntry.isDirectory()){
file.mkdirs();
}else{
byte buffer[] = new byte[BUFFER_SIZE];

FileOutputStream fileOutputStream = new FileOutputStream(file);
bufferedOutputStream
= new BufferedOutputStream(fileOutputStream, BUFFER_SIZE);
int count;

while ((count = zipInputStream.read(buffer, 0, BUFFER_SIZE)) != -1) {
bufferedOutputStream.write(buffer, 0, count);
}

bufferedOutputStream.flush();
bufferedOutputStream.close();
}
}
}
zipInputStream.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}



To create folder and save unzipped files in sdcard, AndroidManifest.xml have to be modified to add permission of "android.permission.WRITE_EXTERNAL_STORAGE".
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidUnZip"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AndroidUnZipActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

1 comment:

  1. Thanks this is right on the money.
    consider adding instructions to create test folder and place a zip folder in it named test.zip

    ReplyDelete

Infolinks In Text Ads