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.rptools.lib.FileUtil.java

public static void unzip(ZipInputStream in, File destDir) throws IOException {
    if (in == null)
        throw new IOException("input stream cannot be null");

    // Prepare destination
    destDir.mkdirs();// w w w.j  a va 2s .com
    File absDestDir = destDir.getAbsoluteFile();

    // Pull out the files
    OutputStream out = null;
    ZipEntry entry = null;
    try {
        while ((entry = in.getNextEntry()) != null) {
            if (entry.isDirectory())
                continue;

            // Prepare file destination
            File entryFile = new File(absDestDir, entry.getName());
            entryFile.getParentFile().mkdirs();

            out = new FileOutputStream(entryFile);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(out);
            in.closeEntry();
        }
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:com.gisgraphy.importer.ImporterHelper.java

/**
 * unzip a file in the same directory as the zipped file
 * /*from w  ww.  ja v a 2s. c  o  m*/
 * @param file
 *            The file to unzip
 */
public static void unzipFile(File file) {
    logger.info("will Extracting file: " + file.getName());
    Enumeration<? extends ZipEntry> entries;
    ZipFile zipFile;

    try {
        zipFile = new ZipFile(file);

        entries = zipFile.entries();

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

            if (entry.isDirectory()) {
                // Assume directories are stored parents first then
                // children.
                (new File(entry.getName())).mkdir();
                continue;
            }

            logger.info("Extracting file: " + entry.getName() + " to " + file.getParent() + File.separator
                    + entry.getName());
            copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(
                    new FileOutputStream(file.getParent() + File.separator + entry.getName())));
        }

        zipFile.close();
    } catch (IOException e) {
        logger.error("can not unzip " + file.getName() + " : " + e.getMessage(), e);
        throw new ImporterException(e);
    }
}

From source file:de.spiritcroc.ownlog.FileHelper.java

public static ImportFiles unpackImport(Context context, InputStream inputStream) {
    File importingDir = new File(context.getCacheDir(), context.getString(R.string.import_file_dir));
    rmdir(importingDir);/* w  w w .j  a  v a  2 s .  c o  m*/
    importingDir.mkdirs();
    ZipInputStream in = new ZipInputStream(inputStream);
    ZipEntry entry;
    try {
        while ((entry = in.getNextEntry()) != null) {
            String path = importingDir.getAbsolutePath() + File.separator + entry.getName();
            if (DEBUG)
                Log.d(TAG, "Unzipping path: " + path);
            File f = new File(path);
            if (entry.isDirectory()) {
                if (!f.isDirectory()) {
                    f.mkdirs();
                }
            } else {
                f.getParentFile().mkdirs();
                f.createNewFile();
                FileOutputStream out = new FileOutputStream(path, false);
                try {
                    int size;
                    while ((size = in.read()) != -1) {
                        out.write(size);
                    }
                    in.closeEntry();
                } finally {
                    try {
                        out.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    } catch (Exception e) {
        Log.w(TAG, "Import failed", e);
        return null;
    }
    File logDbFile = new File(importingDir, context.getString(R.string.import_file_db));
    File attachmentsDir = new File(importingDir, context.getString(R.string.import_file_attachments));
    if (logDbFile.exists()) {
        return new ImportFiles(logDbFile, attachmentsDir);
    } else {
        Log.w(TAG, "Import failed: database not found");
        return null;
    }
}

From source file:eu.freme.eservices.epublishing.Unzipper.java

public static void unzip(ZipInputStream zis, File outputFolder) throws IOException {
    //create output directory is not exists

    if (!outputFolder.exists() && !outputFolder.mkdirs()) {
        throw new IOException("Cannot create directory " + outputFolder);
    }/*  w  w  w. j  av a 2 s.  co m*/

    //get the zipped file list entry
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {
        String fileName = ze.getName();
        File newFile = new File(outputFolder, fileName);

        logger.debug("file unzip : " + newFile.getAbsoluteFile());

        //create all non exists folders
        //else you will hit FileNotFoundException for compressed folder
        File parentDir = newFile.getParentFile();
        if (!parentDir.exists() && !parentDir.mkdirs()) {
            throw new IOException("Cannot create directory " + newFile.getParent());
        }

        if (ze.isDirectory()) {
            newFile.mkdirs();
        } else {
            try (FileOutputStream fos = new FileOutputStream(newFile)) {
                IOUtils.copyLarge(zis, fos);
            }
        }
        ze = zis.getNextEntry();
    }
    zis.closeEntry();
}

From source file:com.microsoft.intellij.helpers.IDEHelperImpl.java

private static void copyJarFiles(@NotNull final Module module, @NotNull VirtualFile baseDir,
        @NotNull File zipFile, @NotNull String zipPath) throws IOException {
    if (baseDir.isDirectory()) {
        final ZipFile zip = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> entries = zip.entries();

        while (entries.hasMoreElements()) {
            final ZipEntry zipEntry = entries.nextElement();

            if (!zipEntry.isDirectory() && zipEntry.getName().startsWith(zipPath)
                    && zipEntry.getName().endsWith(".jar") && !(zipEntry.getName().endsWith("-sources.jar")
                            || zipEntry.getName().endsWith("-javadoc.jar"))) {
                VirtualFile libsVf = null;

                for (VirtualFile vf : baseDir.getChildren()) {
                    if (vf.getName().equals("libs")) {
                        libsVf = vf;//from w  w w. ja  va2  s  .c  o m
                        break;
                    }
                }

                if (libsVf == null) {
                    libsVf = baseDir.createChildDirectory(module.getProject(), "libs");
                }

                final VirtualFile libs = libsVf;
                final String fileName = zipEntry.getName().split("/")[1];

                if (libs.findChild(fileName) == null) {
                    ApplicationManager.getApplication().invokeAndWait(new Runnable() {
                        @Override
                        public void run() {
                            ApplicationManager.getApplication().runWriteAction(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        InputStream mobileserviceInputStream = zip.getInputStream(zipEntry);
                                        VirtualFile msVF = libs.createChildData(module.getProject(), fileName);
                                        msVF.setBinaryContent(getArray(mobileserviceInputStream));
                                    } catch (Throwable ex) {
                                        DefaultLoader.getUIHelper().showException(
                                                "An error occurred while attempting "
                                                        + "to configure Azure Mobile Services.",
                                                ex,
                                                "Azure Services Explorer - Error Configuring Mobile Services",
                                                false, true);
                                    }
                                }
                            });
                        }
                    }, ModalityState.defaultModalityState());
                }
            }
        }
    }
}

From source file:lucee.commons.io.compress.CompressUtil.java

private static void listZip(Resource zipFile) throws IOException {
    if (!zipFile.exists())
        throw new IOException(zipFile + " is not a existing file");

    if (zipFile.isDirectory()) {
        throw new IOException(zipFile + " is a directory");
    }/*from  www. j a va 2 s  .  c  o  m*/

    ZipInputStream zis = null;
    try {
        zis = new ZipInputStream(IOUtil.toBufferedInputStream(zipFile.getInputStream()));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                IOUtil.copy(zis, baos, false, false);
                byte[] barr = baos.toByteArray();
                aprint.o(entry.getName() + ":" + barr.length);
            }
        }
    } finally {
        IOUtil.closeEL(zis);
    }
}

From source file:net.amigocraft.mpt.util.MiscUtil.java

public static boolean unzip(ZipFile zip, File dest, List<String> files) throws MPTException {
    boolean returnValue = true;
    try {// w ww  .  j a  v a2  s  . c o  m
        List<String> existingDirs = new ArrayList<>();
        Enumeration<? extends ZipEntry> en = zip.entries();
        entryLoop: while (en.hasMoreElements()) {
            ZipEntry entry = en.nextElement();
            String name = entry.getName().startsWith("./")
                    ? entry.getName().substring(2, entry.getName().length())
                    : entry.getName();
            File file = new File(dest, name);
            if (entry.isDirectory()) {
                if (file.exists()) {
                    if (DISALLOW_MERGE) {
                        existingDirs.add(name);
                        if (VERBOSE)
                            Main.log.warning("Refusing to extract directory " + name + ": already exists");
                    }
                }
            } else {
                files.add(name);
                for (String dir : DISALLOWED_DIRECTORIES) {
                    if (file.getPath().startsWith(dir)) {
                        if (VERBOSE)
                            Main.log.warning("Refusing to extract " + name + " from " + zip.getName()
                                    + ": parent directory \"" + dir + "\" is not allowed");
                        continue entryLoop;
                    }
                }
                if (DISALLOW_MERGE) {
                    for (String dir : existingDirs) {
                        if (file.getPath().substring(2, file.getPath().length()).replace(File.separator, "/")
                                .startsWith(dir)) {
                            continue entryLoop;
                        }
                    }
                }
                if (!DISALLOW_OVERWRITE || !file.exists()) {
                    file.getParentFile().mkdirs();
                    for (String ext : DISALLOWED_EXTENSIONS) {
                        if (file.getName().endsWith(ext)) {
                            if (VERBOSE)
                                Main.log.warning("Refusing to extract " + name + " from " + zip.getName()
                                        + ": extension \"" + ext + "\" is not allowed");
                            returnValue = false;
                            continue entryLoop;
                        }
                    }
                    BufferedInputStream bIs = new BufferedInputStream(zip.getInputStream(entry));
                    int b;
                    byte[] buffer = new byte[1024];
                    FileOutputStream fOs = new FileOutputStream(file);
                    BufferedOutputStream bOs = new BufferedOutputStream(fOs, 1024);
                    while ((b = bIs.read(buffer, 0, 1024)) != -1)
                        bOs.write(buffer, 0, b);
                    bOs.flush();
                    bOs.close();
                    bIs.close();
                } else {
                    if (VERBOSE)
                        Main.log.warning(
                                "Refusing to extract " + name + " from " + zip.getName() + ": already exists");
                    returnValue = false;
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace(); //TODO
        throw new MPTException(ERROR_COLOR + "Failed to extract archive!");
    }
    return returnValue;
}

From source file:edu.harvard.i2b2.eclipse.plugins.metadataLoader.util.FileUtil.java

public static void unZipAll(File source, File destination) throws IOException {
    System.out.println("Unzipping - " + source.getName());
    int BUFFER = 2048;

    ZipFile zip = new ZipFile(source);
    try {/* www  . j a  va  2 s  .c  om*/
        destination.getParentFile().mkdirs();
        Enumeration zipFileEntries = zip.entries();

        // Process each entry
        while (zipFileEntries.hasMoreElements()) {
            // grab a zip file entry
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();
            File destFile = new File(destination, currentEntry);
            //destFile = new File(newPath, destFile.getName());
            File destinationParent = destFile.getParentFile();

            // create the parent directory structure if needed
            destinationParent.mkdirs();

            if (!entry.isDirectory()) {
                BufferedInputStream is = null;
                FileOutputStream fos = null;
                BufferedOutputStream dest = null;
                try {
                    is = new BufferedInputStream(zip.getInputStream(entry));
                    int currentByte;
                    // establish buffer for writing file
                    byte data[] = new byte[BUFFER];

                    // write the current file to disk
                    fos = new FileOutputStream(destFile);
                    dest = new BufferedOutputStream(fos, BUFFER);

                    // read and write until last byte is encountered
                    while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                        dest.write(data, 0, currentByte);
                    }
                } catch (Exception e) {
                    System.out.println("unable to extract entry:" + entry.getName());
                    throw e;
                } finally {
                    if (dest != null) {
                        dest.close();
                    }
                    if (fos != null) {
                        fos.close();
                    }
                    if (is != null) {
                        is.close();
                    }
                }
            } else {
                //Create directory
                destFile.mkdirs();
            }

            if (currentEntry.endsWith(".zip")) {
                // found a zip file, try to extract
                unZipAll(destFile, destinationParent);
                if (!destFile.delete()) {
                    System.out.println("Could not delete zip");
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Failed to successfully unzip:" + source.getName());
    } finally {
        zip.close();
    }
    System.out.println("Done Unzipping:" + source.getName());
}

From source file:com.jsonstore.util.JSONStoreUtil.java

public static void unpack(InputStream in, File targetDir) throws IOException {
    ZipInputStream zin = new ZipInputStream(in);

    ZipEntry entry;
    while ((entry = zin.getNextEntry()) != null) {
        String extractFilePath = entry.getName();
        if (extractFilePath.startsWith("/") || extractFilePath.startsWith("\\")) {
            extractFilePath = extractFilePath.substring(1);
        }//from w  w w.j a v  a 2  s .  c o  m
        File extractFile = new File(targetDir.getPath() + File.separator + extractFilePath);
        if (entry.isDirectory()) {
            if (!extractFile.exists()) {
                extractFile.mkdirs();
            }
            continue;
        } else {
            File parent = extractFile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
        }

        // if not directory instead of the previous check and continue
        OutputStream os = new BufferedOutputStream(new FileOutputStream(extractFile));
        copyFile(zin, os);
        os.flush();
        os.close();
    }
}

From source file:Main.java

public static void unzip(URL _url, URL _dest, boolean _remove_top) {
    int BUFFER_SZ = 2048;
    File file = new File(_url.getPath());

    String dest = _dest.getPath();
    new File(dest).mkdir();

    try {/*from ww  w . ja v a  2  s  . c o  m*/
        ZipFile zip = new ZipFile(file);
        Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();

        // Process each entry
        while (zipFileEntries.hasMoreElements()) {
            // grab a zip file entry
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();

            if (_remove_top)
                currentEntry = currentEntry.substring(currentEntry.indexOf('/'), currentEntry.length());

            File destFile = new File(dest, currentEntry);
            //destFile = new File(newPath, destFile.getName());
            File destinationParent = destFile.getParentFile();

            // create the parent directory structure if needed
            destinationParent.mkdirs();

            if (!entry.isDirectory()) {
                BufferedInputStream is;
                is = new BufferedInputStream(zip.getInputStream(entry));
                int currentByte;
                // establish buffer for writing file
                byte data[] = new byte[BUFFER_SZ];

                // write the current file to disk
                FileOutputStream fos = new FileOutputStream(destFile);
                BufferedOutputStream dst = new BufferedOutputStream(fos, BUFFER_SZ);

                // read and write until last byte is encountered
                while ((currentByte = is.read(data, 0, BUFFER_SZ)) != -1) {
                    dst.write(data, 0, currentByte);
                }
                dst.flush();
                dst.close();
                is.close();
            }
            /*
            if (currentEntry.endsWith(".zip"))
            {
            // found a zip file, try to open
            extractFolder(destFile.getAbsolutePath());
            }*/
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}