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:ZipExploder.java

/**
 * copy a single entry from the archive//  w  ww .j ava 2  s .c  o  m
 * 
 * @param destDir
 * @param zf
 * @param ze
 * @throws IOException
 */
public void copyFileEntry(String destDir, ZipFile zf, ZipEntry ze) throws IOException {
    DataInputStream dis = new DataInputStream(new BufferedInputStream(zf.getInputStream(ze)));
    try {
        copyFileEntry(destDir, ze.isDirectory(), ze.getName(), dis);
    } finally {
        try {
            dis.close();
        } catch (IOException ioe) {
        }
    }
}

From source file:net.sourceforge.vulcan.core.support.AbstractFileStore.java

@Override
public final PluginMetaDataDto extractPlugin(InputStream is) throws StoreException {
    final File pluginsDir = getPluginsRoot();
    String toplevel = null;/* ww w  .j a v a 2  s  .  co m*/
    File tmpDir = null;

    if (!pluginsDir.exists()) {
        createDir(pluginsDir);
    }

    final ZipInputStream zis = new ZipInputStream(is);
    ZipEntry entry;

    try {
        entry = zis.getNextEntry();
        if (entry == null || !entry.isDirectory()) {
            throw new InvalidPluginLayoutException();
        }
        toplevel = entry.getName();
        tmpDir = new File(pluginsDir, "tmp-" + toplevel);
        if (!tmpDir.exists()) {
            createDir(tmpDir);
        }

        final String id = toplevel.replaceAll("/", "");

        while ((entry = zis.getNextEntry()) != null) {
            if (!entry.getName().startsWith(toplevel)) {
                throw new InvalidPluginLayoutException();
            }

            final File out = new File(pluginsDir, "tmp-" + entry.getName());

            if (entry.isDirectory()) {
                createDir(out);
                zis.closeEntry();
            } else {
                final FileOutputStream os = new FileOutputStream(out);

                try {
                    IOUtils.copy(zis, os);
                } finally {
                    os.close();
                    zis.closeEntry();
                }
            }
        }

        checkPluginVersion(pluginsDir, id);

        return createPluginConfig(new File(pluginsDir, toplevel));
    } catch (DuplicatePluginIdException e) {
        throw e;
    } catch (Exception e) {
        if (e instanceof StoreException) {
            throw (StoreException) e;
        }
        throw new StoreException(e.getMessage(), e);
    } finally {
        try {
            zis.close();
        } catch (IOException ignore) {
        }
        if (tmpDir != null && tmpDir.exists()) {
            try {
                FileUtils.deleteDirectory(tmpDir);
            } catch (Exception ignore) {
            }
        }
    }
}

From source file:gdt.data.entity.ArchiveHandler.java

/**
 * Extract zip archive stream into the target directory
 * @param targetDirectory$ the path of the target directory
 * @param zis the zip archive input stream.
 *//* www . j  a  va  2 s .c o  m*/
public static void extractEntitiesFromZip(String targetDirectory$, ZipInputStream zis) {
    try {
        ZipEntry entry = null;
        File outputFile;
        File outputDir;
        FileOutputStream outputStream;
        while ((entry = zis.getNextEntry()) != null) {
            outputFile = new File(targetDirectory$ + "/" + entry.getName());
            outputDir = outputFile.getParentFile();
            if (!outputDir.exists())
                outputDir.mkdirs();
            if (entry.isDirectory()) {
                outputFile.mkdir();
            } else {
                outputStream = new FileOutputStream(outputFile);
                IOUtils.copy(zis, outputStream);
                outputStream.close();
            }
        }
        zis.close();
    } catch (Exception e) {
        Logger.getLogger(ArchiveHandler.class.getName()).severe(e.toString());
    }
}

From source file:nl.imvertor.common.file.ZipFile.java

/**
 * Unzip it//from w w w.  j  a va2  s . co m
 * @param zipFile input zip file
 * @param outputFolder zip file output folder
 * @param requestedFilePattern Pattern to match the file name.
 * 
 * @throws Exception 
 */
private void unZipIt(String zipFile, String outputFolder, Pattern requestedFilePattern) throws Exception {

    byte[] buffer = new byte[1024];

    //create output folder is not exists
    AnyFolder folder = new AnyFolder(outputFolder);
    folder.mkdir();

    //get the zip file content
    ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
    //get the zipped file list entry
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {
        if (!ze.isDirectory()) {
            String fileName = ze.getName();
            // if the pattern specified, use a matcher. Otherwise accept any file.
            Matcher m = (requestedFilePattern != null) ? requestedFilePattern.matcher(fileName) : null;
            if (requestedFilePattern == null || m.find()) {
                File newFile = new File(outputFolder + File.separator + fileName);
                //create all non exists folders
                //else you will hit FileNotFoundException for compressed folder
                new File(newFile.getParent()).mkdirs();
                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:com.l2jfree.gameserver.script.ScriptPackage.java

/**
 * @param scriptFiles The scriptFiles to set.
 *//*from  ww  w.java2  s  .  co  m*/
private void addFiles(ZipFile pack) {
    for (Enumeration<? extends ZipEntry> e = pack.entries(); e.hasMoreElements();) {
        ZipEntry entry = e.nextElement();
        if (entry.getName().endsWith(".xml")) {
            try {
                ScriptDocument newScript = new ScriptDocument(entry.getName(), pack.getInputStream(entry));
                _scriptFiles.add(newScript);
            } catch (IOException e1) {
                _log.error(e1.getMessage(), e1);
            }
        } else if (!entry.isDirectory()) {
            // ignore it
        }
    }
}

From source file:com.github.nethad.clustermeister.integration.JPPFTestNode.java

private void unzipNode(InputStream fileToUnzip, File targetDir) {
    //        Enumeration entries;
    ZipInputStream zipFile;/*  w  w  w .j  a  v  a2  s  .  c om*/
    try {
        zipFile = new ZipInputStream(fileToUnzip);
        ZipEntry entry;
        while ((entry = zipFile.getNextEntry()) != null) {
            //                ZipEntry entry = (ZipEntry) entries.nextElement();
            if (entry.isDirectory()) {
                // Assume directories are stored parents first then children.
                System.err.println("Extracting directory: " + entry.getName());
                // This is not robust, just for demonstration purposes.
                (new File(targetDir, entry.getName())).mkdir();
                continue;
            }
            System.err.println("Extracting file: " + entry.getName());
            File targetFile = new File(targetDir, entry.getName());
            copyInputStream_notClosing(zipFile, new BufferedOutputStream(new FileOutputStream(targetFile)));
            //                zipFile.closeEntry();
        }
        zipFile.close();
    } catch (IOException ioe) {
        System.err.println("Unhandled exception:");
        ioe.printStackTrace();
    }
}

From source file:mobac.mapsources.loader.MapPackManager.java

/**
 * Calculate the md5sum on all files in the map pack file (except those in META-INF) and their filenames inclusive
 * path in the map pack file)./*from  w ww . ja  v  a  2 s . c  om*/
 * 
 * @param mapPackFile
 * @return
 * @throws IOException
 * @throws NoSuchAlgorithmException
 */
public String generateMappackMD5(File mapPackFile) throws IOException, NoSuchAlgorithmException {
    ZipFile zip = new ZipFile(mapPackFile);
    try {
        Enumeration<? extends ZipEntry> entries = zip.entries();
        MessageDigest md5Total = MessageDigest.getInstance("MD5");
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();

            if (entry.isDirectory())
                continue;
            // Do not hash files from META-INF
            String name = entry.getName();
            if (name.toUpperCase().startsWith("META-INF"))
                continue;
            md5.reset();
            InputStream in = zip.getInputStream(entry);
            byte[] data = Utilities.getInputBytes(in);
            in.close();
            // name = name.replaceAll("\\\\", "/");
            byte[] digest = md5.digest(data);
            log.trace("Hashsum " + Hex.encodeHexString(digest) + " includes \"" + name + "\"");
            md5Total.update(digest);
            md5Total.update(name.getBytes());
        }
        String md5sum = Hex.encodeHexString(md5Total.digest());
        log.trace("md5sum of " + mapPackFile.getName() + ": " + md5sum);
        return md5sum;
    } finally {
        zip.close();
    }
}

From source file:io.github.jeremgamer.preview.actions.Launch.java

private void extractNatives() throws IOException {
    for (String s : NATIVE_JARS) {
        File archive = new File(s);
        ZipFile zipFile = new ZipFile(archive, Charset.forName("Cp437"));
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File entryDestination = new File(VANILLA_NATIVE_DIR.replaceAll("<version>", "1.8.1"),
                    entry.getName());//  w  w w .j av a 2 s  . c  o  m
            entryDestination.getParentFile().mkdirs();
            if (entry.isDirectory())
                entryDestination.mkdirs();
            else {
                InputStream in = zipFile.getInputStream(entry);
                OutputStream out = new FileOutputStream(entryDestination);
                IOUtils.copy(in, out);
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
                in.close();
                out.close();
            }
        }
        zipFile.close();
    }
}

From source file:apim.restful.importexport.utils.APIImportUtil.java

/**
 * This method decompresses API the archive
 *
 * @param sourceFile  The archive containing the API
 * @param destination location of the archive to be extracted
 * @return Name of the extracted directory
 * @throws APIImportException If the decompressing fails
 *//*from w ww  .ja v a 2s .  co  m*/
public static String extractArchive(File sourceFile, String destination) throws APIImportException {

    BufferedInputStream inputStream = null;
    InputStream zipInputStream = null;
    FileOutputStream outputStream = null;
    ZipFile zip = null;
    String archiveName = null;

    try {
        zip = new ZipFile(sourceFile);
        Enumeration zipFileEntries = zip.entries();
        int index = 0;

        // Process each entry
        while (zipFileEntries.hasMoreElements()) {

            // grab a zip file entry
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();

            //This index variable is used to get the extracted folder name; that is root directory
            if (index == 0) {
                archiveName = currentEntry.substring(0,
                        currentEntry.indexOf(APIImportExportConstants.ARCHIVE_PATH_SEPARATOR));
                --index;
            }

            File destinationFile = new File(destination, currentEntry);
            File destinationParent = destinationFile.getParentFile();

            // create the parent directory structure
            if (destinationParent.mkdirs()) {
                log.info("Creation of folder is successful. Directory Name : " + destinationParent.getName());
            }

            if (!entry.isDirectory()) {
                zipInputStream = zip.getInputStream(entry);
                inputStream = new BufferedInputStream(zipInputStream);

                // write the current file to the destination
                outputStream = new FileOutputStream(destinationFile);
                IOUtils.copy(inputStream, outputStream);
            }
        }
        return archiveName;
    } catch (IOException e) {
        log.error("Failed to extract archive file ", e);
        throw new APIImportException("Failed to extract archive file. " + e.getMessage());
    } finally {
        IOUtils.closeQuietly(zipInputStream);
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:de.tarent.maven.plugins.pkg.packager.IzPackPackager.java

/**
 * Puts the embedded izpack jar from the (resource) classpath into the work
 * directory and unpacks it there.//from  w  ww . j a  v  a  2  s.c  o  m
 * 
 * @param l
 * @param izPackEmbeddedFile
 * @param izPackEmbeddedHomeDir
 * @throws MojoExecutionException
 */
private void unpackIzPack(Log l, File izPackEmbeddedFile, File izPackEmbeddedHomeDir)
        throws MojoExecutionException {
    l.info("storing embedded IzPack installation in " + izPackEmbeddedFile);
    Utils.storeInputStream(IzPackPackager.class.getResourceAsStream(IZPACK_EMBEDDED_JAR), izPackEmbeddedFile,
            "IOException while unpacking embedded IzPack installation.");

    l.info("unzipping embedded IzPack installation to" + izPackEmbeddedHomeDir);
    int count = 0;
    ZipFile zip = null;
    try {
        zip = new ZipFile(izPackEmbeddedFile);
        Enumeration<? extends ZipEntry> e = zip.entries();

        while (e.hasMoreElements()) {
            count++;
            ZipEntry entry = (ZipEntry) e.nextElement();
            File unpacked = new File(izPackEmbeddedHomeDir, entry.getName());
            if (entry.isDirectory()) {
                unpacked.mkdirs(); // TODO: Check success.
            } else {
                Utils.createFile(unpacked, "Unable to create ZIP file entry ");
                Utils.storeInputStream(zip.getInputStream(entry), unpacked,
                        "IOException while unpacking ZIP file entry.");
            }
        }
    } catch (IOException ioe) {
        throw new MojoExecutionException("IOException while unpacking embedded IzPack installation.", ioe);
    } finally {
        if (zip != null) {
            try {
                zip.close();
            } catch (IOException e) {
                l.info(String.format("Error at closing zipfile %s caused by %s", izPackEmbeddedFile.getName(),
                        e.getMessage()));
            }
        }
    }
    l.info("unpacked " + count + " entries");
}