Java Unzip File unZip(File file, String outPath, String zipFileName)

Here you can find the source of unZip(File file, String outPath, String zipFileName)

Description

un Zip

License

Apache License

Declaration

public static boolean unZip(File file, String outPath, String zipFileName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    public static boolean unZip(File file, String outPath, String zipFileName) {
        FileInputStream is = null;
        try {//from   www.j a va 2  s. c  o  m
            is = new FileInputStream(file);
            return unZip(is, outPath, zipFileName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    public static boolean unZip(InputStream is, String outPath, String zipFileName) {
        ZipInputStream inZip = null;
        try {
            inZip = new ZipInputStream(is);
            ZipEntry zipEntry;
            String szName;
            while ((zipEntry = inZip.getNextEntry()) != null) {
                szName = zipEntry.getName();
                if (zipFileName != null && szName.startsWith(zipFileName + "/")) {
                    szName = szName.substring(zipFileName.length() + 1);
                }
                if ("".equals(szName)) {
                    continue;
                }

                if (zipEntry.isDirectory()) {
                    if (szName.endsWith("/")) {
                        szName = szName.substring(0, szName.length() - 1);
                    }
                    File folder = new File(outPath + File.separator + szName);
                    folder.mkdirs();
                } else {

                    File file = new File(outPath + File.separator + szName);
                    file.createNewFile();

                    FileOutputStream out = null;
                    try {
                        out = new java.io.FileOutputStream(file);
                        int len;
                        byte[] buffer = new byte[1024];
                        while ((len = inZip.read(buffer)) != -1) {
                            out.write(buffer, 0, len);
                        }
                        out.flush();
                    } finally {
                        if (out != null) {
                            try {
                                out.close();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        } finally {
            if (inZip != null) {
                try {
                    inZip.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }
}

Related

  1. unzip(File file, File targetDirectory)
  2. unzip(File file, File targetDirectory)
  3. unzip(File file, File toDir)
  4. unzip(File file, File toDirectory)
  5. unzip(File file, String destination, String container)
  6. unzip(File file, String targetDirectory)
  7. unzip(File fileToUnzip, File destinationDirectory)
  8. unZip(File inFile, File unzipDir)
  9. unzip(File input)