Java Unzip File unzip(File zipfile, File directory)

Here you can find the source of unzip(File zipfile, File directory)

Description

unzip

License

Open Source License

Declaration

public static void unzip(File zipfile, File directory) throws IOException 

Method Source Code

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

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

import java.util.Enumeration;

import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    public static void unzip(File zipfile, File directory) throws IOException {
        ZipFile zfile = new ZipFile(zipfile);
        Enumeration<? extends ZipEntry> entries = zfile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File file = new File(directory, entry.getName());
            if (entry.isDirectory()) {
                file.mkdirs();/*from  w ww  .  j a  va2 s  .co m*/
            } else {
                file.getParentFile().mkdirs();
                InputStream in = zfile.getInputStream(entry);
                try {
                    copy(in, file);
                } finally {
                    in.close();
                }
            }
        }
    }

    private static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        while (true) {
            int readCount = in.read(buffer);
            if (readCount < 0) {
                break;
            }
            out.write(buffer, 0, readCount);
        }
    }

    private static void copy(File file, OutputStream out) throws IOException {
        InputStream in = new FileInputStream(file);
        try {
            copy(in, out);
        } finally {
            in.close();
        }
    }

    private static void copy(InputStream in, File file) throws IOException {
        OutputStream out = new FileOutputStream(file);
        try {
            copy(in, out);
        } finally {
            out.close();
        }
    }
}

Related

  1. unzip(File zipFile, File destDir)
  2. unzip(File zipFile, File destDir)
  3. unzip(File zipFile, File destDir)
  4. unzip(File zipFile, File destination)
  5. unzip(File zipFile, File destination, IProgressMonitor monitor)
  6. unzip(File zipfile, File directory)
  7. unzip(File zipFile, File outputDir)
  8. unzip(File zipFile, File outputFolder)
  9. unZip(File zipFile, String desdir)