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:com.sldeditor.test.unit.tool.vector.VectorToolTest.java

/**
 * Unzip a zip file containing shp file.
 *
 * @param zipFilePath the zip file path//from ww  w.  j a va2  s . c om
 * @param destDirectory the dest directory
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static void unzip(String zipFilePath, String destDirectory) throws IOException {
    File destDir = new File(destDirectory);
    if (!destDir.exists()) {
        destDir.mkdir();
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    // iterates over entries in the zip file
    while (entry != null) {
        String filePath = destDirectory + File.separator + entry.getName();
        if (!entry.isDirectory()) {
            // if the entry is a file, extracts it
            extractFile(zipIn, filePath);
        } else {
            // if the entry is a directory, make the directory
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

From source file:com.ibm.amc.FileManager.java

public static File decompress(URI temporaryFileUri) {
    final File destination = new File(getUploadDirectory(), temporaryFileUri.getSchemeSpecificPart());
    if (!destination.mkdirs()) {
        throw new AmcRuntimeException(Status.INTERNAL_SERVER_ERROR, "CWZBA2001E_DIRECTORY_CREATION_FAILED",
                destination.getPath());/*from w w w.  j  a va  2  s.c  o  m*/
    }

    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(getFileForUri(temporaryFileUri));

        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File newDirOrFile = new File(destination, entry.getName());
            if (newDirOrFile.getParentFile() != null && !newDirOrFile.getParentFile().exists()) {
                if (!newDirOrFile.getParentFile().mkdirs()) {
                    throw new AmcRuntimeException(Status.INTERNAL_SERVER_ERROR,
                            "CWZBA2001E_DIRECTORY_CREATION_FAILED", newDirOrFile.getParentFile().getPath());
                }
            }
            if (entry.isDirectory()) {
                if (!newDirOrFile.mkdir()) {
                    throw new AmcRuntimeException(Status.INTERNAL_SERVER_ERROR,
                            "CWZBA2001E_DIRECTORY_CREATION_FAILED", newDirOrFile.getPath());
                }
            } else {
                BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
                int size;
                byte[] buffer = new byte[ZIP_BUFFER];
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newDirOrFile),
                        ZIP_BUFFER);
                while ((size = bis.read(buffer, 0, ZIP_BUFFER)) != -1) {
                    bos.write(buffer, 0, size);
                }
                bos.flush();
                bos.close();
                bis.close();
            }
        }
    } catch (Exception e) {
        throw new AmcRuntimeException(e);
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
                logger.debug("decompress", "close failed with " + e);
            }
        }
    }

    return destination;
}

From source file:net.technicpack.utilslib.ZipUtils.java

public static void unzipFile(File zip, File output, IZipFileFilter fileFilter, DownloadListener listener)
        throws IOException, InterruptedException {
    if (!zip.exists()) {
        Utils.getLogger().log(Level.SEVERE, "File to unzip does not exist: " + zip.getAbsolutePath());
        return;//  w  w  w  . j  av a 2s . c om
    }
    if (!output.exists()) {
        output.mkdirs();
    }

    ZipFile zipFile = new ZipFile(zip);
    int size = zipFile.size() + 1;
    int progress = 1;
    try {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            if (Thread.interrupted())
                throw new InterruptedException();

            ZipEntry entry = null;

            try {
                entry = entries.nextElement();
            } catch (IllegalArgumentException ex) {
                //We must catch & rethrow as a zip exception because some crappy code in the zip lib will
                //throw illegal argument exceptions for malformed zips.
                throw new ZipException("IllegalArgumentException while parsing next element.");
            }

            if (!entry.getName().contains("../")
                    && (fileFilter == null || fileFilter.shouldExtract(entry.getName()))) {
                File outputFile = new File(output, entry.getName());

                if (outputFile.getParentFile() != null) {
                    outputFile.getParentFile().mkdirs();
                }

                if (!entry.isDirectory()) {
                    unzipEntry(zipFile, entry, outputFile);
                }
            }

            if (listener != null) {
                float totalProgress = (float) progress / (float) size;
                listener.stateChanged("Extracting " + entry.getName() + "...", totalProgress * 100.0f);
            }
            progress++;
        }
    } finally {
        zipFile.close();
    }
}

From source file:moskitt4me.repositoryClient.core.util.RepositoryClientUtil.java

public static void extractZipFile(String folder, String zipName) throws Exception {

    ZipFile zipFile = new ZipFile(new File(folder + "/" + zipName));

    Enumeration entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        Object obj = entries.nextElement();
        if (obj instanceof ZipEntry) {
            ZipEntry entry = (ZipEntry) obj;

            InputStream eis = zipFile.getInputStream(entry);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;

            File f = new File(folder + "/" + entry.getName());

            if (entry.isDirectory()) {
                f.mkdirs();//from w  w  w.  jav a2  s. c o m
                eis.close();
                continue;
            } else {
                f.getParentFile().mkdirs();
                f.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(f);

            while ((bytesRead = eis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }

            if (eis != null) {
                eis.close();
            }

            if (fos != null) {
                fos.close();
            }
        }
    }

    zipFile.close();
}

From source file:fridgegameinstaller.installation.java

public static void unzip(ZipFile zipFile, File jiniHomeParentDir) {

    Enumeration files = zipFile.entries();
    File f = null;//  ww  w.  ja  v  a2s .  c  om
    FileOutputStream fos = null;

    while (files.hasMoreElements()) {
        try {
            ZipEntry entry = (ZipEntry) files.nextElement();
            InputStream eis = zipFile.getInputStream(entry);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;

            f = new File(jiniHomeParentDir.getAbsolutePath() + File.separator + entry.getName());

            if (entry.isDirectory()) {
                f.mkdirs();
                continue;
            } else {
                f.getParentFile().mkdirs();
                f.createNewFile();
            }

            fos = new FileOutputStream(f);

            while ((bytesRead = eis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
            continue;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }
}

From source file:org.ambraproject.article.service.XslIngestArchiveProcessor.java

/**
 * Generate a description for a single zip-entry.
 *
 * @param ze  the zip entry to describe.
 * @param buf the buffer to place the description into
 *///from   www.  j  av  a2 s . c o  m
private static void entry2xml(ZipEntry ze, StringBuilder buf) {
    buf.append("<ZipEntry name=\"").append(attrEscape(ze.getName())).append("\"");

    if (ze.isDirectory())
        buf.append(" isDirectory=\"true\"");
    if (ze.getCrc() >= 0)
        buf.append(" crc=\"").append(ze.getCrc()).append("\"");
    if (ze.getSize() >= 0)
        buf.append(" size=\"").append(ze.getSize()).append("\"");
    if (ze.getCompressedSize() >= 0)
        buf.append(" compressedSize=\"").append(ze.getCompressedSize()).append("\"");
    if (ze.getTime() >= 0)
        buf.append(" time=\"").append(ze.getTime()).append("\"");

    if (ze.getComment() != null || ze.getExtra() != null) {
        buf.append(">\n");

        if (ze.getComment() != null)
            buf.append("<Comment>").append(xmlEscape(ze.getComment())).append("</Comment>\n");
        if (ze.getExtra() != null)
            buf.append("<Extra>").append(base64Encode(ze.getExtra())).append("</Extra>\n");

        buf.append("</ZipEntry>\n");
    } else {
        buf.append("/>\n");
    }
}

From source file:com.mhs.hboxmaintenanceserver.utils.Utils.java

/**
 * Unzip it/* w w w . jav  a  2 s . c o  m*/
 *
 * @param zipFile input zip file
 * @param outputFolder
 * @throws java.io.FileNotFoundException
 */
public static void unzip(String zipFile, String outputFolder) throws FileNotFoundException, IOException {
    byte[] buffer = new byte[1024];

    try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) {
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {

            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);
            if (ze.isDirectory()) {
                newFile.mkdirs();
            } else {
                try (FileOutputStream fos = new FileOutputStream(newFile)) {
                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                    }
                    fos.close();
                }
            }
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();
    }
}

From source file:net.technicpack.launchercore.util.ZipUtils.java

/**
 * Unzips a file into the specified directory.
 *
 * @param zip          file to unzip/*from  w  ww .  j a  v  a 2s  .c  om*/
 * @param output       directory to unzip into
 * @param extractRules extractRules for this zip file. May be null indicating no rules.
 * @param listener     to update progress on - may be null for no progress indicator
 */
public static void unzipFile(File zip, File output, ExtractRules extractRules, DownloadListener listener)
        throws IOException {
    if (!zip.exists()) {
        Utils.getLogger().log(Level.SEVERE, "File to unzip does not exist: " + zip.getAbsolutePath());
        return;
    }
    if (!output.exists()) {
        output.mkdirs();
    }

    ZipFile zipFile = new ZipFile(zip);
    int size = zipFile.size() + 1;
    int progress = 1;
    try {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = null;

            try {
                entry = entries.nextElement();
            } catch (IllegalArgumentException ex) {
                //We must catch & rethrow as a zip exception because some crappy code in the zip lib will
                //throw illegal argument exceptions for malformed zips.
                throw new ZipException("IllegalArgumentException while parsing next element.");
            }

            if ((extractRules == null || extractRules.shouldExtract(entry.getName()))
                    && !entry.getName().contains("../")) {
                File outputFile = new File(output, entry.getName());

                if (outputFile.getParentFile() != null) {
                    outputFile.getParentFile().mkdirs();
                }

                if (!entry.isDirectory()) {
                    unzipEntry(zipFile, entry, outputFile);
                }
            }

            if (listener != null) {
                float totalProgress = (float) progress / (float) size;
                listener.stateChanged("Extracting " + entry.getName() + "...", totalProgress * 100.0f);
            }
            progress++;
        }
    } finally {
        zipFile.close();
    }
}

From source file:JarResources.java

private String dumpZipEntry(ZipEntry ze) {
    StringBuffer sb = new StringBuffer();
    if (ze.isDirectory()) {
        sb.append("d ");
    } else {//from  w  w  w  .  j ava 2 s. c o  m
        sb.append("f ");
    }

    if (ze.getMethod() == ZipEntry.STORED) {
        sb.append("stored   ");
    } else {
        sb.append("defalted ");
    }

    sb.append(ze.getName());
    sb.append("\t");
    sb.append("" + ze.getSize());
    if (ze.getMethod() == ZipEntry.DEFLATED) {
        sb.append("/" + ze.getCompressedSize());
    }

    return (sb.toString());
}

From source file:JarResource.java

private List<String> load(File jarFile) throws IOException {
    List<String> jarContents = new ArrayList<String>();
    try {// w w  w  .java 2  s.  co  m
        ZipFile zf = new ZipFile(jarFile);
        for (Enumeration e = zf.entries(); e.hasMoreElements();) {
            ZipEntry ze = (ZipEntry) e.nextElement();
            if (ze.isDirectory()) {
                continue;
            }
            jarContents.add(ze.getName());
        }
    } catch (NullPointerException e) {
        System.out.println("done.");
    } catch (ZipException ze) {
        ze.printStackTrace();
    }
    return jarContents;
}