Example usage for java.util.zip ZipFile ZipFile

List of usage examples for java.util.zip ZipFile ZipFile

Introduction

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

Prototype

public ZipFile(File file) throws ZipException, IOException 

Source Link

Document

Opens a ZIP file for reading given the specified File object.

Usage

From source file:com.twosigma.beakerx.kernel.magic.command.ClasspathAddMvnDepsCellMagicCommandTest.java

private static void unzipRepo() {
    try {/*from  w w  w .  jav a2 s  .  c o m*/
        ZipFile zipFile = new ZipFile(BUILD_PATH + "/testMvnCache.zip");
        Enumeration<?> enu = zipFile.entries();
        while (enu.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) enu.nextElement();
            String name = BUILD_PATH + "/" + zipEntry.getName();
            File file = new File(name);
            if (name.endsWith("/")) {
                file.mkdirs();
                continue;
            }

            File parent = file.getParentFile();
            if (parent != null) {
                parent.mkdirs();
            }

            InputStream is = zipFile.getInputStream(zipEntry);
            FileOutputStream fos = new FileOutputStream(file);
            byte[] bytes = new byte[1024];
            int length;
            while ((length = is.read(bytes)) >= 0) {
                fos.write(bytes, 0, length);
            }
            is.close();
            fos.close();

        }
        zipFile.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:io.wcm.devops.conga.tooling.maven.plugin.GenerateMojo.java

/**
 * Checks if the JAR file of the given dependency has a CONGA-INF/ directory.
 * @param dependency Dependency/*from w w w.  ja va2  s.  com*/
 * @return true if configuration definitions found
 */
private boolean hasCongaDefinitions(Dependency dependency) {
    if (!StringUtils.equals(dependency.getType(), "jar")) {
        return false;
    }
    String fileInfo = dependency.toString();
    try {
        Artifact artifact = getArtifact(dependency);
        fileInfo = FileUtil.getCanonicalPath(artifact.getFile());
        try (ZipFile zipFile = new ZipFile(artifact.getFile())) {
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                if (StringUtils.startsWith(entry.getName(), BuildConstants.CLASSPATH_PREFIX)) {
                    return true;
                }
            }
        }
    } catch (IOException ex) {
        throw new GeneratorException("Unable to read from JAR file: " + fileInfo, ex);
    }
    return false;
}

From source file:io.github.bonigarcia.wdm.Downloader.java

public static final File extract(File compressedFile, String export) throws IOException {
    log.trace("Compressed file {}", compressedFile);

    File file = null;/*from w w  w.j  ava2  s  . c om*/
    if (compressedFile.getName().toLowerCase().endsWith("tar.bz2")) {
        file = unBZip2(compressedFile);
    } else if (compressedFile.getName().toLowerCase().endsWith("tar.gz")) {
        file = unTarGz(compressedFile);
    } else if (compressedFile.getName().toLowerCase().endsWith("gz")) {
        file = unGzip(compressedFile);
    } else {
        ZipFile zipFolder = new ZipFile(compressedFile);
        Enumeration<?> enu = zipFolder.entries();

        while (enu.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) enu.nextElement();

            String name = zipEntry.getName();
            long size = zipEntry.getSize();
            long compressedSize = zipEntry.getCompressedSize();
            log.trace("Unzipping {} (size: {} KB, compressed size: {} KB)", name, size, compressedSize);

            file = new File(compressedFile.getParentFile() + File.separator + name);
            if (!file.exists() || WdmConfig.getBoolean("wdm.override")) {
                if (name.endsWith("/")) {
                    file.mkdirs();
                    continue;
                }

                File parent = file.getParentFile();
                if (parent != null) {
                    parent.mkdirs();
                }

                InputStream is = zipFolder.getInputStream(zipEntry);
                FileOutputStream fos = new FileOutputStream(file);
                byte[] bytes = new byte[1024];
                int length;
                while ((length = is.read(bytes)) >= 0) {
                    fos.write(bytes, 0, length);
                }
                is.close();
                fos.close();
                file.setExecutable(true);
            } else {
                log.debug(file + " already exists");
            }

        }
        zipFolder.close();
    }

    file = checkPhantom(compressedFile, export);

    log.trace("Resulting binary file {}", file.getAbsoluteFile());
    return file.getAbsoluteFile();
}

From source file:edu.umd.cs.buildServer.ProjectSubmission.java

public Set<String> getFilesInSubmission() throws IOException {
    HashSet<String> result = new HashSet<String>();
    ZipFile z = null;// w w  w.  jav a2 s .co m
    try {
        z = new ZipFile(getZipFile());
        Enumeration<? extends ZipEntry> e = z.entries();
        while (e.hasMoreElements()) {
            ZipEntry entry = e.nextElement();
            result.add(entry.getName());
        }
    } finally {
        IO.closeSilently(z);
    }
    return result;
}

From source file:gov.nih.nci.caarray.application.fileaccess.FileAccessUtils.java

/**
 * Unzips a .zip file, removes it from <code>files</code> and adds the extracted files to <code>files</code>.
 * //from   w  w  w.  j  av a  2  s . c  om
 * @param files the list of files to unzip and the files extracted from the zips
 * @param fileNames the list of filenames to go along with the list of files
 */
@SuppressWarnings("PMD.ExcessiveMethodLength")
public void unzipFiles(List<File> files, List<String> fileNames) {
    try {
        final Pattern p = Pattern.compile("\\.zip$");
        int index = 0;
        for (int i = 0; i < fileNames.size(); i++) {
            final Matcher m = p.matcher(fileNames.get(i).toLowerCase());

            if (m.find()) {
                final File file = files.get(i);
                final String fileName = file.getAbsolutePath();
                final String directoryPath = file.getParent();
                final ZipFile zipFile = new ZipFile(fileName);
                final Enumeration<? extends ZipEntry> entries = zipFile.entries();
                while (entries.hasMoreElements()) {
                    final ZipEntry entry = entries.nextElement();
                    final File entryFile = new File(directoryPath + "/" + entry.getName());

                    final InputStream fileInputStream = zipFile.getInputStream(entry);
                    final FileOutputStream fileOutputStream = new FileOutputStream(entryFile);
                    IOUtils.copy(fileInputStream, fileOutputStream);
                    IOUtils.closeQuietly(fileOutputStream);

                    files.add(entryFile);
                    fileNames.add(entry.getName());
                }
                zipFile.close();
                files.remove(index);
                fileNames.remove(index);
            }
            index++;
        }
    } catch (final IOException e) {
        throw new FileAccessException("Couldn't unzip archive.", e);
    }
}

From source file:com.machinepublishers.jbrowserdriver.JBrowserDriver.java

private static void initClasspath() {
    List<String> classpathSimpleTmp = new ArrayList<String>();
    List<String> classpathUnpackedTmp = new ArrayList<String>();
    try {//from w  ww.j  a va  2 s.  co m
        List<File> classpathElements = new FastClasspathScanner().getUniqueClasspathElements();
        final File classpathDir = Files.createTempDirectory("jbd_classpath_").toFile();
        Runtime.getRuntime().addShutdownHook(new FileRemover(classpathDir));
        List<String> pathsSimple = new ArrayList<String>();
        List<String> pathsUnpacked = new ArrayList<String>();
        for (File curElement : classpathElements) {
            String rootLevelElement = curElement.getAbsoluteFile().toURI().toURL().toExternalForm();
            pathsSimple.add(rootLevelElement);
            pathsUnpacked.add(rootLevelElement);
            if (curElement.isFile() && curElement.getPath().endsWith(".jar")) {
                try (ZipFile jar = new ZipFile(curElement)) {
                    Enumeration<? extends ZipEntry> entries = jar.entries();
                    while (entries.hasMoreElements()) {
                        ZipEntry entry = entries.nextElement();
                        if (entry.getName().endsWith(".jar")) {
                            try (InputStream in = jar.getInputStream(entry)) {
                                File childJar = new File(classpathDir, Util.randomFileName() + ".jar");
                                Files.copy(in, childJar.toPath());
                                pathsUnpacked.add(childJar.getAbsoluteFile().toURI().toURL().toExternalForm());
                                childJar.deleteOnExit();
                            }
                        }
                    }
                }
            }
        }
        classpathSimpleTmp = createClasspathJar(classpathDir, "classpath-simple.jar", pathsSimple);
        classpathUnpackedTmp = createClasspathJar(classpathDir, "classpath-unpacked.jar", pathsUnpacked);
    } catch (Throwable t) {
        Util.handleException(t);
    }
    classpathSimpleArgs = Collections.unmodifiableList(classpathSimpleTmp);
    classpathUnpackedArgs = Collections.unmodifiableList(classpathUnpackedTmp);
}

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

/**
 * Unzip file to a temp directory/*from w  ww .j a v a2s .c o m*/
 * @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;
}

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  ww .  j  a v  a 2s .c  om
                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:ZipURLStreamHandler.java

/**
 * Attempts to open the zip entry./*from   ww  w .java  2 s. c o m*/
 */

public void connect() throws IOException {
    this.zipFile = new ZipFile(file);
    this.zipEntry = zipFile.getEntry(zipEntryName);
    if (zipEntry == null)
        throw new IOException("Entry " + zipEntryName + " not found in file " + file);
    this.connected = true;
}