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:io.vertx.config.vault.utils.VaultDownloader.java

private static void unzip(File zipFilePath, File destDir) throws IOException {
    if (!destDir.exists()) {
        destDir.mkdir();/*from  www.  j a va  2 s . c om*/
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    // iterates over entries in the zip file
    while (entry != null) {
        String filePath = destDir.getAbsolutePath() + 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.microsoft.azurebatch.jenkins.utils.ZipHelper.java

private static void unzipFile(File zipFile, String outputFolderPath) throws FileNotFoundException, IOException {
    ZipInputStream zip = null;/*from  w  w w. ja  va2s  .  c  o m*/
    try {
        zip = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry entry;

        while ((entry = zip.getNextEntry()) != null) {
            File entryFile = new File(outputFolderPath, entry.getName());
            if (entry.isDirectory()) {
                if (!entryFile.exists()) {
                    if (!entryFile.mkdirs()) {
                        throw new IOException(String.format("Failed to create folder %s %s", outputFolderPath,
                                entry.getName()));
                    }
                }
            } else {
                // Create parent folder if it's not exist
                File folder = entryFile.getParentFile();
                if (!folder.exists()) {
                    if (!folder.mkdirs()) {
                        throw new IOException(
                                String.format("Failed to create folder %s", folder.getAbsolutePath()));
                    }
                }

                // Create the target file
                if (!entryFile.createNewFile()) {
                    throw new IOException(
                            String.format("Failed to create entry file %s", entryFile.getAbsolutePath()));
                }

                // And rewrite data from stream
                OutputStream os = null;
                try {
                    os = new FileOutputStream(entryFile);
                    IOUtils.copy(zip, os);
                } finally {
                    IOUtils.closeQuietly(os);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(zip);
    }
}

From source file:Main.java

public static void unZip(String zipFile, String outputFolder) {
    byte[] buffer = new byte[BUFFER_SIZE];

    try {/*from   www  .  ja v  a 2  s .  c  o m*/
        File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }

        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);

            System.out.println("file unzip : " + newFile.getAbsoluteFile());
            if (ze.isDirectory()) {
                newFile.mkdirs();
            } else {
                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();
        System.out.println("Done");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:com.nuvolect.securesuite.util.OmniZip.java

public static List<String> getDirsList(OmniFile zipOmni) {

    ZipInputStream zis = new ZipInputStream(zipOmni.getFileInputStream());

    List<String> arrayList = new ArrayList<>();

    ZipEntry entry = null;
    try {/* ww  w.  jav  a  2 s  .c  o  m*/
        while ((entry = zis.getNextEntry()) != null) {

            if (entry.isDirectory())
                arrayList.add(entry.getName());
        }
        zis.close();

    } catch (IOException e) {
        LogUtil.logException(LogUtil.LogType.OMNI_ZIP, e);
    }

    return arrayList;
}

From source file:org.openo.nfvo.jujuvnfmadapter.common.DownloadCsarManager.java

/**
 * unzip CSAR packge//from w ww  .j  a  v  a 2s .  c  o  m
 * @param fileName filePath
 * @return
 */
public static int unzipCSAR(String fileName, String filePath) {
    final int BUFFER = 2048;
    int status = 0;

    try {
        ZipFile zipFile = new ZipFile(fileName);
        Enumeration emu = zipFile.entries();
        int i = 0;
        while (emu.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) emu.nextElement();
            //read directory as file first,so only need to create directory
            if (entry.isDirectory()) {
                new File(filePath + entry.getName()).mkdirs();
                continue;
            }
            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
            File file = new File(filePath + entry.getName());
            //Because that is random to read zipfile,maybe the file is read first
            //before the directory is read,so we need to create directory first.
            File parent = file.getParentFile();
            if (parent != null && (!parent.exists())) {
                parent.mkdirs();
            }
            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

            int count;
            byte data[] = new byte[BUFFER];
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                bos.write(data, 0, count);
            }
            bos.flush();
            bos.close();
            bis.close();

            if (entry.getName().endsWith(".zip")) {
                File subFile = new File(filePath + entry.getName());
                if (subFile.exists()) {
                    int subStatus = unzipCSAR(filePath + entry.getName(), subFile.getParent() + "/");
                    if (subStatus != 0) {
                        LOG.error("sub file unzip fail!" + subFile.getName());
                        status = Constant.UNZIP_FAIL;
                        return status;
                    }
                }
            }
        }
        status = Constant.UNZIP_SUCCESS;
        zipFile.close();
    } catch (Exception e) {
        status = Constant.UNZIP_FAIL;
        e.printStackTrace();
    }
    return status;
}

From source file:org.opendatakit.api.forms.FormService.java

static Map<String, byte[]> processZipInputStream(ZipInputStream zipInputStream) throws IOException {
    int c;/*from   w  w w .  j a  v a2s . c  o  m*/
    Map<String, byte[]> files = new HashMap<>();
    byte buffer[] = new byte[2084];
    ByteArrayOutputStream tempBAOS;
    ZipEntry zipEntry;
    while ((zipEntry = zipInputStream.getNextEntry()) != null) {
        if (!(zipEntry.isDirectory())) {
            tempBAOS = new ByteArrayOutputStream();
            while ((c = zipInputStream.read(buffer, 0, 2048)) > -1) {
                tempBAOS.write(buffer, 0, c);
            }
            files.put("tables" + BasicConsts.FORWARDSLASH + zipEntry.getName(), tempBAOS.toByteArray());
        }
    }
    return files;
}

From source file:com.thruzero.common.core.utils.FileUtilsExt.java

public static final boolean unzipArchive(final File fromFile, final File toDir) throws IOException {
    boolean result = false; // assumes error

    logHelper.logBeginUnzip(fromFile, toDir);

    ZipFile zipFile = new ZipFile(fromFile);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    if (!toDir.exists()) {
        toDir.mkdirs();//from   w w  w . j av  a  2 s . c o m
        logHelper.logProgressCreatedToDir(toDir);
    }
    logHelper.logProgressToDirIsWritable(toDir);

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

            if (entry.isDirectory()) {
                File dir = new File(
                        toDir.getAbsolutePath() + EnvironmentHelper.FILE_PATH_SEPARATOR + entry.getName());

                if (!dir.exists()) {
                    dir.mkdirs();
                    logHelper.logProgressFilePathCreated(dir);
                }
            } else {
                File fosz = new File(
                        toDir.getAbsolutePath() + EnvironmentHelper.FILE_PATH_SEPARATOR + entry.getName());
                logHelper.logProgressCopyEntry(fosz);

                File parent = fosz.getParentFile();
                if (!parent.exists()) {
                    parent.mkdirs();
                }

                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fosz));
                IOUtils.copy(zipFile.getInputStream(entry), bos);
                bos.flush();
            }
        }
        zipFile.close();
        result = true; // success
    } else {
        logHelper.logFileWriteError(fromFile, null);
        zipFile.close();
    }

    return result;
}

From source file:be.i8c.sag.util.FileUtils.java

/**
 * Extract files from a zipped (and jar) file
 *
 * @param internalDir The directory you want to copy
 * @param zipFile The file that contains the content you want to copy
 * @param to The directory you want to copy to
 * @param deleteOnExit If true, delete the files once the application has closed.
 * @throws IOException When failed to write to a file
 * @throws FileNotFoundException If a file could not be found
 *///from w  ww  .j  ava  2 s  .  com
public static void extractDirectory(String internalDir, File zipFile, File to, boolean deleteOnExit)
        throws IOException {
    try (ZipInputStream zip = new ZipInputStream(new FileInputStream(zipFile))) {
        while (true) {
            ZipEntry zipEntry = zip.getNextEntry();
            if (zipEntry == null)
                break;

            if (zipEntry.getName().startsWith(internalDir) && !zipEntry.isDirectory()) {
                File f = createFile(new File(to, zipEntry.getName()
                        .replace((internalDir.endsWith("/") ? internalDir : internalDir + "/"), "")));
                if (deleteOnExit)
                    f.deleteOnExit();
                OutputStream bos = new FileOutputStream(f);
                try {
                    IOUtils.copy(zip, bos);
                } finally {
                    bos.flush();
                    bos.close();
                }
            }

            zip.closeEntry();
        }
    }
}

From source file:org.calrissian.restdoclet.writer.swagger.SwaggerWriter.java

private static void copySwagger() throws IOException {
    ZipInputStream swaggerZip = null;
    FileOutputStream out = null;//from   www .j  a  v a2  s.  co m
    try {
        swaggerZip = new ZipInputStream(
                Thread.currentThread().getContextClassLoader().getResourceAsStream(SWAGGER_UI_ARTIFACT));
        ZipEntry entry;
        while ((entry = swaggerZip.getNextEntry()) != null) {
            final File swaggerFile = new File(".", entry.getName());
            if (entry.isDirectory()) {
                if (!swaggerFile.isDirectory() && !swaggerFile.mkdirs()) {
                    throw new RuntimeException("Unable to create directory: " + swaggerFile);
                }
            } else {
                copy(swaggerZip, new FileOutputStream(swaggerFile));
            }
        }
    } finally {
        close(swaggerZip, out);
    }
}

From source file:com.seleniumtests.util.FileUtility.java

/**
 * Unzip file to a temp directory//from   w ww  . j  av a2  s .  c om
 * @param zippedFile
 * @return the output folder
 * @throws IOException
 */
public static File unzipFile(final File zippedFile) throws IOException {
    File outputFolder = Files.createTempDirectory("tmp").toFile();
    try (ZipFile zipFile = new ZipFile(zippedFile)) {
        final Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            final ZipEntry entry = entries.nextElement();
            final File entryDestination = new File(outputFolder, entry.getName());
            if (entry.isDirectory()) {
                //noinspection ResultOfMethodCallIgnored
                entryDestination.mkdirs();
            } else {
                //noinspection ResultOfMethodCallIgnored
                entryDestination.getParentFile().mkdirs();
                final InputStream in = zipFile.getInputStream(entry);
                final OutputStream out = new FileOutputStream(entryDestination);
                IOUtils.copy(in, out);
                IOUtils.closeQuietly(in);
                out.close();
            }
        }
    }
    return outputFolder;
}