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.xwiki.filter.test.internal.ZIPFileAssertComparator.java

private static Map<String, byte[]> unzip(File filename) throws IOException {
    Map<String, byte[]> zipContent = new HashMap<String, byte[]>();

    ZipFile zipFile = new ZipFile(filename);

    try {/*from ww  w. j  a v a  2s .co m*/
        Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
        while (entries.hasMoreElements()) {
            ZipArchiveEntry entry = entries.nextElement();

            InputStream inputStream = zipFile.getInputStream(entry);
            try {
                zipContent.put(entry.getName(), IOUtils.toByteArray(inputStream));
            } finally {
                inputStream.close();
            }
        }
    } finally {
        zipFile.close();
    }

    return zipContent;
}

From source file:org.xwiki.xar.XarPackage.java

private void readEntry(InputStream stream, ZipArchiveEntry entry) throws XarException, IOException {
    if (entry.getName().equals(XarModel.PATH_PACKAGE)) {
        readDescriptor(stream);//  ww w.  j  a  va  2  s .co  m
    } else {
        LocalDocumentReference reference = XarUtils.getReference(stream);

        // Get current action associated to the document
        int defaultAction = getDefaultAction(reference);

        // Create entry
        XarEntry xarEntry = new XarEntry(reference, entry.getName(), defaultAction);

        // Register entry
        this.entries.put(xarEntry, xarEntry);

        // Update existing package file entry name
        updatePackageFileEntryName(xarEntry);
    }
}

From source file:org.yes.cart.utils.impl.ZipUtils.java

private void unzipEntry(final ZipFile zipfile, final ZipArchiveEntry entry, final File outputDir)
        throws IOException {

    if (entry.isDirectory()) {
        createDir(new File(outputDir, entry.getName()));
        return;//from  w w  w.  j a  v a  2  s.com
    }

    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        createDir(outputFile.getParentFile());
    }

    BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

    try {
        IOUtils.copy(inputStream, outputStream);
    } finally {
        outputStream.close();
        inputStream.close();
    }
}

From source file:org.zuinnote.hadoop.office.format.common.writer.msexcel.internal.EncryptedZipEntrySource.java

public void setInputStream(InputStream is) throws IOException {
    this.tmpFile = TempFile.createTempFile("hadoopoffice-protected", ".zip");

    ZipArchiveInputStream zis = new ZipArchiveInputStream(is);
    FileOutputStream fos = new FileOutputStream(tmpFile);
    ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos);
    ZipArchiveEntry ze;
    while ((ze = (ZipArchiveEntry) zis.getNextEntry()) != null) {
        // rewrite zip entries to match the size of the encrypted data (with padding)
        ZipArchiveEntry zeNew = new ZipArchiveEntry(ze.getName());
        zeNew.setComment(ze.getComment());
        zeNew.setExtra(ze.getExtra());//from   w w  w.  j  a  va  2 s.c o  m
        zeNew.setTime(ze.getTime());
        zos.putArchiveEntry(zeNew);
        FilterOutputStream fos2 = new FilterOutputStream(zos) {
            // do not close underlyzing ZipOutputStream
            @Override
            public void close() {
            }
        };
        OutputStream nos;
        if (this.ciEncoder != null) { // encrypt if needed
            nos = new CipherOutputStream(fos2, this.ciEncoder);
        } else { // do not encrypt
            nos = fos2;
        }
        IOUtils.copy(zis, nos);
        nos.close();
        if (fos2 != null) {
            fos2.close();
        }
        zos.closeArchiveEntry();

    }
    zos.close();
    fos.close();
    zis.close();
    IOUtils.closeQuietly(is);
    this.zipFile = new ZipFile(this.tmpFile);

}

From source file:stroom.proxy.repo.StroomStreamProcessor.java

private void processZipStream(final InputStream inputStream, final String prefix) throws IOException {
    final ByteCountInputStream byteCountInputStream = new ByteCountInputStream(inputStream);

    final Map<String, MetaMap> bufferedMetaMap = new HashMap<>();
    final Map<String, Long> dataStreamSizeMap = new HashMap<>();
    final List<String> sendDataList = new ArrayList<>();
    final StroomZipNameSet stroomZipNameSet = new StroomZipNameSet(false);

    final ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(byteCountInputStream);

    ZipArchiveEntry zipEntry = null;
    while (true) {
        // We have to wrap our stream reading code in a individual try/catch
        // so we can return to the client an error in the case of a corrupt
        // stream.
        try {/*from   w w  w.  ja  v  a 2 s.c  om*/
            zipEntry = zipArchiveInputStream.getNextZipEntry();
        } catch (final IOException ioEx) {
            throw new StroomStreamException(StroomStatusCode.COMPRESSED_STREAM_INVALID, ioEx.getMessage());
        }

        if (zipEntry == null) {
            // All done
            break;
        }

        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("process() - " + zipEntry);
        }

        final String entryName = prefix + zipEntry.getName();
        final StroomZipEntry stroomZipEntry = stroomZipNameSet.add(entryName);

        if (StroomZipFileType.Meta.equals(stroomZipEntry.getStroomZipFileType())) {
            final MetaMap entryMetaMap = MetaMapFactory.cloneAllowable(globalMetaMap);
            // We have to wrap our stream reading code in a individual
            // try/catch so we can return to the client an error in the case
            // of a corrupt stream.
            try {
                entryMetaMap.read(zipArchiveInputStream, false);
            } catch (final IOException ioEx) {
                throw new StroomStreamException(StroomStatusCode.COMPRESSED_STREAM_INVALID, ioEx.getMessage());
            }

            if (appendReceivedPath) {
                // Here we build up a list of stroom servers that have received
                // the message

                // The entry one will be initially set at the boundary Stroom
                // server
                final String entryReceivedServer = entryMetaMap.get(StroomHeaderArguments.RECEIVED_PATH);

                if (entryReceivedServer != null) {
                    if (!entryReceivedServer.contains(getHostName())) {
                        entryMetaMap.put(StroomHeaderArguments.RECEIVED_PATH,
                                entryReceivedServer + "," + getHostName());
                    }
                } else {
                    entryMetaMap.put(StroomHeaderArguments.RECEIVED_PATH, getHostName());
                }
            }

            if (entryMetaMap.containsKey(StroomHeaderArguments.STREAM_SIZE)) {
                // Header already has stream size so just send it on
                sendHeader(stroomZipEntry, entryMetaMap);
            } else {
                // We need to add the stream size
                // Send the data file yet ?
                final String dataFile = stroomZipNameSet.getName(stroomZipEntry.getBaseName(),
                        StroomZipFileType.Data);
                if (dataFile != null && dataStreamSizeMap.containsKey(dataFile)) {
                    // Yes we can send the header now
                    entryMetaMap.put(StroomHeaderArguments.STREAM_SIZE,
                            String.valueOf(dataStreamSizeMap.get(dataFile)));
                    sendHeader(stroomZipEntry, entryMetaMap);
                } else {
                    // Else we have to buffer it
                    bufferedMetaMap.put(stroomZipEntry.getBaseName(), entryMetaMap);
                }
            }
        } else {
            handleEntryStart(stroomZipEntry);
            long totalRead = 0;
            int read = 0;
            while (true) {
                // We have to wrap our stream reading code in a individual
                // try/catch so we can return to the client an error in the
                // case of a corrupt stream.
                try {
                    read = StreamUtil.eagerRead(zipArchiveInputStream, buffer);
                } catch (final IOException ioEx) {
                    throw new StroomStreamException(StroomStatusCode.COMPRESSED_STREAM_INVALID,
                            ioEx.getMessage());
                }
                if (read == -1) {
                    break;
                }
                streamProgressMonitor.progress(read);
                handleEntryData(buffer, 0, read);
                totalRead += read;
            }
            handleEntryEnd();

            if (StroomZipFileType.Data.equals(stroomZipEntry.getStroomZipFileType())) {
                sendDataList.add(entryName);
                dataStreamSizeMap.put(entryName, totalRead);
            }

            // Buffered header can now be sent as we have sent the
            // data
            if (stroomZipEntry.getBaseName() != null) {
                final MetaMap entryMetaMap = bufferedMetaMap.remove(stroomZipEntry.getBaseName());
                if (entryMetaMap != null) {
                    entryMetaMap.put(StroomHeaderArguments.STREAM_SIZE, String.valueOf(totalRead));
                    handleEntryStart(
                            new StroomZipEntry(null, stroomZipEntry.getBaseName(), StroomZipFileType.Meta));
                    final byte[] headerBytes = entryMetaMap.toByteArray();
                    handleEntryData(headerBytes, 0, headerBytes.length);
                    handleEntryEnd();
                }
            }
        }

    }

    if (stroomZipNameSet.getBaseNameSet().isEmpty()) {
        if (byteCountInputStream.getByteCount() > 22) {
            throw new StroomStreamException(StroomStatusCode.COMPRESSED_STREAM_INVALID, "No Zip Entries");
        } else {
            LOGGER.warn("processZipStream() - Zip stream with no entries ! {}", globalMetaMap);
        }
    }

    // Add missing headers
    for (final String baseName : stroomZipNameSet.getBaseNameList()) {
        final String headerName = stroomZipNameSet.getName(baseName, StroomZipFileType.Meta);
        // Send Generic Header
        if (headerName == null) {
            final String dataFileName = stroomZipNameSet.getName(baseName, StroomZipFileType.Data);
            final MetaMap entryMetaMap = MetaMapFactory.cloneAllowable(globalMetaMap);
            entryMetaMap.put(StroomHeaderArguments.STREAM_SIZE,
                    String.valueOf(dataStreamSizeMap.remove(dataFileName)));
            sendHeader(new StroomZipEntry(null, baseName, StroomZipFileType.Meta), entryMetaMap);
        }
    }
}

From source file:stroom.util.zip.StroomStreamProcessor.java

private void processZipStream(final InputStream inputStream, final String prefix) throws IOException {
    final ByteCountInputStream byteCountInputStream = new ByteCountInputStream(inputStream);

    final Map<String, HeaderMap> bufferedHeaderMap = new HashMap<String, HeaderMap>();
    final Map<String, Long> dataStreamSizeMap = new HashMap<String, Long>();
    final List<String> sendDataList = new ArrayList<String>();
    final StroomZipNameSet stroomZipNameSet = new StroomZipNameSet(false);

    final ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(byteCountInputStream);

    ZipArchiveEntry zipEntry = null;
    while (true) {
        // We have to wrap our stream reading code in a individual try/catch
        // so we can return to the client an error in the case of a corrupt
        // stream.
        try {/*www .j  a v a2 s.  c  o m*/
            zipEntry = zipArchiveInputStream.getNextZipEntry();
        } catch (final IOException ioEx) {
            throw new StroomStreamException(StroomStatusCode.COMPRESSED_STREAM_INVALID, ioEx.getMessage());
        }

        if (zipEntry == null) {
            // All done
            break;
        }

        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("process() - " + zipEntry);
        }

        final String entryName = prefix + zipEntry.getName();
        final StroomZipEntry stroomZipEntry = stroomZipNameSet.add(entryName);

        if (StroomZipFileType.Meta.equals(stroomZipEntry.getStroomZipFileType())) {
            final HeaderMap entryHeaderMap = globalHeaderMap.cloneAllowable();
            // We have to wrap our stream reading code in a individual
            // try/catch so we can return to the client an error in the case
            // of a corrupt stream.
            try {
                entryHeaderMap.read(zipArchiveInputStream, false);
            } catch (final IOException ioEx) {
                throw new StroomStreamException(StroomStatusCode.COMPRESSED_STREAM_INVALID, ioEx.getMessage());
            }

            if (appendReceivedPath) {
                // Here we build up a list of stroom servers that have received
                // the message

                // The entry one will be initially set at the boundary Stroom
                // server
                final String entryReceivedServer = entryHeaderMap.get(StroomHeaderArguments.RECEIVED_PATH);

                if (entryReceivedServer != null) {
                    if (!entryReceivedServer.contains(getHostName())) {
                        entryHeaderMap.put(StroomHeaderArguments.RECEIVED_PATH,
                                entryReceivedServer + "," + getHostName());
                    }
                } else {
                    entryHeaderMap.put(StroomHeaderArguments.RECEIVED_PATH, getHostName());
                }
            }

            if (entryHeaderMap.containsKey(StroomHeaderArguments.STREAM_SIZE)) {
                // Header already has stream size so just send it on
                sendHeader(stroomZipEntry, entryHeaderMap);
            } else {
                // We need to add the stream size
                // Send the data file yet ?
                final String dataFile = stroomZipNameSet.getName(stroomZipEntry.getBaseName(),
                        StroomZipFileType.Data);
                if (dataFile != null && dataStreamSizeMap.containsKey(dataFile)) {
                    // Yes we can send the header now
                    entryHeaderMap.put(StroomHeaderArguments.STREAM_SIZE,
                            String.valueOf(dataStreamSizeMap.get(dataFile)));
                    sendHeader(stroomZipEntry, entryHeaderMap);
                } else {
                    // Else we have to buffer it
                    bufferedHeaderMap.put(stroomZipEntry.getBaseName(), entryHeaderMap);
                }
            }
        } else {
            handleEntryStart(stroomZipEntry);
            long totalRead = 0;
            int read = 0;
            while (true) {
                // We have to wrap our stream reading code in a individual
                // try/catch so we can return to the client an error in the
                // case of a corrupt stream.
                try {
                    read = StreamUtil.eagerRead(zipArchiveInputStream, buffer);
                } catch (final IOException ioEx) {
                    throw new StroomStreamException(StroomStatusCode.COMPRESSED_STREAM_INVALID,
                            ioEx.getMessage());
                }
                if (read == -1) {
                    break;
                }
                streamProgressMonitor.progress(read);
                handleEntryData(buffer, 0, read);
                totalRead += read;
            }
            handleEntryEnd();

            if (StroomZipFileType.Data.equals(stroomZipEntry.getStroomZipFileType())) {
                sendDataList.add(entryName);
                dataStreamSizeMap.put(entryName, totalRead);
            }

            // Buffered header can now be sent as we have sent the
            // data
            if (stroomZipEntry.getBaseName() != null) {
                final HeaderMap entryHeaderMap = bufferedHeaderMap.remove(stroomZipEntry.getBaseName());
                if (entryHeaderMap != null) {
                    entryHeaderMap.put(StroomHeaderArguments.STREAM_SIZE, String.valueOf(totalRead));
                    handleEntryStart(
                            new StroomZipEntry(null, stroomZipEntry.getBaseName(), StroomZipFileType.Meta));
                    final byte[] headerBytes = entryHeaderMap.toByteArray();
                    handleEntryData(headerBytes, 0, headerBytes.length);
                    handleEntryEnd();
                }
            }
        }

    }

    if (stroomZipNameSet.getBaseNameSet().isEmpty()) {
        if (byteCountInputStream.getByteCount() > 22) {
            throw new StroomStreamException(StroomStatusCode.COMPRESSED_STREAM_INVALID, "No Zip Entries");
        } else {
            LOGGER.warn("processZipStream() - Zip stream with no entries ! %s", globalHeaderMap);
        }
    }

    // Add missing headers
    for (final String baseName : stroomZipNameSet.getBaseNameList()) {
        final String headerName = stroomZipNameSet.getName(baseName, StroomZipFileType.Meta);
        // Send Generic Header
        if (headerName == null) {
            final String dataFileName = stroomZipNameSet.getName(baseName, StroomZipFileType.Data);
            final HeaderMap entryHeaderMap = globalHeaderMap.cloneAllowable();
            entryHeaderMap.put(StroomHeaderArguments.STREAM_SIZE,
                    String.valueOf(dataStreamSizeMap.remove(dataFileName)));
            sendHeader(new StroomZipEntry(null, baseName, StroomZipFileType.Meta), entryHeaderMap);
        }
    }
}

From source file:stroom.util.zip.StroomZipFile.java

public StroomZipNameSet getStroomZipNameSet() throws IOException {
    if (stroomZipNameSet == null) {
        stroomZipNameSet = new StroomZipNameSet(false);
        Enumeration<ZipArchiveEntry> entryE = getZipFile().getEntries();

        while (entryE.hasMoreElements()) {
            ZipArchiveEntry entry = entryE.nextElement();

            // Skip Dir's
            if (!entry.isDirectory()) {
                String fileName = entry.getName();
                stroomZipNameSet.add(fileName);
            }//from  w ww  .  ja v a 2 s .c  o m

            long entrySize = entry.getSize();
            if (entrySize == -1 || totalSize == -1) {
                // Can nolonger sum
            } else {
                totalSize += entrySize;
            }

        }

    }
    return stroomZipNameSet;
}

From source file:uk.ac.ebi.metabolights.utils.Zipper.java

public static void unzip2(String strZipFile, String folder) throws IOException, ArchiveException {

    //Check if the file exists
    FileUtil.fileExists(strZipFile, true);

    final InputStream is = new FileInputStream(strZipFile);
    ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("zip", is);

    ZipArchiveEntry entry = null;
    OutputStream out = null;//  w  ww  . j  ava 2  s . c  om

    while ((entry = (ZipArchiveEntry) in.getNextEntry()) != null) {

        //create directories if required.
        File zipPath = new File(folder);
        File destinationFilePath = new File(zipPath, entry.getName());
        destinationFilePath.getParentFile().mkdirs();

        //if the entry is directory, leave it. Otherwise extract it.
        if (entry.isDirectory()) {
            continue;
        } else {
            out = new FileOutputStream(new File(folder, entry.getName()));
            IOUtils.copy(in, out);
            out.close();
        }

    }

    in.close();

}

From source file:uk.ac.ebi.metabolights.webservice.utils.Zipper.java

public static void unzip2(String strZipFile, String folder) throws IOException, ArchiveException {

    // Remove any previous content of the unzip folder.
    File uf = new File(folder);
    FileUtil.deleteDir(uf);/*from w w w  .ja va2s . c om*/
    // Create it again, this time it will be empty..
    uf.mkdir();

    //Check if the file exists
    FileUtil.fileExists(strZipFile, true);

    final InputStream is = new FileInputStream(strZipFile);
    ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("zip", is);

    ZipArchiveEntry entry = null;
    OutputStream out = null;

    while ((entry = (ZipArchiveEntry) in.getNextEntry()) != null) {

        //create directories if required.
        File zipPath = new File(folder);
        File destinationFilePath = new File(zipPath, entry.getName());
        destinationFilePath.getParentFile().mkdirs();

        //if the entry is directory, leave it. Otherwise extract it.
        if (entry.isDirectory()) {
            continue;
        } else {
            out = new FileOutputStream(new File(folder, entry.getName()));
            IOUtils.copy(in, out);
            out.close();
        }

    }

    in.close();

}

From source file:uk.nhs.cfh.dsp.srth.distribution.FileDownloader.java

public void getFileFromTRUDArchive(String trudArchiveName, String fileName, String outputURL) {

    logger.info("Now getting file... ");
    if (ftpClient.isConnected()) {
        logger.info("ftp client is connected... ");
        // now get file specified by inputURL
        try {// w  ww .  java  2 s  . co  m
            ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
            logger.info("Sending NOOP command... ");
            // send NOOP command to see if connection is still active
            if (ftpClient.sendNoOp()) {
                logger.info("ftpClient.getReplyString() = " + ftpClient.getReplyString());
                FTPFile[] files = ftpClient.listFiles(getPackPath());
                for (FTPFile file : files) {
                    logger.info("file.getName() = " + file.getName());
                    logger.info("file.getSize() = " + file.getSize());
                    if (file.getName().equals(trudArchiveName) && file.getSize() > 0) {
                        InputStream is = ftpClient.retrieveFileStream(getPackPath() + trudArchiveName);

                        if (is != null) {
                            ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(is);
                            ZipArchiveEntry zipArchiveEntry = zipArchiveInputStream.getNextZipEntry();
                            while (zipArchiveEntry != null) {
                                String zippedArchiveEntryName = zipArchiveEntry.getName();
                                logger.info("zippedArchiveEntryName = " + zippedArchiveEntryName);
                                logger.info("fileName = " + fileName);
                                if (zippedArchiveEntryName.equals(fileName)) {
                                    logger.info("Extracting: " + zippedArchiveEntryName);
                                    File outputLocation = new File(outputURL);
                                    boolean canWrite = outputLocation.exists();
                                    logger.info("canWrite = " + canWrite);
                                    if (!canWrite) {
                                        canWrite = outputLocation.mkdirs();
                                        logger.info("canWrite after mkdirs = " + canWrite);
                                    }

                                    if (canWrite && outputLocation.canWrite()) {
                                        logger.info("outputLocation.getPath() = " + outputLocation.getPath());
                                        File destinationFile = new File(outputLocation.getAbsolutePath(),
                                                zippedArchiveEntryName);
                                        OutputStream out = new FileOutputStream(destinationFile);
                                        IOUtils.copy(zipArchiveInputStream, out);
                                        out.close();

                                        if (zippedArchiveEntryName.indexOf(".zip") > -1) {
                                            // unpackZip file
                                            extractZipFileContents(destinationFile);
                                        }
                                    }
                                } else {
                                    logger.info("Resetting zipArchiveEntry");
                                    zipArchiveEntry = zipArchiveInputStream.getNextZipEntry();
                                    if (zipArchiveEntry != null) {
                                        logger.info("zipArchiveEntry.getName() = " + zipArchiveEntry.getName());
                                    }
                                }

                            }

                            zipArchiveInputStream.close();
                            is.close();
                        }
                        break;
                    }
                }

                // complete pending commands; needed after opening and closing streams
                ftpClient.completePendingCommand();
            } else {
                logger.warning("FTP connection might have timed out.");
                ftpClient.disconnect();
            }
        } catch (IOException e) {
            logger.warning("FTP connection might have timed out. " + ERR_MESSAGE + e.fillInStackTrace());
        }
    } else {
        logger.warning("No connection to TRUD is available. Ensure FTP connection is open");
    }
}