Example usage for java.util.zip ZipEntry isDirectory

List of usage examples for java.util.zip ZipEntry isDirectory

Introduction

In this page you can find the example usage for java.util.zip ZipEntry isDirectory.

Prototype

public boolean isDirectory() 

Source Link

Document

Returns true if this is a directory entry.

Usage

From source file:net.sourceforge.floggy.maven.ZipUtils.java

/**
 * DOCUMENT ME!/*from  www .  ja va 2  s .  c  om*/
*
* @param file DOCUMENT ME!
* @param directory DOCUMENT ME!
*
* @throws IOException DOCUMENT ME!
*/
public static void unzip(File file, File directory) throws IOException {
    ZipFile zipFile = new ZipFile(file);
    Enumeration entries = zipFile.entries();

    if (!directory.exists() && !directory.mkdirs()) {
        throw new IOException("Unable to create the " + directory + " directory!");
    }

    while (entries.hasMoreElements()) {
        File temp;
        ZipEntry entry = (ZipEntry) entries.nextElement();

        if (entry.isDirectory()) {
            temp = new File(directory, entry.getName());

            if (!temp.exists() && !temp.mkdirs()) {
                throw new IOException("Unable to create the " + temp + " directory!");
            }
        } else {
            temp = new File(directory, entry.getName());
            IOUtils.copy(zipFile.getInputStream(entry), new FileOutputStream(temp));
        }
    }

    zipFile.close();
}

From source file:com.predic8.membrane.examples.DistributionExtractingTestcase.java

public static final void unzip(File zip, File target) throws IOException {
    ZipFile zipFile = new ZipFile(zip);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            // Assume directories are stored parents first then children.
            // This is not robust, just for demonstration purposes.
            new File(target, entry.getName()).mkdir();
        } else {//from w  w  w.  j  a  v a  2  s .c o m
            FileOutputStream fos = new FileOutputStream(new File(target, entry.getName()));
            try {
                copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(fos));
            } finally {
                fos.close();
            }
        }
    }
    zipFile.close();
}

From source file:org.apereo.portal.utils.ZipUtils.java

/**
 * Creates any required parent directories, returns the File to extract the entry to, returns null if there is no file to extract (such as a directory entry)
 *//*ww  w  .  j a va  2 s  . co m*/
protected static File checkDirectories(ZipEntry entry, File outputDir) {
    final String name = entry.getName();

    if (entry.isDirectory()) {
        createDir(new File(outputDir, name));
        return null;
    }

    final File outputFile = new File(outputDir, name);
    if (!outputFile.getParentFile().exists()) {
        createDir(outputFile.getParentFile());
    }

    log.debug("Extracting " + name);
    return outputFile;
}

From source file:Main.java

/**
 * Reads the in_stream and extracts them to out_dir.
 * @param in_stream Input stream corresponding to the zip file.
 * @param out_dir Output directory for the zip file contents.
 * @throws IOException/*from   w  w  w  . java 2s  .  c o  m*/
 */
public static void zipExtract(InputStream in_stream, File out_dir) throws IOException {
    if (!out_dir.exists()) {
        if (!out_dir.mkdirs()) {
            throw new IOException("Could not create output directory: " + out_dir.getAbsolutePath());
        }
    }
    ZipInputStream zis = new ZipInputStream(in_stream);
    ZipEntry ze;
    byte[] buffer = new byte[BUFFER_SIZE];
    int count;
    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            File fmd = new File(out_dir.getAbsolutePath() + "/" + ze.getName());
            fmd.mkdirs();
            continue;
        }
        FileOutputStream fout = new FileOutputStream(out_dir.getAbsolutePath() + "/" + ze.getName());
        while ((count = zis.read(buffer)) != -1) {
            fout.write(buffer, 0, count);
        }

        fout.close();
        zis.closeEntry();
    }
    zis.close();
}

From source file:org.paxml.bean.UnzipTag.java

public static void unzip(File file, File dir) {
    dir.mkdirs();//  w  ww  .j  a  v a 2 s .  c o  m
    ZipFile zipFile = null;

    try {
        zipFile = new ZipFile(file);

        Enumeration entries = zipFile.entries();

        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();

            if (entry.isDirectory()) {

                new File(dir, entry.getName()).mkdirs();
                continue;
            }

            InputStream in = null;
            OutputStream out = null;
            try {
                zipFile.getInputStream(entry);
                out = new BufferedOutputStream(new FileOutputStream(entry.getName()));
                IOUtils.copy(in, out);
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
            }
        }

    } catch (IOException ioe) {

        throw new PaxmlRuntimeException(
                "Cannot unzip file: " + file.getAbsolutePath() + " under dir: " + dir.getAbsolutePath(), ioe);

    } finally {

        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
                // do nothing
            }
        }
    }
}

From source file:Main.java

public static void unCompress(String zipPath, String toPath) throws IOException {
    File zipfile = new File(zipPath);
    if (!zipfile.exists())
        return;/*  w ww .ja v  a  2  s  .c  om*/

    if (!toPath.endsWith("/"))
        toPath += "/";

    File destFile = new File(toPath);
    if (!destFile.exists())
        destFile.mkdirs();

    ZipInputStream zis = new ZipInputStream(new FileInputStream(zipfile));
    ZipEntry entry = null;

    try {

        while ((entry = zis.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                File file = new File(toPath + entry.getName() + "/");
                file.mkdirs();
            } else {
                File file = new File(toPath + entry.getName());
                if (!file.getParentFile().exists())
                    file.getParentFile().mkdirs();

                FileOutputStream fos = null;

                try {
                    fos = new FileOutputStream(file);
                    byte buf[] = new byte[1024];
                    int len = -1;
                    while ((len = zis.read(buf, 0, 1024)) != -1) {
                        fos.write(buf, 0, len);
                    }
                } finally {

                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (Exception e) {
                        }
                    }
                }
            }
        }

    } finally {
        zis.close();
    }

}

From source file:gov.va.oia.terminology.converters.sharedUtils.Unzip.java

public final static void unzip(File zipFile, File rootDir) throws IOException {
    ZipFile zip = new ZipFile(zipFile);
    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        java.io.File f = new java.io.File(rootDir, entry.getName());
        if (entry.isDirectory()) {
            f.mkdirs();//from  ww w  .  j a v a  2 s . c o m
            continue;
        } else {
            f.createNewFile();
        }
        InputStream is = null;
        OutputStream os = null;
        try {
            is = zip.getInputStream(entry);
            os = new FileOutputStream(f);
            IOUtils.copy(is, os);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                    // noop
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (Exception e) {
                    // noop
                }
            }
        }
    }
    zip.close();
}

From source file:com.compomics.pladipus.core.control.util.ZipUtils.java

private static void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException {

    if (entry.isDirectory()) {
        createDir(new File(outputDir, entry.getName()));
        return;//from w ww  .  j  a v  a  2s.c  o  m
    }

    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        createDir(outputFile.getParentFile());
    }

    LOGGER.debug("Extracting: " + entry);

    try (BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile))) {
        IOUtils.copy(inputStream, outputStream);
        outputStream.flush();
    }
}

From source file:Main.java

/***
 * Extract zipfile to outdir with complete directory structure
 * disclaimer, I didn't wrote that .../*w w w .  ja va2  s. c o  m*/
 * @param zipfile Input .zip file
 * @param outdir Output directory
 */
public static void extract(File zipfile, File outdir) {
    // todo: replace with more trustful method
    try {
        ZipInputStream zin = new ZipInputStream(new FileInputStream(zipfile));
        ZipEntry entry;
        String name, dir;
        while ((entry = zin.getNextEntry()) != null) {
            name = entry.getName();
            if (entry.isDirectory()) {
                mkdirs(outdir, name);
                continue;
            }
            dir = dirpart(name);
            if (dir != null) {
                mkdirs(outdir, dir);
            }
            extractFile(zin, outdir, name);
        }
        zin.close();
    } catch (IOException err) {
        throw new RuntimeException("Unable to extract quiz", err);
    }
}

From source file:eurecom.constrained.devices.ReadZipFile.java

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

    final Enumeration<? extends ZipEntry> entries = zipfile.entries();
    while (entries.hasMoreElements()) {
        final ZipEntry entry = entries.nextElement();
        final File file = file(directory, entry);
        if (entry.isDirectory()) {
            continue;
        }//from  w  w  w.java 2s.com
        final InputStream input = zipfile.getInputStream(entry);
        try {
            // copy bytes from input to file
        } finally {
            input.close();
        }
    }
}