Java Code Examples for org.apache.commons.compress.archivers.ArchiveStreamFactory#createArchiveInputStream()

The following examples show how to use org.apache.commons.compress.archivers.ArchiveStreamFactory#createArchiveInputStream() . 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: 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 2
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 3
Source File: ArchiveResource.java    From embedded-cassandra with Apache License 2.0 5 votes vote down vote up
private static ArchiveInputStreamFactory create(String archiveType, String compressorType) {
	return is -> {
		ArchiveStreamFactory af = new ArchiveStreamFactory();
		CompressorStreamFactory csf = new CompressorStreamFactory();
		return af.createArchiveInputStream(archiveType, csf.createCompressorInputStream(compressorType, is));
	};
}
 
Example 4
Source File: CompressTools.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static Map<String, Object> readEntries(
            File srcFile, ArchiveStreamFactory aFactory,
            BufferedInputStream fileIn, String encoding) {
        try {
            if (aFactory == null || fileIn == null) {
                return null;
            }
            Map<String, Object> unarchive = null;
            String detected = ArchiveStreamFactory.detect(fileIn);
            if (ArchiveStreamFactory.SEVEN_Z.equals(detected)) {
                unarchive = readEntries7z(srcFile);
            } else if (ArchiveStreamFactory.ZIP.equals(detected)) {
                unarchive = readEntriesZip(srcFile);
            } else {
                try ( ArchiveInputStream in = aFactory.createArchiveInputStream(detected, fileIn, encoding)) {
                    List<FileInformation> entires = readEntries(in);
                    String archiver = detected;
                    if (entires != null && !entires.isEmpty()) {
                        unarchive = new HashMap<>();
                        unarchive.put("archiver", archiver);
                        unarchive.put("entries", entires);
                    }
                }
            }
            return unarchive;
        } catch (Exception e) {
//            logger.debug(e.toString());
            return null;
        }
    }
 
Example 5
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 6
Source File: ArchiveResource.java    From embedded-cassandra with Apache License 2.0 4 votes vote down vote up
private static ArchiveInputStreamFactory create(String archiveType) {
	return is -> {
		ArchiveStreamFactory af = new ArchiveStreamFactory();
		return af.createArchiveInputStream(archiveType, is);
	};
}
 
Example 7
Source File: FileUnarchiveController.java    From MyBox with Apache License 2.0 4 votes vote down vote up
protected void unarchive() {
        try {
            if (selected == null || selected.isEmpty()) {
                return;
            }
            ArchiveStreamFactory aFactory = new ArchiveStreamFactory();
            if (archiver == null || !aFactory.getInputStreamArchiveNames().contains(archiver)) {
                return;
            }
            try ( BufferedInputStream fileIn = new BufferedInputStream(new FileInputStream(sourceFile));
                     ArchiveInputStream in = aFactory.createArchiveInputStream(archiver, fileIn, encodeSelector.getValue())) {
                ArchiveEntry entry;
                while ((entry = in.getNextEntry()) != null) {
                    if (!in.canReadEntryData(entry)) {
                        archiveFail++;
//                        logger.debug(message("CanNotReadEntryData" + ":" + entry.getName()));
                        continue;
                    }
                    if (entry.isDirectory() || !selected.contains(entry.getName())) {
                        continue;
                    }
                    File file = makeTargetFile(entry.getName(), targetPath);
                    if (file == null) {
                        continue;
                    }
                    File parent = file.getParentFile();
                    if (!parent.isDirectory() && !parent.mkdirs()) {
                        archiveFail++;
//                        logger.debug(message("FailOpenFile" + ":" + file));
                        continue;
                    }
                    try ( OutputStream o = Files.newOutputStream(file.toPath())) {
                        IOUtils.copy(in, o);
                    }
                    archiveSuccess++;
                }
            }
        } catch (Exception e) {
            archiveFail++;
            error = e.toString();
            if (error.contains("java.nio.charset.MalformedInputException")
                    || error.contains("Illegal char")) {
                charsetIncorrect = true;
            }
        }
    }
 
Example 8
Source File: UnZip.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
public static void unCompress (String zip_file, String output_folder)
      throws IOException, CompressorException, ArchiveException
{
   ArchiveInputStream ais = null;
   ArchiveStreamFactory asf = new ArchiveStreamFactory ();

   FileInputStream fis = new FileInputStream (new File (zip_file));

   if (zip_file.toLowerCase ().endsWith (".tar"))
   {
      ais = asf.createArchiveInputStream (
            ArchiveStreamFactory.TAR, fis);
   }
   else if (zip_file.toLowerCase ().endsWith (".zip"))
   {
      ais = asf.createArchiveInputStream (
            ArchiveStreamFactory.ZIP, fis);
   }
   else if (zip_file.toLowerCase ().endsWith (".tgz") ||
         zip_file.toLowerCase ().endsWith (".tar.gz"))
   {
      CompressorInputStream cis = new CompressorStreamFactory ().
            createCompressorInputStream (CompressorStreamFactory.GZIP, fis);
      ais = asf.createArchiveInputStream (new BufferedInputStream (cis));
   }
   else
   {
      try
      {
         fis.close ();
      }
      catch (IOException e)
      {
         LOGGER.warn ("Cannot close FileInputStream:", e);
      }
      throw new IllegalArgumentException (
            "Format not supported: " + zip_file);
   }

   File output_file = new File (output_folder);
   if (!output_file.exists ()) output_file.mkdirs ();

   // copy the existing entries
   ArchiveEntry nextEntry;
   while ((nextEntry = ais.getNextEntry ()) != null)
   {
      File ftemp = new File (output_folder, nextEntry.getName ());
      if (nextEntry.isDirectory ())
      {
         ftemp.mkdir ();
      }
      else
      {
         FileOutputStream fos = FileUtils.openOutputStream (ftemp);
         IOUtils.copy (ais, fos);
         fos.close ();
      }
   }
   ais.close ();
   fis.close ();
}