Java Unzip File unzip(File zipFile, File dest)

Here you can find the source of unzip(File zipFile, File dest)

Description

unzip

License

Open Source License

Declaration

static public void unzip(File zipFile, File dest) 

Method Source Code


//package com.java2s;
/*/*from w  w  w . ja v  a2s.c  om*/
  Part of the Processing project - http://processing.org
    
  Copyright (c) 2012-15 The Processing Foundation
  Copyright (c) 2004-12 Ben Fry and Casey Reas
    
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  version 2, as published by the Free Software Foundation.
    
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
    
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import java.io.*;

import java.util.zip.*;

public class Main {
    static public void unzip(File zipFile, File dest) {
        try {
            FileInputStream fis = new FileInputStream(zipFile);
            CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32());
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum));
            ZipEntry next = null;
            while ((next = zis.getNextEntry()) != null) {
                File currentFile = new File(dest, next.getName());
                if (next.isDirectory()) {
                    currentFile.mkdirs();
                } else {
                    File parentDir = currentFile.getParentFile();
                    // Sometimes the directory entries aren't already created
                    if (!parentDir.exists()) {
                        parentDir.mkdirs();
                    }
                    currentFile.createNewFile();
                    unzipEntry(zis, currentFile);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static protected void unzipEntry(ZipInputStream zin, File f) throws IOException {
        FileOutputStream out = new FileOutputStream(f);
        byte[] b = new byte[512];
        int len = 0;
        while ((len = zin.read(b)) != -1) {
            out.write(b, 0, len);
        }
        out.flush();
        out.close();
    }
}

Related

  1. unzip(File zip, File targetDir)
  2. unzip(File zip, File toDir)
  3. unzip(File zip, File toDir)
  4. unzip(File zip, String folder, File into)
  5. unZip(File zipf, String targetDir)
  6. unzip(File zipFile, File destDir)
  7. unzip(File zipFile, File destDir)
  8. unzip(File zipFile, File destDir)
  9. unzip(File zipFile, File destDir)