Java Unzip File unzip(File file, File toDirectory)

Here you can find the source of unzip(File file, File toDirectory)

Description

Unzips a given file to the set directory

License

Open Source License

Parameter

Parameter Description
file The file to unzip
toDirectory The directory to unzip to

Exception

Parameter Description
IOException an exception

Declaration

public static void unzip(File file, File toDirectory) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;

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

public class Main {
    /**/* www  . j  a  va  2  s.  com*/
     * Unzips a given file to the set directory
     * @param file The file to unzip
     * @param toDirectory The directory to unzip to
     * @throws IOException
     */
    public static void unzip(File file, File toDirectory) throws IOException {
        byte[] buffer = new byte[1024];
        if (!toDirectory.exists())
            toDirectory.mkdir();
        ZipInputStream zips = new ZipInputStream(new FileInputStream(file));
        ZipEntry zip = zips.getNextEntry();
        while (zip != null) {
            String fileName = zip.getName();
            File newFile = new File(toDirectory.getPath() + File.separator + fileName);
            new File(newFile.getParent()).mkdirs();
            FileOutputStream fos = new FileOutputStream(newFile);
            int len;
            while ((len = zips.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            zip = zips.getNextEntry();
        }
        zips.closeEntry();
        zips.close();
    }
}

Related

  1. unzip(File file, File destDir)
  2. unzip(File file, File directory)
  3. unzip(File file, File targetDirectory)
  4. unzip(File file, File targetDirectory)
  5. unzip(File file, File toDir)
  6. unzip(File file, String destination, String container)
  7. unZip(File file, String outPath, String zipFileName)
  8. unzip(File file, String targetDirectory)
  9. unzip(File fileToUnzip, File destinationDirectory)