Java Unzip File unzip(File zip, File toDir)

Here you can find the source of unzip(File zip, File toDir)

Description

unzip

License

Open Source License

Declaration

public static void unzip(File zip, File toDir) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w  w w. j  a va 2  s  .  c  o  m
 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     bstefanescu
 */

import java.io.BufferedWriter;
import java.io.File;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStreamWriter;

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

public class Main {
    public static void unzip(File zip, File toDir) throws IOException {
        ZipFile zf = new ZipFile(zip);
        try {
            Enumeration<? extends ZipEntry> entries = zf.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                File file = new File(toDir, entry.getName());
                file.getParentFile().mkdirs();
                if (entry.isDirectory()) {
                    file.mkdir();
                } else {
                    InputStream in = zf.getInputStream(entry);
                    writeTo(in, file);
                    in.close();
                }
            }
        } finally {
            zf.close();
        }
    }

    public static void writeTo(String data, File file) throws IOException {
        FileWriter w = new FileWriter(file);
        try {
            w.write(data);
        } finally {
            w.close();
        }
    }

    public static void writeTo(String data, File file, String charset) throws IOException {
        FileOutputStream out = new FileOutputStream(file);
        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(out, charset));
        try {
            w.write(data);
        } finally {
            w.close();
        }
    }

    public static void writeTo(InputStream in, File file) throws IOException {
        FileOutputStream out = new FileOutputStream(file);
        try {
            byte[] buf = new byte[1024 * 1024];
            int r = -1;
            while ((r = in.read(buf)) > -1) {
                if (r > 0) {
                    out.write(buf, 0, r);
                }
            }
        } catch (IOException e) {
            file.delete();
            throw e;
        } finally {
            out.close();
        }
    }

    public static String read(InputStream in) throws IOException {
        StringBuilder sb = new StringBuilder();
        int size = in.available();
        if (size < 1 || size > 64 * 1024) {
            size = 64 * 1024;
        }
        byte[] buffer = new byte[size];
        try {
            int read;
            while ((read = in.read(buffer)) != -1) {
                sb.append(new String(buffer, 0, read));
            }
        } finally {
            in.close();
        }
        return sb.toString();
    }
}

Related

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