Example usage for org.apache.commons.compress.archivers.zip ZipArchiveEntry getName

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveEntry getName

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.zip ZipArchiveEntry getName.

Prototype

public String getName() 

Source Link

Document

Get the name of the entry.

Usage

From source file:org.fabrician.maven.plugins.CompressUtils.java

public static boolean entryExistsInZip(File zipFile, String entryName) throws IOException {
    ZipFile zip = new ZipFile(zipFile);
    for (Enumeration<ZipArchiveEntry> zipEnum = zip.getEntries(); zipEnum.hasMoreElements();) {
        ZipArchiveEntry source = zipEnum.nextElement();
        if (source.getName().equals(entryName)) {
            return true;
        }// www  .java2 s .c  om
    }
    return false;
}

From source file:org.interreg.docexplore.util.ZipUtils.java

public static void unzip(File zipfile, File directory, float[] progress, float progressOffset,
        float progressAmount, boolean overwrite) throws IOException {
    org.apache.commons.compress.archivers.zip.ZipFile zfile = new org.apache.commons.compress.archivers.zip.ZipFile(
            zipfile);// ww  w  .ja  va 2 s  . co  m

    int nEntries = 0;
    Enumeration<ZipArchiveEntry> entries = zfile.getEntries();
    while (entries.hasMoreElements())
        if (!entries.nextElement().isDirectory())
            nEntries++;

    int cnt = 0;
    entries = zfile.getEntries();
    while (entries.hasMoreElements()) {
        ZipArchiveEntry entry = entries.nextElement();
        File file = new File(directory, entry.getName());
        if (entry.isDirectory())
            file.mkdirs();
        else {
            if (!file.exists() || overwrite) {
                file.getParentFile().mkdirs();
                InputStream in = zfile.getInputStream(entry);
                try {
                    copy(in, file);
                } finally {
                    in.close();
                }
            }

            cnt++;
            if (progress != null)
                progress[0] = progressOffset + cnt * progressAmount / nEntries;
        }
    }
    zfile.close();
}

From source file:org.jason.mapmaker.server.util.ZipUtil.java

/**
 * Decompress a ZIP file into the temp directory, then return alist of File objects for further
 * processing. User is responsible for cleaning up the files later
 *
 * @param zipFile File object representing the zipped file to be decompressed
 * @return a List<File> containing File objects for each file that was in the zip archive
 * @throws FileNotFoundException zip archive file was not found
 * @throws ArchiveException      something went wrong creating
 *                               the archive input stream
 * @throws IOException/*from  w  w w . j a v a2 s . c  o m*/
 */
public static List<File> decompress(File zipFile) throws FileNotFoundException, ArchiveException, IOException {

    List<File> archiveContents = new ArrayList<File>();

    // create the input stream for the file, then the input stream for the actual zip file
    final InputStream is = new FileInputStream(zipFile);
    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.ZIP, is);

    // cycle through the entries in the zip archive and write them to the system temp dir
    ZipArchiveEntry entry = (ZipArchiveEntry) ais.getNextEntry();
    while (entry != null) {
        File outputFile = new File(tmpFile, entry.getName()); // don't do this anonymously, need it for the list
        OutputStream os = new FileOutputStream(outputFile);

        IOUtils.copy(ais, os); // copy from the archiveinputstream to the output stream
        os.close(); // close the output stream

        archiveContents.add(outputFile);

        entry = (ZipArchiveEntry) ais.getNextEntry();
    }

    ais.close();
    is.close();

    return archiveContents;
}

From source file:org.jason.mapmaker.server.util.ZipUtil.java

/**
 * Decompress a zip file from a remote URL.
 *
 * @param url       remote URL to import the file from
 * @return          a List<File> containing File objects for each file that was in the zip archive
 * @throws FileNotFoundException    zip archive file was not found
 * @throws ArchiveException         something went wrong creating
 *                                  the archive input stream
 * @throws IOException/* ww  w . j a  v a2 s . c  o  m*/
 */
public static List<File> decompress(URL url) throws FileNotFoundException, ArchiveException, IOException {

    List<File> archiveContents = new ArrayList<File>();
    final InputStream is = new BufferedInputStream(url.openStream(), 1024);
    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.ZIP, is);

    // cycle through the entries in the zip archive and write them to the system temp dir
    ZipArchiveEntry entry = (ZipArchiveEntry) ais.getNextEntry();
    while (entry != null) {
        File outputFile = new File(tmpFile, entry.getName()); // don't do this anonymously, need it for the list
        OutputStream os = new FileOutputStream(outputFile);

        IOUtils.copy(ais, os); // copy from the archiveinputstream to the output stream
        os.close(); // close the output stream

        archiveContents.add(outputFile);

        entry = (ZipArchiveEntry) ais.getNextEntry();
    }

    ais.close();
    is.close();

    return archiveContents;
}

From source file:org.jboss.as.forge.util.Files.java

public static boolean extractAppServer(final String zipPath, final File target, final boolean overwrite)
        throws IOException {
    if (target.exists() && !overwrite) {
        throw new IllegalStateException(Messages.INSTANCE.getMessage("files.not.empty.directory"));
    }/*from www .ja  v a 2  s.c o  m*/
    // Create a temporary directory
    final File tmpDir = new File(getTempDirectory(), "jboss-as-" + zipPath.hashCode());
    if (tmpDir.exists()) {
        deleteRecursively(tmpDir);
    }
    try {
        final byte buff[] = new byte[1024];
        ZipFile file = null;
        try {
            file = new ZipFile(zipPath);
            final Enumeration<ZipArchiveEntry> entries = file.getEntries();
            while (entries.hasMoreElements()) {
                final ZipArchiveEntry entry = entries.nextElement();
                // Create the extraction target
                final File extractTarget = new File(tmpDir, entry.getName());
                if (entry.isDirectory()) {
                    extractTarget.mkdirs();
                } else {
                    final File parent = new File(extractTarget.getParent());
                    parent.mkdirs();
                    final BufferedInputStream in = new BufferedInputStream(file.getInputStream(entry));
                    try {
                        final BufferedOutputStream out = new BufferedOutputStream(
                                new FileOutputStream(extractTarget));
                        try {
                            int read;
                            while ((read = in.read(buff)) != -1) {
                                out.write(buff, 0, read);
                            }
                        } finally {
                            Streams.safeClose(out);
                        }
                    } finally {
                        Streams.safeClose(in);
                    }
                    // Set the file permissions
                    if (entry.getUnixMode() > 0) {
                        setPermissions(extractTarget, FilePermissions.of(entry.getUnixMode()));
                    }
                }
            }
        } catch (IOException e) {
            throw new IOException(Messages.INSTANCE.getMessage("files.extraction.error", file), e);
        } finally {
            ZipFile.closeQuietly(file);
            // Streams.safeClose(file);
        }
        // If the target exists, remove then rename
        if (target.exists()) {
            deleteRecursively(target);
        }
        // First child should be a directory and there should only be one child
        final File[] children = tmpDir.listFiles();
        if (children != null && children.length == 1) {
            return moveDirectory(children[0], target);
        }
        return moveDirectory(tmpDir, target);

    } finally {
        deleteRecursively(tmpDir);
    }
}

From source file:org.jboss.datavirt.commons.dev.server.util.ArchiveUtils.java

/**
 * Unpacks the given archive file into the output directory.
 * @param archiveFile an archive file/* w ww .  j  a v  a 2s .co  m*/
 * @param toDir where to unpack the archive to
 * @throws IOException
 */
public static void unpackToWorkDir(File archiveFile, File toDir) throws IOException {
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(archiveFile);
        Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntries();
        while (zipEntries.hasMoreElements()) {
            ZipArchiveEntry entry = zipEntries.nextElement();
            String entryName = entry.getName();
            File outFile = new File(toDir, entryName);
            if (!outFile.getParentFile().exists()) {
                if (!outFile.getParentFile().mkdirs()) {
                    throw new IOException(
                            "Failed to create parent directory: " + outFile.getParentFile().getCanonicalPath());
                }
            }

            if (entry.isDirectory()) {
                if (!outFile.mkdir()) {
                    throw new IOException("Failed to create directory: " + outFile.getCanonicalPath());
                }
            } else {
                InputStream zipStream = null;
                OutputStream outFileStream = null;

                zipStream = zipFile.getInputStream(entry);
                outFileStream = new FileOutputStream(outFile);
                try {
                    IOUtils.copy(zipStream, outFileStream);
                } finally {
                    IOUtils.closeQuietly(zipStream);
                    IOUtils.closeQuietly(outFileStream);
                }
            }
        }
    } finally {
        ZipFile.closeQuietly(zipFile);
    }
}

From source file:org.jboss.qa.jenkins.test.executor.utils.unpack.UnZipper.java

@Override
public void unpack(File archive, File destination) throws IOException {
    try (ZipFile zip = new ZipFile(archive)) {
        final Set<ZipArchiveEntry> entries = new HashSet<>(Collections.list(zip.getEntries()));

        if (ignoreRootFolders) {
            pathSegmentsToTrim = countRootFolders(entries);
        }/*from w  w  w .ja v a 2s. c  o m*/

        for (ZipArchiveEntry entry : entries) {
            if (entry.isDirectory()) {
                continue;
            }
            final String zipPath = trimPathSegments(entry.getName(), pathSegmentsToTrim);
            final File file = new File(destination, zipPath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs(); // Create parent folders if not exist
            }

            try (InputStream is = zip.getInputStream(entry); OutputStream fos = new FileOutputStream(file)) {
                IOUtils.copy(is, fos);
                // check for user-executable bit on entry and apply to file
                if ((entry.getUnixMode() & 0100) != 0) {
                    file.setExecutable(true);
                }
            }
            file.setLastModified(entry.getTime());
        }
    }
}

From source file:org.kuali.ole.docstore.model.bagit.BagExtractor.java

/**
 * @param bagFile//from   w  ww.j a  va  2 s . c  o  m
 * @return
 */
public static boolean verifyBag(File bagFile) throws IOException, FormatHelper.UnknownFormatException {

    SimpleResult result = null;
    Bag bag;
    LOG.info("synchronized verifyBagggggg " + bagFile.getAbsolutePath());
    synchronized (bf) {
        LOG.info("synchronizeddddd");
        System.out.println("yes");
        //    try {
        Bag.Format format = FormatHelper.getFormat(bagFile);
        LOG.info("format " + format);
        //gov.loc.repository.bagit.filesystem.FileSystem fileFileSystem = new FileFileSystem(bagFile);
        gov.loc.repository.bagit.filesystem.FileSystem zipFileSystem = new ZipFileSystem(bagFile);
        //LOG.info("FileFileSystem "+fileFileSystem);

        Iterator ite = zipFileSystem.getRoot().listChildren().iterator();
        while (ite.hasNext()) {
            FileSystemNode fs = (FileSystemNode) ite.next();
            ZipFile zipFile = new ZipFile(bagFile);
            Enumeration<ZipArchiveEntry> entryEnum = zipFile.getEntries();
            while (entryEnum.hasMoreElements()) {
                ZipArchiveEntry entry = entryEnum.nextElement();
                String entryFilepath = entry.getName();
                LOG.info("entryFilepath " + entryFilepath);
                if (entryFilepath.endsWith("/")) {
                    LOG.info("in if entryFilepath.endsWith");
                    entryFilepath = entryFilepath.substring(0, entryFilepath.length() - 1);
                } else {
                    //entryFilepath = entryFilepath.substring(0, entryFilepath.length()-1);
                    LOG.info("in else entryFilepath.endsWith entryFilepath " + entryFilepath);
                }
                File parentFile = new File(entryFilepath).getParentFile();

                List<String> parentPaths = new ArrayList<String>();
                while (parentFile != null) {
                    parentPaths.add((parentFile.getPath()));
                    parentFile = parentFile.getParentFile();
                }
                LOG.info("parentPaths " + parentPaths);
            }
        }

        //}
        //            catch (FormatHelper.UnknownFormatException e) {
        //                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        //            }

        bag = bf.createBag(bagFile);
        LOG.info("synchronized AFTER createBag");
    }
    result = bag.verifyValid();
    LOG.info("result " + result.getMessages());
    LOG.info("isSuccess " + result.isSuccess());
    return result.isSuccess();
}

From source file:org.mitre.xtext.converters.ArchiveNavigator.java

public File unzip(File zipFile) throws IOException {

    String _working = tempDir.getAbsolutePath() + File.separator + FilenameUtils.getBaseName(zipFile.getPath());
    File workingDir = new File(_working);
    workingDir.mkdir();/* w  w w .  java  2 s .c om*/

    InputStream input = new BufferedInputStream(new FileInputStream(zipFile));
    try {
        ZipArchiveInputStream in = (ZipArchiveInputStream) (new ArchiveStreamFactory()
                .createArchiveInputStream("zip", input));

        ZipArchiveEntry zipEntry;
        while ((zipEntry = (ZipArchiveEntry) in.getNextEntry()) != null) {
            if (filterEntry(zipEntry)) {
                continue;
            }

            try {
                File tmpFile = saveArchiveEntry(zipEntry, in, _working);
                converter.convert(tmpFile);

            } catch (IOException err) {
                log.error("Unable to save item, FILE=" + zipEntry.getName() + "!" + zipEntry.getName(), err);
            }
        }
        in.close();
    } catch (ArchiveException ae) {
        throw new IOException(ae);
    }

    return workingDir;
}

From source file:org.moe.cli.utils.ArchiveUtils.java

public static void unzipArchive(ZipFile zipFile, File destination) throws IOException {
    Enumeration<ZipArchiveEntry> e = zipFile.getEntries();
    InputStream is = null;/*  ww  w  .  j av a 2  s .co  m*/
    FileOutputStream fStream = null;
    try {
        while (e.hasMoreElements()) {
            ZipArchiveEntry entry = e.nextElement();
            if (entry.isDirectory()) {
                String dest = entry.getName();
                File destFolder = new File(destination, dest);
                if (!destFolder.exists()) {
                    destFolder.mkdirs();
                }
            } else {
                if (!entry.isUnixSymlink()) {
                    String dest = entry.getName();
                    File destFile = new File(destination, dest);
                    is = zipFile.getInputStream(entry); // get the input stream
                    fStream = new FileOutputStream(destFile);
                    copyFiles(is, fStream);
                } else {
                    String link = zipFile.getUnixSymlink(entry);

                    String entryName = entry.getName();
                    int parentIdx = entryName.lastIndexOf("/");

                    String newLink = entryName.substring(0, parentIdx) + "/" + link;
                    File destFile = new File(destination, newLink);
                    File linkFile = new File(destination, entryName);

                    Files.createSymbolicLink(Paths.get(linkFile.getPath()), Paths.get(destFile.getPath()));

                }
            }
        }
    } finally {
        if (is != null) {
            is.close();
        }

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