List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveInputStream getNextZipEntry
public ZipArchiveEntry getNextZipEntry() throws IOException
From source file:org.apache.tika.parser.utils.ZipSalvager.java
/** * This streams the broken zip and rebuilds a new zip that * is at least a valid zip file. The contents of the final stream * may be truncated, but the result should be a valid zip file. * <p>// w w w .j a va 2 s.c o m * This does nothing fancy to fix the underlying broken zip. * * @param brokenZip * @param salvagedZip */ public static void salvageCopy(InputStream brokenZip, File salvagedZip) { try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(salvagedZip)) { ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(brokenZip); ZipArchiveEntry zae = zipArchiveInputStream.getNextZipEntry(); while (zae != null) { try { if (!zae.isDirectory() && zipArchiveInputStream.canReadEntryData(zae)) { //create a new ZAE and copy over only the name so that //if there is bad info (e.g. CRC) in brokenZip's zae, that //won't be propagated or cause an exception outputStream.putArchiveEntry(new ZipArchiveEntry(zae.getName())); //this will copy an incomplete stream...so there //could be truncation of the xml/contents, but the zip file //should be intact. boolean successfullyCopied = false; try { IOUtils.copy(zipArchiveInputStream, outputStream); successfullyCopied = true; } catch (IOException e) { //this can hit a "truncated ZipFile" IOException } outputStream.flush(); outputStream.closeArchiveEntry(); if (!successfullyCopied) { break; } } zae = zipArchiveInputStream.getNextZipEntry(); } catch (ZipException | EOFException e) { break; } } outputStream.flush(); outputStream.finish(); } catch (IOException e) { LOG.warn("problem fixing zip", e); } }
From source file:org.callimachusproject.behaviours.ZipArchiveSupport.java
public void validateZipAndClose(InputStream in) throws IOException { ZipArchiveInputStream zip = new ZipArchiveInputStream(in); try {/* w ww. j a va 2 s . c o m*/ byte[] buf = new byte[1024]; ZipArchiveEntry entry = zip.getNextZipEntry(); if (entry == null) throw new BadRequest("Archive is empty"); do { entry.getName(); entry.getMethod(); entry.getSize(); while (zip.read(buf, 0, buf.length) >= 0) ; entry = zip.getNextZipEntry(); } while (entry != null); } finally { zip.close(); } }
From source file:org.callimachusproject.behaviours.ZipArchiveSupport.java
public InputStream readZipEntry(String match) throws IOException { InputStream in = this.openInputStream(); try {// w w w . ja v a2s .co m ZipArchiveInputStream zip = new ZipArchiveInputStream(in); byte[] buf = new byte[1024]; ZipArchiveEntry entry = zip.getNextZipEntry(); do { if (entry.getName().equals(match)) { return zip; } long size = entry.getSize(); if (size > 0) { zip.skip(size); } else { while (zip.read(buf, 0, buf.length) >= 0) ; } entry = zip.getNextZipEntry(); } while (entry != null); zip.close(); } catch (RuntimeException | Error | IOException e) { in.close(); throw e; } return null; }
From source file:org.callimachusproject.behaviours.ZipArchiveSupport.java
public XMLEventReader createAtomFeedFromArchive(final String id, final String entryPattern) throws IOException { final FileObject file = this; final XMLEventFactory ef = XMLEventFactory.newInstance(); final byte[] buf = new byte[1024]; final ZipArchiveInputStream zip = new ZipArchiveInputStream(file.openInputStream()); return new XMLEventReaderBase() { private boolean started; private boolean ended; public void close() throws XMLStreamException { try { zip.close();/*from w w w. j ava 2s . c o m*/ } catch (IOException e) { throw new XMLStreamException(e); } } protected boolean more() throws XMLStreamException { try { ZipArchiveEntry entry; if (!started) { Namespace atom = ef.createNamespace(FEED.getPrefix(), FEED.getNamespaceURI()); add(ef.createStartDocument()); add(ef.createStartElement(FEED, null, Arrays.asList(atom).iterator())); add(ef.createStartElement(TITLE, null, null)); add(ef.createCharacters(file.getName())); add(ef.createEndElement(TITLE, null)); add(ef.createStartElement(ID, null, null)); add(ef.createCharacters(id)); add(ef.createEndElement(ID, null)); Attribute href = ef.createAttribute("href", file.toUri().toASCIIString()); List<Attribute> attrs = Arrays.asList(href, ef.createAttribute("type", "application/zip")); add(ef.createStartElement(LINK, attrs.iterator(), null)); add(ef.createEndElement(LINK, null)); add(ef.createStartElement(UPDATED, null, null)); add(ef.createCharacters(format(new Date(file.getLastModified())))); add(ef.createEndElement(UPDATED, null)); started = true; return true; } else if (started && !ended && (entry = zip.getNextZipEntry()) != null) { String name = entry.getName(); String link = entryPattern.replace("{entry}", PercentCodec.encode(name)); MimetypesFileTypeMap mimetypes = new javax.activation.MimetypesFileTypeMap(); String type = mimetypes.getContentType(name); if (type == null || type.length() == 0) { type = "application/octet-stream"; } add(ef.createStartElement(ENTRY, null, null)); add(ef.createStartElement(TITLE, null, null)); add(ef.createCharacters(name)); add(ef.createEndElement(TITLE, null)); Attribute href = ef.createAttribute("href", link); List<Attribute> attrs = Arrays.asList(href, ef.createAttribute("type", type)); add(ef.createStartElement(LINK, attrs.iterator(), null)); add(ef.createEndElement(LINK, null)); long size = entry.getSize(); if (size > 0) { zip.skip(size); } else { while (zip.read(buf, 0, buf.length) >= 0) ; } add(ef.createEndElement(ENTRY, null)); return true; } else if (!ended) { add(ef.createEndElement(FEED, null)); add(ef.createEndDocument()); ended = true; return true; } else { return false; } } catch (IOException e) { throw new XMLStreamException(e); } } }; }
From source file:org.dbflute.helper.io.compress.DfZipArchiver.java
/** * Extract the archive file to the directory. * @param baseDir The base directory to compress. (NotNull) * @param filter The file filter, which doesn't need to accept the base directory. (NotNull) *//*from w w w . j a va 2s. co m*/ public void extract(File baseDir, FileFilter filter) { if (baseDir == null) { String msg = "The argument 'baseDir' should not be null."; throw new IllegalArgumentException(msg); } if (baseDir.exists() && !baseDir.isDirectory()) { String msg = "The baseDir should be directory but not: " + baseDir; throw new IllegalArgumentException(msg); } baseDir.mkdirs(); final String baseDirPath = resolvePath(baseDir); InputStream ins = null; ZipArchiveInputStream archive = null; try { ins = new FileInputStream(_zipFile); archive = new ZipArchiveInputStream(ins, "UTF-8", true); ZipArchiveEntry entry; while ((entry = archive.getNextZipEntry()) != null) { final String entryName = resolveFileSeparator(entry.getName()); // just in case final File file = new File(baseDirPath + "/" + entryName); if (!filter.accept(file)) { continue; } if (entry.isDirectory()) { file.mkdirs(); } else { if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } OutputStream out = null; try { out = new FileOutputStream(file); IOUtils.copy(archive, out); out.close(); } catch (IOException e) { String msg = "Failed to IO-copy the file: " + file.getPath(); throw new IllegalStateException(msg, e); } finally { if (out != null) { try { out.close(); } catch (IOException ignored) { } } } } } } catch (IOException e) { String msg = "Failed to extract the files from " + _zipFile.getPath(); throw new IllegalArgumentException(msg, e); } finally { if (archive != null) { try { archive.close(); } catch (IOException ignored) { } } if (ins != null) { try { ins.close(); } catch (IOException ignored) { } } } }
From source file:org.eclipse.cbi.maven.plugins.macsigner.SignMojo.java
/** * Decompresses zip files./* w w w.j a v a 2 s. c o m*/ * @param zipFile The zip file to decompress. * @throws IOException * @throws MojoExecutionException */ private static void unZip(File zipFile, File output_dir) throws IOException, MojoExecutionException { ZipArchiveInputStream zis = new ZipArchiveInputStream(new FileInputStream(zipFile)); ZipArchiveEntry ze; String name, parent; try { ze = zis.getNextZipEntry(); // check for at least one zip entry if (ze == null) { throw new MojoExecutionException("Could not decompress " + zipFile); } while (ze != null) { name = ze.getName(); //make directories if (ze.isDirectory()) { mkdirs(output_dir, name); } else { parent = getParentDirAbsolutePath(name); mkdirs(output_dir, parent); File outFile = new File(output_dir, name); outFile.createNewFile(); // check for match in executable list if (executableFiles.contains(name)) { Files.setPosixFilePermissions(outFile.toPath(), PosixFilePermissions.fromString("rwxr-x---")); } FileOutputStream fos = new FileOutputStream(outFile); copyInputStreamToOutputStream(zis, fos); fos.close(); } ze = zis.getNextZipEntry(); } } finally { zis.close(); } }
From source file:org.jwebsocket.util.Tools.java
/** * Uncompress a byte array using zip compression * * @param aBA/*w ww . jav a 2s. c o m*/ * @param aBase64Decode If TRUE, the byte array is Base64 decoded before uncompress * @return * @throws Exception */ public static byte[] unzip(byte[] aBA, Boolean aBase64Decode) throws Exception { if (aBase64Decode) { aBA = Base64.decodeBase64(aBA); } ByteArrayInputStream lBAIS = new ByteArrayInputStream(aBA); ZipArchiveInputStream lAIOS = new ZipArchiveInputStream(lBAIS); // ATTENTION: do not comment next line!!! lAIOS.getNextZipEntry(); ByteArrayOutputStream lBAOS = new ByteArrayOutputStream(); IOUtils.copy(lAIOS, lBAOS); lAIOS.close(); return lBAOS.toByteArray(); }
From source file:org.ngrinder.common.util.CompressionUtil.java
/** * Unzip the given input stream into destination directory with the given character set. * /*from w w w . j a va 2 s .com*/ * @param is * input stream * @param destDir * destination directory * @param charsetName * character set name */ public static void unzip(InputStream is, File destDir, String charsetName) { ZipArchiveInputStream zis = null; try { ZipArchiveEntry entry; String name; File target; int nWritten = 0; BufferedOutputStream bos; byte[] buf = new byte[1024 * 8]; zis = new ZipArchiveInputStream(is, charsetName, false); while ((entry = zis.getNextZipEntry()) != null) { name = entry.getName(); target = new File(destDir, name); if (entry.isDirectory()) { target.mkdirs(); /* does it always work? */ } else { target.createNewFile(); bos = new BufferedOutputStream(new FileOutputStream(target)); while ((nWritten = zis.read(buf)) >= 0) { bos.write(buf, 0, nWritten); } bos.close(); } } } catch (Exception e) { throw new NGrinderRuntimeException(e.getMessage(), e); } finally { IOUtils.closeQuietly(zis); } }
From source file:org.ngrinder.script.util.CompressionUtil.java
public void unzip(InputStream is, File destDir, String charsetName) throws IOException { ZipArchiveInputStream zis; ZipArchiveEntry entry;/*from w w w . j a va2 s . co m*/ String name; File target; int nWritten = 0; BufferedOutputStream bos; byte[] buf = new byte[1024 * 8]; zis = new ZipArchiveInputStream(is, charsetName, false); while ((entry = zis.getNextZipEntry()) != null) { name = entry.getName(); target = new File(destDir, name); if (entry.isDirectory()) { target.mkdirs(); /* does it always work? */ } else { target.createNewFile(); bos = new BufferedOutputStream(new FileOutputStream(target)); while ((nWritten = zis.read(buf)) >= 0) { bos.write(buf, 0, nWritten); } bos.close(); } } zis.close(); }
From source file:org.opencastproject.ingest.impl.IngestServiceImpl.java
/** * {@inheritDoc}/*from w w w .j av a2 s. co m*/ * * @see org.opencastproject.ingest.api.IngestService#addZippedMediaPackage(java.io.InputStream, java.lang.String, * java.util.Map, java.lang.Long) */ @Override public WorkflowInstance addZippedMediaPackage(InputStream zipStream, String workflowDefinitionId, Map<String, String> workflowConfig, Long workflowInstanceId) throws MediaPackageException, IOException, IngestException, NotFoundException, UnauthorizedException { // Start a job synchronously. We can't keep the open input stream waiting around. Job job = null; // Get hold of the workflow instance if specified WorkflowInstance workflowInstance = null; if (workflowInstanceId != null) { logger.info("Ingesting zipped mediapackage for workflow {}", workflowInstanceId); try { workflowInstance = workflowService.getWorkflowById(workflowInstanceId); } catch (NotFoundException e) { logger.debug("Ingest target workflow not found, starting a new one"); } catch (WorkflowDatabaseException e) { throw new IngestException(e); } } else { logger.info("Ingesting zipped mediapackage"); } ZipArchiveInputStream zis = null; Set<String> collectionFilenames = new HashSet<String>(); try { // We don't need anybody to do the dispatching for us. Therefore we need to make sure that the job is never in // QUEUED state but set it to INSTANTIATED in the beginning and then manually switch it to RUNNING. job = serviceRegistry.createJob(JOB_TYPE, INGEST_STREAM, null, null, false); job.setStatus(Status.RUNNING); serviceRegistry.updateJob(job); // Create the working file target collection for this ingest operation String wfrCollectionId = Long.toString(job.getId()); zis = new ZipArchiveInputStream(zipStream); ZipArchiveEntry entry; MediaPackage mp = null; Map<String, URI> uris = new HashMap<String, URI>(); // Sequential number to append to file names so that, if two files have the same // name, one does not overwrite the other int seq = 1; // While there are entries write them to a collection while ((entry = zis.getNextZipEntry()) != null) { try { if (entry.isDirectory() || entry.getName().contains("__MACOSX")) continue; if (entry.getName().endsWith("manifest.xml") || entry.getName().endsWith("index.xml")) { // Build the mediapackage mp = loadMediaPackageFromManifest(new ZipEntryInputStream(zis, entry.getSize())); } else { logger.info("Storing zip entry {} in working file repository collection '{}'", job.getId() + entry.getName(), wfrCollectionId); // Since the directory structure is not being mirrored, makes sure the file // name is different than the previous one(s) by adding a sequential number String fileName = FilenameUtils.getBaseName(entry.getName()) + "_" + seq++ + "." + FilenameUtils.getExtension(entry.getName()); URI contentUri = workingFileRepository.putInCollection(wfrCollectionId, fileName, new ZipEntryInputStream(zis, entry.getSize())); collectionFilenames.add(fileName); // Each entry name starts with zip file name; discard it so that the map key will match the media package // element uri String key = entry.getName().substring(entry.getName().indexOf('/') + 1); uris.put(key, contentUri); ingestStatistics.add(entry.getSize()); logger.info("Zip entry {} stored at {}", job.getId() + entry.getName(), contentUri); } } catch (IOException e) { logger.warn("Unable to process zip entry {}: {}", entry.getName(), e.getMessage()); throw e; } } if (mp == null) throw new MediaPackageException("No manifest found in this zip"); // Determine the mediapackage identifier String mediaPackageId = null; if (workflowInstance != null) { mediaPackageId = workflowInstance.getMediaPackage().getIdentifier().toString(); mp.setIdentifier(workflowInstance.getMediaPackage().getIdentifier()); } else { if (mp.getIdentifier() == null || StringUtils.isBlank(mp.getIdentifier().toString())) mp.setIdentifier(new UUIDIdBuilderImpl().createNew()); mediaPackageId = mp.getIdentifier().toString(); } logger.info("Ingesting mediapackage {} is named '{}'", mediaPackageId, mp.getTitle()); // Make sure there are tracks in the mediapackage if (mp.getTracks().length == 0) { logger.warn("Mediapackage {} has no media tracks", mediaPackageId); } // Update the element uris to point to their working file repository location for (MediaPackageElement element : mp.elements()) { URI uri = uris.get(element.getURI().toString()); if (uri == null) throw new MediaPackageException( "Unable to map element name '" + element.getURI() + "' to workspace uri"); logger.info("Ingested mediapackage element {}/{} is located at {}", new Object[] { mediaPackageId, element.getIdentifier(), uri }); URI dest = workingFileRepository.moveTo(wfrCollectionId, uri.toString(), mediaPackageId, element.getIdentifier(), FilenameUtils.getName(element.getURI().toString())); element.setURI(dest); // TODO: This should be triggered somehow instead of being handled here if (MediaPackageElements.SERIES.equals(element.getFlavor())) { logger.info("Ingested mediapackage {} contains updated series information", mediaPackageId); updateSeries(element.getURI()); } } // Now that all elements are in place, start with ingest logger.info("Initiating processing of ingested mediapackage {}", mediaPackageId); workflowInstance = ingest(mp, workflowDefinitionId, workflowConfig, workflowInstanceId); logger.info("Ingest of mediapackage {} done", mediaPackageId); job.setStatus(Job.Status.FINISHED); return workflowInstance; } catch (ServiceRegistryException e) { throw new IngestException(e); } catch (MediaPackageException e) { job.setStatus(Job.Status.FAILED, Job.FailureReason.DATA); if (workflowInstance != null) { workflowInstance.getCurrentOperation().setState(OperationState.FAILED); workflowInstance.setState(WorkflowState.FAILED); try { logger.info("Marking related workflow {} as failed", workflowInstance); workflowService.update(workflowInstance); } catch (WorkflowException e1) { logger.error("Error updating workflow instance {} with ingest failure: {}", workflowInstance, e1.getMessage()); } } throw e; } catch (Exception e) { job.setStatus(Job.Status.FAILED); if (workflowInstance != null) { workflowInstance.getCurrentOperation().setState(OperationState.FAILED); workflowInstance.setState(WorkflowState.FAILED); try { logger.info("Marking related workflow {} as failed", workflowInstance); workflowService.update(workflowInstance); } catch (WorkflowException e1) { logger.error("Error updating workflow instance {} with ingest failure: {}", workflowInstance, e1.getMessage()); } } if (e instanceof IngestException) throw (IngestException) e; throw new IngestException(e); } finally { IOUtils.closeQuietly(zis); for (String filename : collectionFilenames) { workingFileRepository.deleteFromCollection(Long.toString(job.getId()), filename); } try { serviceRegistry.updateJob(job); } catch (Exception e) { throw new IngestException("Unable to update job", e); } } }