List of usage examples for org.apache.commons.compress.archivers ArchiveStreamFactory createArchiveInputStream
public ArchiveInputStream createArchiveInputStream(final InputStream in) throws ArchiveException
From source file:com.ttech.cordovabuild.infrastructure.archive.ArchiveUtils.java
public static void extractFiles(InputStream is, Path localPath) { ArchiveStreamFactory archiveStreamFactory = new ArchiveStreamFactory(); try {// www. jav a2s . co m Files.createDirectories(localPath); } catch (IOException e) { throw new ArchiveExtractionException(e); } try (ArchiveInputStream ais = archiveStreamFactory.createArchiveInputStream(is);) { extractArchive(localPath, ais); } catch (ArchiveException e) { LOGGER.info("archiveFactory could not determine archive file type probably tar.gz"); try (ArchiveInputStream ais = new TarArchiveInputStream(new GzipCompressorInputStream(is))) { extractArchive(localPath, ais); } catch (IOException e1) { throw new ArchiveExtractionException(e1); } } catch (IOException e) { throw new ArchiveExtractionException(e); } }
From source file:mj.ocraptor.extraction.tika.parser.pkg.ZipContainerDetector.java
private static MediaType detectArchiveFormat(byte[] prefix, int length) { try {//w ww . j a va 2 s. c om ArchiveStreamFactory factory = new ArchiveStreamFactory(); ArchiveInputStream ais = factory.createArchiveInputStream(new ByteArrayInputStream(prefix, 0, length)); try { if ((ais instanceof TarArchiveInputStream) && !TarArchiveInputStream.matches(prefix, length)) { // ArchiveStreamFactory is too relaxed, see COMPRESS-117 return MediaType.OCTET_STREAM; } else { return PackageParser.getMediaType(ais); } } finally { IOUtils.closeQuietly(ais); } } catch (ArchiveException e) { return MediaType.OCTET_STREAM; } }
From source file:com.geewhiz.pacify.test.TestUtil.java
private static void archiveDoesNotContainAdditionEntries(File replacedArchive, File expectedArchive) throws ArchiveException, IOException { ArchiveStreamFactory factory = new ArchiveStreamFactory(); FileInputStream replacedIS = new FileInputStream(replacedArchive); ArchiveInputStream replacedAIS = factory.createArchiveInputStream(new BufferedInputStream(replacedIS)); ArchiveEntry replacedEntry = null;//from w w w. j a v a 2 s. co m while ((replacedEntry = replacedAIS.getNextEntry()) != null) { FileInputStream expectedIS = new FileInputStream(expectedArchive); ArchiveInputStream expectedAIS = factory.createArchiveInputStream(new BufferedInputStream(expectedIS)); ArchiveEntry expectedEntry = null; boolean entryFound = false; while ((expectedEntry = expectedAIS.getNextEntry()) != null) { Assert.assertNotNull("We expect an entry.", expectedEntry); if (!replacedEntry.getName().equals(expectedEntry.getName())) { continue; } entryFound = true; if (ArchiveUtils.isArchiveAndIsSupported(expectedEntry.getName())) { Assert.assertTrue("we expect a archive", ArchiveUtils.isArchiveAndIsSupported(replacedEntry.getName())); File replacedChildArchive = ArchiveUtils.extractFile(replacedArchive, ArchiveUtils.getArchiveType(replacedArchive), replacedEntry.getName()); File expectedChildArchive = ArchiveUtils.extractFile(expectedArchive, ArchiveUtils.getArchiveType(expectedArchive), expectedEntry.getName()); archiveDoesNotContainAdditionEntries(replacedChildArchive, expectedChildArchive); replacedChildArchive.delete(); expectedChildArchive.delete(); } break; } expectedIS.close(); Assert.assertTrue("Entry [" + replacedEntry.getName() + "] is not in the expected archive. This file shouldn't exist.", entryFound); } replacedIS.close(); }
From source file:com.geewhiz.pacify.test.TestUtil.java
private static void archiveContainsEntries(File replacedArchive, File expectedArchive) throws ArchiveException, IOException { ArchiveStreamFactory factory = new ArchiveStreamFactory(); FileInputStream expectedIS = new FileInputStream(expectedArchive); ArchiveInputStream expectedAIS = factory.createArchiveInputStream(new BufferedInputStream(expectedIS)); ArchiveEntry expectedEntry = null;/* www. j a va 2 s .com*/ while ((expectedEntry = expectedAIS.getNextEntry()) != null) { FileInputStream replacedIS = new FileInputStream(replacedArchive); ArchiveInputStream replacedAIS = factory.createArchiveInputStream(new BufferedInputStream(replacedIS)); ArchiveEntry replacedEntry = null; boolean entryFound = false; while ((replacedEntry = replacedAIS.getNextEntry()) != null) { Assert.assertNotNull("We expect an entry.", replacedEntry); if (!expectedEntry.getName().equals(replacedEntry.getName())) { continue; } entryFound = true; if (expectedEntry.isDirectory()) { Assert.assertTrue("we expect a directory", replacedEntry.isDirectory()); break; } if (ArchiveUtils.isArchiveAndIsSupported(expectedEntry.getName())) { Assert.assertTrue("we expect a archive", ArchiveUtils.isArchiveAndIsSupported(replacedEntry.getName())); File replacedChildArchive = ArchiveUtils.extractFile(replacedArchive, ArchiveUtils.getArchiveType(replacedArchive), replacedEntry.getName()); File expectedChildArchive = ArchiveUtils.extractFile(expectedArchive, ArchiveUtils.getArchiveType(expectedArchive), expectedEntry.getName()); archiveContainsEntries(replacedChildArchive, expectedChildArchive); replacedChildArchive.delete(); expectedChildArchive.delete(); break; } ByteArrayOutputStream expectedContent = readContent(expectedAIS); ByteArrayOutputStream replacedContent = readContent(replacedAIS); Assert.assertEquals("Content should be same of entry " + expectedEntry.getName(), expectedContent.toString("UTF-8"), replacedContent.toString("UTF-8")); break; } replacedIS.close(); Assert.assertTrue("Entry [" + expectedEntry.getName() + "] in the result archive expected.", entryFound); } expectedIS.close(); }
From source file:mj.ocraptor.extraction.tika.parser.pkg.PackageParser.java
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { // At the end we want to close the archive stream to release // any associated resources, but the underlying document stream // should not be closed stream = new CloseShieldInputStream(stream); // Ensure that the stream supports the mark feature stream = new BufferedInputStream(stream); ArchiveInputStream ais;/*w w w. jav a 2 s.c om*/ try { ArchiveStreamFactory factory = new ArchiveStreamFactory(); ais = factory.createArchiveInputStream(stream); } catch (ArchiveException e) { throw new TikaException("Unable to unpack document stream", e); } MediaType type = getMediaType(ais); if (!type.equals(MediaType.OCTET_STREAM)) { metadata.set(CONTENT_TYPE, type.toString()); } // Use the delegate parser to parse the contained document EmbeddedDocumentExtractor extractor = context.get(EmbeddedDocumentExtractor.class, new ParsingEmbeddedDocumentExtractor(context)); XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); try { ArchiveEntry entry = ais.getNextEntry(); while (entry != null) { if (!entry.isDirectory()) { parseEntry(ais, entry, extractor, xhtml); } entry = ais.getNextEntry(); } } finally { ais.close(); } xhtml.endDocument(); }
From source file:at.beris.virtualfile.provider.LocalArchiveOperationProvider.java
@Override public List<File> list(FileModel model, Filter filter) throws IOException { List<File> fileList = new ArrayList<>(); ArchiveInputStream ais = null;/*from w ww . ja v a 2 s . co m*/ InputStream fis = null; URL rootUrl = model.getUrl(); try { ArchiveStreamFactory factory = new ArchiveStreamFactory(); fis = new BufferedInputStream(new FileInputStream(new java.io.File(model.getUrl().toURI()))); ais = factory.createArchiveInputStream(fis); ArchiveEntry archiveEntry; while ((archiveEntry = ais.getNextEntry()) != null) { Map<String, URL> urlMap = getArchiveEntryURLMap(rootUrl, archiveEntry); File file = fileContext.newFile(urlMap.get(URL)); if (filter == null || filter.filter(file)) fileList.add(file); } } catch (ArchiveException | URISyntaxException e) { throw new IOException(e); } finally { if (ais != null) ais.close(); if (fis != null) fis.close(); } return fileList; }
From source file:at.beris.virtualfile.provider.LocalArchiveOperationProvider.java
@Override public List<File> extract(FileModel model, File target) throws IOException { List<File> fileList = new ArrayList<>(); ArchiveInputStream ais = null;//from w w w. j ava2 s. co m InputStream fis = null; try { target.create(); ArchiveStreamFactory archiveStreamFactory = new ArchiveStreamFactory(); fis = new BufferedInputStream(new FileInputStream(new java.io.File(model.getUrl().toURI()))); ais = archiveStreamFactory.createArchiveInputStream(fis); ArchiveEntry archiveEntry; while ((archiveEntry = ais.getNextEntry()) != null) { Map<String, URL> urlMap = getArchiveEntryURLMap(target.getUrl(), archiveEntry); if (archiveEntry.isDirectory()) { Files.createDirectory(new java.io.File(urlMap.get(URL).toURI()).toPath()); } else { OutputStream out = new FileOutputStream(new java.io.File(urlMap.get(URL).toURI())); IOUtils.copy(ais, out); out.close(); } File file = fileContext.newFile(urlMap.get(URL)); fileList.add(file); } } catch (ArchiveException e) { throw new IOException(e); } catch (URISyntaxException e) { throw new IOException(e); } finally { if (ais != null) ais.close(); if (fis != null) fis.close(); } return fileList; }
From source file:at.beris.virtualfile.provider.LocalArchivedFileOperationProvider.java
@Override public Boolean exists(FileModel model) throws IOException { String archivePath = getArchivePath(model); String targetArchiveEntryPath = model.getUrl().getPath().substring(archivePath.length() + 1); ArchiveInputStream ais = null;// w w w. j av a 2 s. co m InputStream fis = null; try { ArchiveStreamFactory factory = new ArchiveStreamFactory(); fis = new BufferedInputStream(new FileInputStream(new java.io.File(archivePath))); ais = factory.createArchiveInputStream(fis); ArchiveEntry archiveEntry; while ((archiveEntry = ais.getNextEntry()) != null) { String archiveEntryPath = archiveEntry.getName(); if (archiveEntryPath.equals(targetArchiveEntryPath)) { model.setSize(archiveEntry.getSize()); model.setLastModifiedTime(FileTime.fromMillis(archiveEntry.getLastModifiedDate().getTime())); if (model.getUrl().toString().endsWith("/") && (!archiveEntry.isDirectory())) { String urlString = model.getUrl().toString(); model.setUrl(UrlUtils.newUrl(urlString.substring(0, urlString.length() - 1))); } else if (!model.getUrl().toString().endsWith("/") && (archiveEntry.isDirectory())) { String urlString = model.getUrl().toString() + "/"; model.setUrl(UrlUtils.newUrl(urlString)); } break; } } } catch (ArchiveException e) { throw new IOException(e); } finally { if (ais != null) ais.close(); if (fis != null) fis.close(); } return false; }
From source file:org.apache.tika.parser.pkg.PackageParser.java
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { //lazily load the MediaTypeRegistry at parse time //only want to call getDefaultConfig() once, and can't //load statically because of the ForkParser TikaConfig config = context.get(TikaConfig.class); MediaTypeRegistry mediaTypeRegistry = null; if (config != null) { mediaTypeRegistry = config.getMediaTypeRegistry(); } else {/* w ww . j ava2 s.c o m*/ if (bufferedMediaTypeRegistry == null) { //buffer this for next time. synchronized (lock) { //now that we're locked, check again if (bufferedMediaTypeRegistry == null) { bufferedMediaTypeRegistry = TikaConfig.getDefaultConfig().getMediaTypeRegistry(); } } } mediaTypeRegistry = bufferedMediaTypeRegistry; } // Ensure that the stream supports the mark feature if (!stream.markSupported()) { stream = new BufferedInputStream(stream); } TemporaryResources tmp = new TemporaryResources(); ArchiveInputStream ais = null; try { ArchiveStreamFactory factory = context.get(ArchiveStreamFactory.class, new ArchiveStreamFactory()); // At the end we want to close the archive stream to release // any associated resources, but the underlying document stream // should not be closed ais = factory.createArchiveInputStream(new CloseShieldInputStream(stream)); } catch (StreamingNotSupportedException sne) { // Most archive formats work on streams, but a few need files if (sne.getFormat().equals(ArchiveStreamFactory.SEVEN_Z)) { // Rework as a file, and wrap stream.reset(); TikaInputStream tstream = TikaInputStream.get(stream, tmp); // Seven Zip suports passwords, was one given? String password = null; PasswordProvider provider = context.get(PasswordProvider.class); if (provider != null) { password = provider.getPassword(metadata); } SevenZFile sevenz; if (password == null) { sevenz = new SevenZFile(tstream.getFile()); } else { sevenz = new SevenZFile(tstream.getFile(), password.getBytes("UnicodeLittleUnmarked")); } // Pending a fix for COMPRESS-269 / TIKA-1525, this bit is a little nasty ais = new SevenZWrapper(sevenz); } else { tmp.close(); throw new TikaException("Unknown non-streaming format " + sne.getFormat(), sne); } } catch (ArchiveException e) { tmp.close(); throw new TikaException("Unable to unpack document stream", e); } updateMediaType(ais, mediaTypeRegistry, metadata); // Use the delegate parser to parse the contained document EmbeddedDocumentExtractor extractor = EmbeddedDocumentUtil.getEmbeddedDocumentExtractor(context); XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); try { ArchiveEntry entry = ais.getNextEntry(); while (entry != null) { if (!entry.isDirectory()) { parseEntry(ais, entry, extractor, metadata, xhtml); } entry = ais.getNextEntry(); } } catch (UnsupportedZipFeatureException zfe) { // If it's an encrypted document of unknown password, report as such if (zfe.getFeature() == Feature.ENCRYPTION) { throw new EncryptedDocumentException(zfe); } // Otherwise throw the exception throw new TikaException("UnsupportedZipFeature", zfe); } catch (PasswordRequiredException pre) { throw new EncryptedDocumentException(pre); } finally { ais.close(); tmp.close(); } xhtml.endDocument(); }
From source file:org.fcrepo.sequencer.archive.ArchiveSequencer.java
/** * TODO// w w w .j a va2 s. c o m * * @param in * @return * @throws IOException * @throws ArchiveException */ public static String listContents(final InputStream in) throws IOException, ArchiveException { final ArchiveStreamFactory factory = new ArchiveStreamFactory(); try (ArchiveInputStream arc = factory.createArchiveInputStream(new BufferedInputStream(in))) { final StringBuffer contents = new StringBuffer(); for (ArchiveEntry entry = null; (entry = arc.getNextEntry()) != null;) { contents.append(entry.getName()); contents.append("\n"); } return contents.toString(); } catch (final Exception ex) { LOG.error("Error parsing archive input", ex); throw propagate(ex); } }