List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveInputStream ZipArchiveInputStream
public ZipArchiveInputStream(InputStream inputStream)
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;// ww w. ja v a2 s .c o m 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()); 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;/*from ww w . j a v a 2 s . c o m*/ 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 { 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;/*from www . ja v a 2 s.c o m*/ 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 { 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:test.camel.support.ZipFileEntryTypeConverter.java
@Converter public static InputStream toInputStream(ZipFileEntry zipFileEntry, Exchange exchange) throws IOException { ZipArchiveInputStream zis = null;//w w w . j a v a 2s . co m InputStream fileInputStream = null; try { // Create input stream zis = new ZipArchiveInputStream( new BufferedInputStream(new FileInputStream(zipFileEntry.getZipFilePath()))); ArchiveEntry entry; while ((entry = zis.getNextEntry()) != null) { if (!entry.isDirectory() && entry.getName().equals(zipFileEntry.getFileEntry())) { fileInputStream = zis; break; } } } finally { if (fileInputStream == null) { IOUtils.closeQuietly(zis); } } if (fileInputStream == null) { String msg = MessageFormat.format("No file entry is found for {0} withing the {0} archive", zipFileEntry.getFileEntry(), zipFileEntry.getZipFilePath()); throw new FileNotFoundException(msg); } return fileInputStream; }
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 {// ww w . ja v a2 s.c o 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"); } }