How to zip and unzip files in android

Zipping files
/**
 * @param input_files_To_Zip String array of path of files which u want to zip
 * @param output_Zip_Folder_Path path of zipFolder to which these files are to be extracted <br/>
 * <br/> <b>Note: </b> The <b>output_Zip_Folder_Path</b> should contain extension <b>.zip</b> otherwise the zip folder is not created
 * @throws Exception
 * <br/> <b>Note: </b>This method requires permission <b>android.permission.WRITE_EXTERNAL_STORAGE</b>
 */
 public static boolean zipFiles(String[] input_files_To_Zip,
 String output_Zip_Folder_Path)
 {

 boolean success = false;

 final int BUFFER = 2048;

if (output_Zip_Folder_Path != null)
 {
 output_Zip_Folder_Path = output_Zip_Folder_Path.trim();

if ( (!output_Zip_Folder_Path.equals("")) && ( output_Zip_Folder_Path.endsWith(".zip")|| output_Zip_Folder_Path.endsWith(".Zip") || output_Zip_Folder_Path.endsWith(".ZIP") ))
 {
 boolean expection = false;

 try
 {
 FileOutputStream dest = new FileOutputStream(output_Zip_Folder_Path);
 ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));

 if (dest != null && out != null)
 {
 byte data[] = new byte[BUFFER];

 if (input_files_To_Zip != null) {
 int len = input_files_To_Zip.length;
 if (len > 0) {
 BufferedInputStream origin = null;
 for (String filePath : input_files_To_Zip) {
 if (filePath != null)
 {
 File file = new File(filePath);

 FileInputStream fi = new FileInputStream(file);
 origin = new BufferedInputStream(fi, BUFFER);

 ZipEntry entry = new ZipEntry(filePath.substring(filePath.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();
 expection = true;
 }
 success = !expection;
 }
 }
 return success;
 }

Unzipping files

/**
 * @param zipfilePath path of the zip file which is to be unzipped
 * @param extractToFolder target folder path to which all the files are to be copied
 */
 public static boolean unzip(String zipfilePath, String extractToFolder)
 {
 boolean success = false;

 if (zipfilePath != null)
 {
 zipfilePath = zipfilePath.trim();

if (!zipfilePath.equals(""))
 {
 try
 {
 FileInputStream fin = new FileInputStream(zipfilePath);
 ZipInputStream zin = new ZipInputStream(fin);
 ZipEntry ze = null;

 if (fin != null && zin != null)
 {
 while ((ze = zin.getNextEntry()) != null)
 {
 FileOutputStream fout = new FileOutputStream(extractToFolder + "/" + ze.getName());
 for (int c = zin.read(); c != -1; c = zin.read())
 {
 fout.write(c);
 }

 zin.closeEntry();
 fout.close();
 }
 zin.close();
 success = true;
 }
 }
 catch (Exception e)
 {
 e.printStackTrace();
 }
 }
 }
 return success;
 }

Advertisements

Tags: ,

About krishjlk

http://www.facebook.com/krishna.kanth.121

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: