org.apache.commons.compress.archivers.ArchiveInputStream Java Examples

The following examples show how to use org.apache.commons.compress.archivers.ArchiveInputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UnpackBuilder.java    From kite with Apache License 2.0 6 votes vote down vote up
private boolean parseEntry(ArchiveInputStream archive, ArchiveEntry entry, EmbeddedExtractor extractor, Record record) {
  String name = entry.getName();
  if (archive.canReadEntryData(entry)) {
    Record entrydata = new Record(); // TODO: or pass myself?
    //Record entrydata = record.copy();
    
    // For detectors to work, we need a mark/reset supporting
    // InputStream, which ArchiveInputStream isn't, so wrap
    TemporaryResources tmp = new TemporaryResources();
    try {
      TikaInputStream tis = TikaInputStream.get(archive, tmp);
      return extractor.parseEmbedded(tis, entrydata, name, getChild());
    } finally {
      try {
        tmp.dispose();
      } catch (TikaException e) {
        LOG.warn("Cannot dispose of tmp Tika resources", e);
      }
    }
  } else {
    return false;
  } 
}
 
Example #2
Source File: ExtractionTools.java    From aws-codepipeline-plugin-for-jenkins with Apache License 2.0 6 votes vote down vote up
private static void extractArchive(final File destination, final ArchiveInputStream archiveInputStream)
        throws IOException {
    final int BUFFER_SIZE = 8192;
    ArchiveEntry entry = archiveInputStream.getNextEntry();

    while (entry != null) {
        final File destinationFile = getDestinationFile(destination, entry.getName());

        if (entry.isDirectory()) {
            destinationFile.mkdir();
        } else {
            destinationFile.getParentFile().mkdirs();
            try (final OutputStream fileOutputStream = new FileOutputStream(destinationFile)) {
                final byte[] buffer = new byte[BUFFER_SIZE];
                int bytesRead;

                while ((bytesRead = archiveInputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, bytesRead);
                }
            }
        }

        entry = archiveInputStream.getNextEntry();
    }
}
 
Example #3
Source File: P4ExtFileUtils.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
private static void extractArchive(ArchiveInputStream archiveInputStream, File outputDir)
        throws IOException {
    createDirectory(outputDir);
    try {
        ArchiveEntry entry = archiveInputStream.getNextEntry();
        while (entry != null) {
            File node = new File(outputDir, entry.getName());
            if (entry.isDirectory()) {
                if (!node.mkdirs() && !node.isDirectory()) {
                    throw new IOException("Could not create directory " + node);
                }
            } else {
                extractFile(archiveInputStream, node);
            }
            entry = archiveInputStream.getNextEntry();
        }
    } finally {
        archiveInputStream.close();
    }
}
 
Example #4
Source File: PdpProcessor.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
public AssemblyTemplate registerPdpFromArchive(InputStream archiveInput) {
    try {
        ArchiveInputStream input = new ArchiveStreamFactory()
            .createArchiveInputStream(archiveInput);
        
        while (true) {
            ArchiveEntry entry = input.getNextEntry();
            if (entry==null) break;
            // TODO unpack entry, create a space on disk holding the archive ?
        }

        // use yaml...
        throw new UnsupportedOperationException("in progress");
        
    } catch (Exception e) {
        throw Exceptions.propagate(e);
    }
}
 
Example #5
Source File: FilesDecompressUnarchiveBatchController.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public void unarchive(File srcFile, BufferedInputStream fileIn) {
        try {
            archiver = ArchiveStreamFactory.detect(fileIn);
            if (archiver == null) {
                return;
            }
            if (archiver.equalsIgnoreCase(ArchiveStreamFactory.SEVEN_Z)) {
                unarchive7z(srcFile);
            } else if (archiver.equalsIgnoreCase(ArchiveStreamFactory.ZIP)) {
                unarchiveZip(srcFile);
            } else {
                try ( ArchiveInputStream in
                        = aFactory.createArchiveInputStream(archiver, fileIn, encoding)) {
                    unarchive(in);
                } catch (Exception ex) {
//                            logger.debug(ex.toString());
                }
            }
        } catch (Exception e) {
//            logger.debug(e.toString());
        }
    }
 
Example #6
Source File: PyPiInfoUtils.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Processes the entries in an archive, attempting to extract metadata. The first possible metadata file found wins.
 */
private static Map<String, String> processArchiveEntries(final ArchiveInputStream ais) throws Exception {
  checkNotNull(ais);
  ArchiveEntry entry = ais.getNextEntry();
  while (entry != null) {
    if (isMetadataFile(entry)) {
      Map<String, String> results = new LinkedHashMap<>();
      for (Entry<String, List<String>> attribute : parsePackageInfo(ais).entrySet()) {
        results.put(attribute.getKey(), String.join("\n", attribute.getValue()));
      }
      return results;
    }
    entry = ais.getNextEntry();
  }
  return new LinkedHashMap<>();
}
 
Example #7
Source File: RDescriptionUtils.java    From nexus-repository-r with Eclipse Public License 1.0 6 votes vote down vote up
private static Map<String, String> extractMetadataFromArchive(final String archiveType, final InputStream is) {
  final ArchiveStreamFactory archiveFactory = new ArchiveStreamFactory();
  try (ArchiveInputStream ais = archiveFactory.createArchiveInputStream(archiveType, is)) {
    ArchiveEntry entry = ais.getNextEntry();
    while (entry != null) {
      if (!entry.isDirectory() && DESCRIPTION_FILE_PATTERN.matcher(entry.getName()).matches()) {
        return parseDescriptionFile(ais);
      }
      entry = ais.getNextEntry();
    }
  }
  catch (ArchiveException | IOException e) {
    throw new RException(null, e);
  }
  throw new IllegalStateException("No metadata file found");
}
 
Example #8
Source File: TarContainerPacker.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] unpackContainerDescriptor(InputStream input)
    throws IOException {
  try (InputStream decompressed = decompress(input);
       ArchiveInputStream archiveInput = untar(decompressed)) {

    ArchiveEntry entry = archiveInput.getNextEntry();
    while (entry != null) {
      String name = entry.getName();
      if (CONTAINER_FILE_NAME.equals(name)) {
        return readEntry(archiveInput, entry.getSize());
      }
      entry = archiveInput.getNextEntry();
    }
  } catch (CompressorException e) {
    throw new IOException(
        "Can't read the container descriptor from the container archive",
        e);
  }

  throw new IOException(
      "Container descriptor is missing from the container archive.");
}
 
Example #9
Source File: NpmPackageParser.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parses the package.json in the supplied tar.gz if present and extractable. In all other situations, an empty map
 * will be returned indicating the absence of (or inability to extract) a valid package.json file and its contents.
 */
public Map<String, Object> parsePackageJson(final Supplier<InputStream> supplier) {
  try (InputStream is = new BufferedInputStream(supplier.get())) {
    final CompressorStreamFactory compressorStreamFactory = new CompressorStreamFactory();
    try (InputStream cis = compressorStreamFactory.createCompressorInputStream(GZIP, is)) {
      final ArchiveStreamFactory archiveFactory = new ArchiveStreamFactory();
      try (ArchiveInputStream ais = archiveFactory.createArchiveInputStream(TAR, cis)) {
        return parsePackageJsonInternal(ais);
      }
    }
  }
  catch (Exception e) {
    log.debug("Error occurred while processing package.json, returning empty map to continue", e);
    return emptyMap();
  }
}
 
Example #10
Source File: TarReader.java    From metafacture-core with Apache License 2.0 6 votes vote down vote up
@Override
public void process(final Reader reader) {
    try (
            InputStream stream = new ReaderInputStream(reader,
                    Charset.defaultCharset());
            ArchiveInputStream tarStream = new TarArchiveInputStream(stream);
    ) {
        ArchiveEntry entry;
        while ((entry = tarStream.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                processFileEntry(tarStream);
            }
        }
    } catch (IOException e) {
        throw new MetafactureException(e);
    }
}
 
Example #11
Source File: CompressExample.java    From spring-boot with Apache License 2.0 6 votes vote down vote up
public static void makeOnlyUnZip() throws ArchiveException, IOException{
		final InputStream is = new FileInputStream("D:/中文名字.zip");
		ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.ZIP, is);
		ZipArchiveEntry entry = entry = (ZipArchiveEntry) in.getNextEntry();
		
		String dir = "D:/cnname";
		File filedir = new File(dir);
		if(!filedir.exists()){
			filedir.mkdir();
		}
//		OutputStream out = new FileOutputStream(new File(dir, entry.getName()));
		OutputStream out = new FileOutputStream(new File(filedir, entry.getName()));
		IOUtils.copy(in, out);
		out.close();
		in.close();
	}
 
Example #12
Source File: ArchiveResource.java    From embedded-cassandra with Apache License 2.0 6 votes vote down vote up
@Override
public ArchiveInputStream getInputStream() throws IOException {
	Resource resource = this.resource;
	ArchiveInputStreamFactory archiveInputStreamFactory = createArchiveInputStreamFactory(resource);
	InputStream is = resource.getInputStream();
	try {
		return archiveInputStreamFactory.create(is);
	}
	catch (Exception ex) {
		try {
			is.close();
		}
		catch (Exception swallow) {
			ex.addSuppressed(swallow);
		}
		throw new IOException(ex);
	}
}
 
Example #13
Source File: ZipTest.java    From document-management-system with GNU General Public License v2.0 6 votes vote down vote up
public void testApache() throws IOException, ArchiveException {
	log.debug("testApache()");
	File zip = File.createTempFile("apache_", ".zip");
	
	// Create zip
	FileOutputStream fos = new FileOutputStream(zip);
	ArchiveOutputStream aos = new ArchiveStreamFactory().createArchiveOutputStream("zip", fos);
	aos.putArchiveEntry(new ZipArchiveEntry("coñeta"));
	aos.closeArchiveEntry();
	aos.close();
	
	// Read zip
	FileInputStream fis = new FileInputStream(zip);
	ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream("zip", fis);
	ZipArchiveEntry zae = (ZipArchiveEntry) ais.getNextEntry();
	assertEquals(zae.getName(), "coñeta");
	ais.close();
}
 
Example #14
Source File: CompressExtension.java    From jphp with Apache License 2.0 6 votes vote down vote up
@Override
public void onRegister(CompileScope scope) {
    // register classes ...
    registerWrapperClass(scope, ArchiveEntry.class, PArchiveEntry.class);
    registerWrapperClass(scope, TarArchiveEntry.class, PTarArchiveEntry.class);
    registerWrapperClass(scope, ZipArchiveEntry.class, PZipArchiveEntry.class);

    registerWrapperClass(scope, ArchiveInputStream.class, PArchiveInput.class);
    registerWrapperClass(scope, TarArchiveInputStream.class, PTarArchiveInput.class);
    registerWrapperClass(scope, ZipArchiveInputStream.class, PZipArchiveInput.class);

    registerWrapperClass(scope, ArchiveOutputStream.class, PArchiveOutput.class);
    registerWrapperClass(scope, TarArchiveOutputStream.class, PTarArchiveOutput.class);
    registerWrapperClass(scope, ZipArchiveOutputStream.class, PZipArchiveOutput.class);

    registerClass(scope, PGzipOutputStream.class);
    registerClass(scope, PGzipInputStream.class);
    registerClass(scope, PBzip2OutputStream.class);
    registerClass(scope, PBZip2InputStream.class);
    registerClass(scope, PLz4OutputStream.class);
    registerClass(scope, PLz4InputStream.class);

    registerClass(scope, PArchive.class);
    registerClass(scope, PTarArchive.class);
    registerClass(scope, PZipArchive.class);
}
 
Example #15
Source File: ComposerJsonExtractor.java    From nexus-repository-composer with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Extracts the contents for the first matching {@code composer.json} file (of which there should only be one) as a
 * map representing the parsed JSON content. If no such file is found then an empty map is returned.
 */
public Map<String, Object> extractFromZip(final Blob blob) throws IOException {
  try (InputStream is = blob.getInputStream()) {
    try (ArchiveInputStream ais = archiveStreamFactory.createArchiveInputStream(ArchiveStreamFactory.ZIP, is)) {
      ArchiveEntry entry = ais.getNextEntry();
      while (entry != null) {
        Map<String, Object> contents = processEntry(ais, entry);
        if (!contents.isEmpty()) {
          return contents;
        }
        entry = ais.getNextEntry();
      }
    }
    return Collections.emptyMap();
  }
  catch (ArchiveException e) {
    throw new IOException("Error reading from archive", e);
  }
}
 
Example #16
Source File: SetsArchiver.java    From cantor with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void restore(final Sets sets, final String namespace, final Path archiveFile) throws IOException {
    checkRestoreArguments(sets, namespace, archiveFile);
    // create the namespace, in case the user hasn't already
    sets.create(namespace);
    try (final ArchiveInputStream archive = getArchiveInputStream(archiveFile)) {
        ArchiveEntry entry;
        int total = 0;
        while ((entry = archive.getNextEntry()) != null) {
            final SetsChunk chunk = SetsChunk.parseFrom(archive);
            sets.add(namespace, chunk.getSet(), chunk.getEntriesMap());
            logger.info("read {} entries from chunk {} ({} bytes) into {}.{}", chunk.getEntriesCount(), entry.getName(), entry.getSize(), namespace, chunk.getSet());
            total += chunk.getEntriesCount();
        }
        logger.info("restored {} entries int namespace '{}' from archive file {}", total, namespace, archiveFile);
    }
}
 
Example #17
Source File: ObjectsArchiver.java    From cantor with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void restore(final Objects objects, final String namespace, final Path archiveFile) throws IOException {
    checkRestoreArguments(objects, namespace, archiveFile);
    // create the namespace, in case the user hasn't already
    objects.create(namespace);
    try (final ArchiveInputStream archive = getArchiveInputStream(archiveFile)) {
        ArchiveEntry entry;
        int total = 0;
        while ((entry = archive.getNextEntry()) != null) {
            final ObjectsChunk chunk = ObjectsChunk.parseFrom(archive);
            for (final Map.Entry<String, ByteString> chunkEntry : chunk.getObjectsMap().entrySet()) {
                objects.store(namespace, chunkEntry.getKey(), chunkEntry.getValue().toByteArray());
            }
            logger.info("read {} objects from chunk {} ({} bytes) into {}", chunk.getObjectsCount(), entry.getName(), entry.getSize(), objects);
            total += chunk.getObjectsCount();
        }
        logger.info("restored {} objects into namespace '{}' from archive file {}", total, namespace, archiveFile);
    }
}
 
Example #18
Source File: EventsArchiver.java    From cantor with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void restore(final Events events, final String namespace, final Path archiveFile) throws IOException {
    checkRestoreArguments(events, namespace, archiveFile);
    // create the namespace, in case the user hasn't already
    events.create(namespace);

    long startNanos = System.nanoTime();
    long totalEventsRestored = 0;
    try (final ArchiveInputStream archive = getArchiveInputStream(archiveFile)) {
        ArchiveEntry entry;
        while ((entry = archive.getNextEntry()) != null) {
            final EventsChunk chunk = EventsChunk.parseFrom(archive);
            for (final EventsChunk.Event event : chunk.getEventsList()) {
                if (ByteString.EMPTY.equals(event.getPayload())) {
                    events.store(namespace, event.getTimestampMillis(), event.getMetadataMap(), event.getDimensionsMap());
                } else {
                    events.store(namespace, event.getTimestampMillis(), event.getMetadataMap(), event.getDimensionsMap(), event.getPayload().toByteArray());
                }
            }
            logger.info("read {} entries from chunk {} ({} bytes) into {}", chunk.getEventsCount(), entry.getName(), entry.getSize(), namespace);
            totalEventsRestored += chunk.getEventsCount();
        }
    } finally {
        logger.info("restoring {} events into namespace '{}' from archive file {} took {}s",
                totalEventsRestored, namespace, archiveFile, TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startNanos));
    }
}
 
Example #19
Source File: ArchiveUtils.java    From dropwizard-debpkg-maven-plugin with Apache License 2.0 5 votes vote down vote up
private static void extractArchive(ArchiveInputStream in, File destination) throws IOException {
    for (ArchiveEntry entry; (entry = in.getNextEntry()) != null; ) {
        final File target = new File(destination, entry.getName());
        if (entry.isDirectory()) {
            target.mkdir();
        }
        else {
            Files.asByteSink(target).writeFrom(in);
        }
    }
}
 
Example #20
Source File: FuncotatorReferenceTestUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Extract a fasta tar.gz (should include only one fasta, the index, and a dict).  Though this is not enforced.
 *
 * Much of this code taken from: http://commons.apache.org/proper/commons-compress/examples.html
 *
 * @param fastaTarGz Should include one fasta file, the associated dict file and the associated index, though this is not enforced.  Never {@code null}
 * @param destDir Where to store te untarred files.  Never {@code null}
 * @return a path (as string) to the fasta file.  If multiple fasta files were in the tar gz, it will return that last one seen.
 */
private static String extractFastaTarGzToTemp(final File fastaTarGz, final Path destDir) {
    Utils.nonNull(fastaTarGz);
    Utils.nonNull(destDir);
    String result = null;
    try (final ArchiveInputStream i = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(fastaTarGz)))) {
        ArchiveEntry entry = null;
        while ((entry = i.getNextEntry()) != null) {
            if (!i.canReadEntryData(entry)) {
                logger.warn("Could not read an entry in the archive: " + entry.getName() + " from " + fastaTarGz.getAbsolutePath());
            }
            final String name = destDir + "/" + entry.getName();
            if (entry.getName().endsWith(".fasta")) {
                result = name;
            }
            final File f = new File(name);
            if (entry.isDirectory()) {
                if (!f.isDirectory() && !f.mkdirs()) {
                    throw new IOException("Failed to create directory " + f);
                }
            } else {
                File parent = f.getParentFile();
                if (!parent.isDirectory() && !parent.mkdirs()) {
                    throw new IOException("Failed to create directory " + parent);
                }
                try (OutputStream o = Files.newOutputStream(f.toPath())) {
                    IOUtils.copy(i, o);
                }
            }
        }
    } catch (final IOException ioe) {
        throw new GATKException("Could not untar test reference: " + fastaTarGz, ioe);
    }
    return result;
}
 
Example #21
Source File: ArchiveExtractor.java    From carbon-kernel with Apache License 2.0 5 votes vote down vote up
/**
 * Extract the file and create directories accordingly.
 *
 * @param is        pass the stream, because the stream is different for various file types
 * @param targetDir target directory
 * @throws IOException
 */
private static void extract(ArchiveInputStream is, File targetDir) throws IOException {
    try {
        if (targetDir.exists()) {
            FileUtils.forceDelete(targetDir);
        }
        createDirectory(targetDir);
        ArchiveEntry entry = is.getNextEntry();
        while (entry != null) {
            String name = entry.getName();
            name = name.substring(name.indexOf("/") + 1);
            File file = new File(targetDir, name);
            if (entry.isDirectory()) {
                createDirectory(file);
            } else {
                createDirectory(file.getParentFile());
                OutputStream os = new FileOutputStream(file);
                try {
                    IOUtils.copy(is, os);
                } finally {
                    IOUtils.closeQuietly(os);
                }
            }
            entry = is.getNextEntry();
        }
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            logger.warn("Error occurred while closing the stream", e);
        }
    }
}
 
Example #22
Source File: TarReader.java    From metafacture-core with Apache License 2.0 5 votes vote down vote up
private void processFileEntry(ArchiveInputStream archiveStream)
        throws IOException {
    try (
            InputStream entryStream = new ArchiveEntryInputStream(archiveStream);
            Reader entryReader = new InputStreamReader(entryStream);
    ) {
        getReceiver().process(entryReader);
    }
}
 
Example #23
Source File: CommonsArchiver.java    From jarchivelib with Apache License 2.0 5 votes vote down vote up
@Override
public void extract(File archive, File destination) throws IOException {
    assertExtractSource(archive);

    IOUtils.requireDirectory(destination);

    ArchiveInputStream input = null;
    try {
        input = createArchiveInputStream(archive);
        extract(input, destination);

    } finally {
        IOUtils.closeQuietly(input);
    }
}
 
Example #24
Source File: CommonsArchiver.java    From jarchivelib with Apache License 2.0 5 votes vote down vote up
private void extract(ArchiveInputStream input, File destination) throws IOException {
    ArchiveEntry entry;
    while ((entry = input.getNextEntry()) != null) {
        File file = new File(destination, entry.getName());

        if (entry.isDirectory()) {
            file.mkdirs();
        } else {
            file.getParentFile().mkdirs();
            IOUtils.copy(input, file);
        }

        FileModeMapper.map(entry, file);
    }
}
 
Example #25
Source File: CompressUtil.java    From dependency-track with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method that attempts to automatically identify an archive and its type,
 * extract the contents as a byte array. If this fails, it will gracefully return
 * the original input byte array without exception. If the input was not an archive
 * or compressed, it will return the original byte array.
 * @param input the
 * @return a byte array
 */
public static byte[] optionallyDecompress(final byte[] input) {
    try (final ByteArrayInputStream bis = new ByteArrayInputStream(input);
         final ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(bis)) {
        final ArchiveEntry entry = ais.getNextEntry();
        if (ais.canReadEntryData(entry)) {
            return IOUtils.toByteArray(ais);
        }
    } catch (ArchiveException | IOException e) {
        // throw it away and return the original byte array
    }
    return input;
}
 
Example #26
Source File: Archives.java    From wildfly-maven-plugin with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Unzips the zip file to the target directory.
 * <p>
 * Note this is specific to how WildFly is archived. The first directory is assumed to be the base home directory
 * and will returned.
 * </p>
 *
 * @param archiveFile     the archive to uncompress, can be a {@code .zip} or {@code .tar.gz}
 * @param targetDir       the directory to extract the zip file to
 * @param replaceIfExists if {@code true} replace the existing files if they exist
 *
 * @return the path to the extracted directory
 *
 * @throws java.io.IOException if an I/O error occurs
 */
@SuppressWarnings("WeakerAccess")
public static Path uncompress(final Path archiveFile, final Path targetDir, final boolean replaceIfExists) throws IOException {
    final Path archive = getArchive(archiveFile);

    Path firstDir = null;

    try (ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(new BufferedInputStream(Files.newInputStream(archive)))) {
        ArchiveEntry entry;
        while ((entry = in.getNextEntry()) != null) {
            final Path extractTarget = targetDir.resolve(entry.getName());
            if (!replaceIfExists && Files.exists(extractTarget)) {
                if (entry.isDirectory() && firstDir == null) {
                    firstDir = extractTarget;
                }
                continue;
            }
            if (entry.isDirectory()) {
                final Path dir = Files.createDirectories(extractTarget);
                if (firstDir == null) {
                    firstDir = dir;
                }
            } else {
                Files.createDirectories(extractTarget.getParent());
                Files.copy(in, extractTarget);
            }
        }
        return firstDir == null ? targetDir : firstDir;
    } catch (ArchiveException e) {
        throw new IOException(e);
    }
}
 
Example #27
Source File: CompressedFileReference.java    From vespa with Apache License 2.0 5 votes vote down vote up
private static void decompress(ArchiveInputStream archiveInputStream, File outputFile) throws IOException {
    int entries = 0;
    ArchiveEntry entry;
    while ((entry = archiveInputStream.getNextEntry()) != null) {
        File outFile = new File(outputFile, entry.getName());
        if (entry.isDirectory()) {
            if (!(outFile.exists() && outFile.isDirectory())) {
                log.log(Level.FINE, () -> "Creating dir: " + outFile.getAbsolutePath());
                if (!outFile.mkdirs()) {
                    log.log(Level.WARNING, "Could not create dir " + entry.getName());
                }
            }
        } else {
            // Create parent dir if necessary
            File parent = new File(outFile.getParent());
            if (!parent.exists() && !parent.mkdirs()) {
                log.log(Level.WARNING, "Could not create dir " + parent.getAbsolutePath());
            }
            FileOutputStream fos = new FileOutputStream(outFile);
            ByteStreams.copy(archiveInputStream, fos);
            fos.close();
        }
        entries++;
    }
    if (entries == 0) {
        throw new IllegalArgumentException("Not able to read any entries from stream (" +
                                                   archiveInputStream.getBytesRead() + " bytes read from stream)");
    }
}
 
Example #28
Source File: NpmPackageParser.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Performs the actual parsing of the package.json file if it exists.
 */
private Map<String, Object> parsePackageJsonInternal(final ArchiveInputStream archiveInputStream)
    throws IOException
{
  ArchiveEntry entry = archiveInputStream.getNextEntry();
  while (entry != null) {
    if (isPackageJson(entry)) {
      return NpmJsonUtils.parse(() -> archiveInputStream).backing();
    }
    entry = archiveInputStream.getNextEntry();
  }
  return emptyMap();
}
 
Example #29
Source File: PyPiInfoUtils.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Extracts metadata from an archive directly (such as for tar and zip formats).
 */
private static Map<String, String> extractMetadataFromArchive(final String archiveType,
                                                              final InputStream is)
    throws Exception
{
  checkNotNull(archiveType);
  checkNotNull(is);

  final ArchiveStreamFactory archiveFactory = new ArchiveStreamFactory();
  try (ArchiveInputStream ais = archiveFactory.createArchiveInputStream(archiveType, is)) {
    return processArchiveEntries(ais);
  }
}
 
Example #30
Source File: CompressedApplicationInputStream.java    From vespa with Apache License 2.0 5 votes vote down vote up
/**
 * Create an instance of a compressed application from an input stream.
 *
 * @param is   the input stream containing the compressed files.
 * @param contentType the content type for determining what kind of compressed stream should be used.
 * @return An instance of an unpacked application.
 */
public static CompressedApplicationInputStream createFromCompressedStream(InputStream is, String contentType) {
    try {
        ArchiveInputStream ais = getArchiveInputStream(is, contentType);
        return createFromCompressedStream(ais);
    } catch (IOException e) {
        throw new InternalServerException("Unable to create compressed application stream", e);
    }
}