org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream Java Examples

The following examples show how to use org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream. 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: TarGzipPacker.java    From twister2 with Apache License 2.0 8 votes vote down vote up
/**
 * given tar.gz file will be copied to this tar.gz file.
 * all files will be transferred to new tar.gz file one by one.
 * original directory structure will be kept intact
 *
 * @param tarGzipFile the archive file to be copied to the new archive
 */
public boolean addTarGzipToArchive(String tarGzipFile) {
  try {
    // construct input stream
    InputStream fin = Files.newInputStream(Paths.get(tarGzipFile));
    BufferedInputStream in = new BufferedInputStream(fin);
    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
    TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzIn);

    // copy the existing entries from source gzip file
    ArchiveEntry nextEntry;
    while ((nextEntry = tarInputStream.getNextEntry()) != null) {
      tarOutputStream.putArchiveEntry(nextEntry);
      IOUtils.copy(tarInputStream, tarOutputStream);
      tarOutputStream.closeArchiveEntry();
    }

    tarInputStream.close();
    return true;
  } catch (IOException ioe) {
    LOG.log(Level.SEVERE, "Archive File can not be added: " + tarGzipFile, ioe);
    return false;
  }
}
 
Example #2
Source File: Utils.java    From tutorials with MIT License 7 votes vote down vote up
/**
 * Extract a "tar.gz" file into a given folder.
 * @param file
 * @param folder 
 */
public static void extractTarArchive(File file, String folder) throws IOException {
    logger.info("Extracting archive {} into folder {}", file.getName(), folder);
    // @formatter:off
    try (FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        GzipCompressorInputStream gzip = new GzipCompressorInputStream(bis); 
        TarArchiveInputStream tar = new TarArchiveInputStream(gzip)) {
   // @formatter:on
        TarArchiveEntry entry;
        while ((entry = (TarArchiveEntry) tar.getNextEntry()) != null) {
            extractEntry(entry, tar, folder);
        }
    }
  logger.info("Archive extracted"); 
}
 
Example #3
Source File: IOUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Extracts the tar.gz file given by {@code tarGzFilePath}.
 * Input {@link Path} MUST be to a gzipped tar file.
 * @param tarGzFilePath {@link Path} to a gzipped tar file for extraction.
 * @param destDir {@link Path} to the directory where the contents of {@code tarGzFilePath} will be extracted.
 * @param overwriteExistingFiles If {@code true}, will enable overwriting of existing files.  If {@code false}, will cause an exception to be thrown if files exist already.
 */
public static void extractTarGz(final Path tarGzFilePath, final Path destDir, final boolean overwriteExistingFiles) {

    logger.info("Extracting data from archive: " + tarGzFilePath.toUri());

    // Create a stream for the data sources input.
    // (We know it will be a tar.gz):
    try ( final InputStream fi = Files.newInputStream(tarGzFilePath);
          final InputStream bi = new BufferedInputStream(fi);
          final InputStream gzi = new GzipCompressorInputStream(bi);
          final TarArchiveInputStream archiveStream = new TarArchiveInputStream(gzi)) {

        extractFilesFromArchiveStream(archiveStream, tarGzFilePath, destDir, overwriteExistingFiles);
    }
    catch (final IOException ex) {
        throw new UserException("Could not extract data from: " + tarGzFilePath.toUri(), ex);
    }
}
 
Example #4
Source File: AbstractInitializrIntegrationTests.java    From initializr with Apache License 2.0 6 votes vote down vote up
private void untar(Path archiveFile, Path project) throws IOException {
	try (TarArchiveInputStream input = new TarArchiveInputStream(
			new GzipCompressorInputStream(Files.newInputStream(archiveFile)))) {
		TarArchiveEntry entry = null;
		while ((entry = input.getNextTarEntry()) != null) {
			Path path = project.resolve(entry.getName());
			if (entry.isDirectory()) {
				Files.createDirectories(path);
			}
			else {
				Files.createDirectories(path.getParent());
				Files.write(path, StreamUtils.copyToByteArray(input));
			}
			applyPermissions(path, getPosixFilePermissions(entry.getMode()));
		}
	}
}
 
Example #5
Source File: DL4JSentimentAnalysisExample.java    From Java-for-Data-Science with MIT License 6 votes vote down vote up
private static void extractTar(String dataIn, String dataOut) throws IOException {

        try (TarArchiveInputStream inStream = new TarArchiveInputStream(
                new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(dataIn))))) {
            TarArchiveEntry tarFile;
            while ((tarFile = (TarArchiveEntry) inStream.getNextEntry()) != null) {
                if (tarFile.isDirectory()) {
                    new File(dataOut + tarFile.getName()).mkdirs();
                } else {
                    int count;
                    byte data[] = new byte[BUFFER_SIZE];

                    FileOutputStream fileInStream = new FileOutputStream(dataOut + tarFile.getName());
                    BufferedOutputStream outStream=  new BufferedOutputStream(fileInStream, BUFFER_SIZE);
                    while ((count = inStream.read(data, 0, BUFFER_SIZE)) != -1) {
                        outStream.write(data, 0, count);
                    }
                }
            }
        }
    }
 
Example #6
Source File: TextFileReader.java    From kafka-connect-fs with Apache License 2.0 6 votes vote down vote up
private Reader getFileReader(InputStream inputStream) throws IOException {
    final InputStreamReader isr;
    switch (this.compression) {
        case BZIP2:
            isr = new InputStreamReader(new BZip2CompressorInputStream(inputStream,
                    this.compression.isConcatenated()), this.charset);
            break;
        case GZIP:
            isr = new InputStreamReader(new GzipCompressorInputStream(inputStream,
                    this.compression.isConcatenated()), this.charset);
            break;
        default:
            isr = new InputStreamReader(inputStream, this.charset);
            break;
    }
    return isr;
}
 
Example #7
Source File: GZipFileSystem.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void readGzipMetadata(File containerFile, TaskMonitor monitor) throws IOException {
	this.containerSize = containerFile.length();
	try (GzipCompressorInputStream gzcis =
		new GzipCompressorInputStream(new FileInputStream(containerFile))) {
		GzipParameters metaData = gzcis.getMetaData();
		origFilename = metaData.getFilename();
		if (origFilename == null) {
			origFilename = GZIP_PAYLOAD_FILENAME;
		}
		else {
			origFilename = FSUtilities.getSafeFilename(origFilename);
		}
		this.origComment = metaData.getComment();

		// NOTE: the following line does not work in apache-commons-compress 1.8
		// Apache has a bug where the computed date value is truncated to 32 bytes before
		// being saved to its 64 bit field.
		// Bug not present in 1.13 (latest ver as of now)
		this.origDate = metaData.getModificationTime();

		this.payloadKey = "uncompressed " + origFilename;
	}
}
 
Example #8
Source File: FileDownloader.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
@SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION")
public static void gunzipFile(String filePath) throws IOException {
    File file = new File(filePath);
    String outPath = null;
    final byte[] buff = new byte[1024];

    if (!file.getName().endsWith("gz")) {
        throw new IOException("File is not ends with .gz extension");
    } else {
        outPath = file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 3);
    }

    try (FileOutputStream fout = new FileOutputStream(outPath);
         FileInputStream fin = new FileInputStream(file);
         BufferedInputStream bin = new BufferedInputStream(fin);
         GzipCompressorInputStream gzipin = new GzipCompressorInputStream(bin);) {
        int n = 0;
        while (-1 != (n = gzipin.read(buff))) {
            fout.write(buff, 0, n);
        }
        LOG.debug("Extracted file path {}", outPath);
    } catch (IOException e) {
        throw e;
    }
}
 
Example #9
Source File: RemoteEngineCustomizer.java    From component-runtime with Apache License 2.0 6 votes vote down vote up
private Map<String, Properties> extractProperties(final Layer it, final Collection<String> paths) {
    final Map<String, Properties> contents = new HashMap<>();
    try {
        final byte[] bytes = Blobs.writeToByteArray(it.getBlob());
        try (final TarArchiveInputStream tis =
                new TarArchiveInputStream(new GzipCompressorInputStream(new ByteArrayInputStream(bytes)))) {
            TarArchiveEntry entry;
            while ((entry = tis.getNextTarEntry()) != null) {
                if (entry.isFile() && paths.contains(entry.getName())) {
                    final Properties properties = new Properties();
                    properties.load(tis);
                    contents.put((!entry.getName().startsWith("/") ? "/" : "") + entry.getName(), properties);
                    if (paths.size() == contents.size()) {
                        return contents;
                    }
                }
            }
        }
    } catch (final IOException ioe) {
        throw new IllegalStateException(ioe);
    }
    return contents;
}
 
Example #10
Source File: JavaLanguage.java    From MSPaintIDE with MIT License 6 votes vote down vote up
@Override
public boolean installLSP() {
    return lspInstallHelper("Would you like to proceed with downloading the Java Language Server by the Eclipse Foundation? This will take up about 94MB.", "https://github.com/eclipse/eclipse.jdt.ls", () -> {
        LOGGER.info("Installing Java LSP server...");
        lspPath.mkdirs();

        var tarGz = new File(lspPath, "jdt-language-server-latest.tar.gz");
        FileUtils.copyURLToFile(new URL("http://download.eclipse.org/jdtls/snapshots/jdt-language-server-latest.tar.gz"), tarGz);

        var destDir = new File(lspPath, "jdt-language-server-latest");

        try (var in = new TarArchiveInputStream(
                new GzipCompressorInputStream(
                        new BufferedInputStream(
                                new FileInputStream(tarGz))))) {
            ExtractUtils.untar(in, destDir);
        }

        tarGz.delete();

        LOGGER.info("Successfully downloaded the Java Language Server");
        return true;
    });
}
 
Example #11
Source File: WorkspaceTest.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static void assertEntry(File file, String name) {
    boolean exist = false;

    try (TarArchiveInputStream tar = new TarArchiveInputStream(
            new GzipCompressorInputStream(
                            new FileInputStream(file)))) {


        ArchiveEntry entry;
        while ((entry = tar.getNextEntry()) != null) {
            if (entry.getName().equals(name)) {
                exist = true;
                break;
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }

    if (!exist) {
        fail("zip entry " + name + " not exist!");
    }

}
 
Example #12
Source File: GzipExtractor.java    From vertx-deploy-tools with Apache License 2.0 6 votes vote down vote up
public void deflateGz(Path input) {
    File tempFile;
    tempFile = new File(getDeflatedFile(input).toUri());

    try (
            InputStream fin = Files.newInputStream(input);
            BufferedInputStream in = new BufferedInputStream(fin);
            GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
            FileOutputStream out = new FileOutputStream(tempFile)) {

        final byte[] buffer = new byte[4096];
        int n;
        while (-1 != (n = gzIn.read(buffer))) {
            out.write(buffer, 0, n);
        }

    } catch (IOException e) {
        LOG.warn("[{} - {}]: Error extracting gzip  {}.", request.getLogName(), request.getId(), e.getMessage());

        throw new IllegalStateException(e);
    }
}
 
Example #13
Source File: LifecycleChaincodePackage.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
public JsonObject getMetaInfJson() throws IOException {

        try (TarArchiveInputStream tarInput = new TarArchiveInputStream(new GzipCompressorInputStream(new ByteArrayInputStream(pBytes)))) {

            TarArchiveEntry currentEntry = tarInput.getNextTarEntry();
            while (currentEntry != null) {
                if (currentEntry.getName().equals("metadata.json")) {
                    byte[] buf = new byte[(int) currentEntry.getSize()];
                    tarInput.read(buf, 0, (int) currentEntry.getSize());

                    try (InputStream stream = new ByteArrayInputStream(buf)) {
                        try (JsonReader reader = Json.createReader(stream)) {

                            return (JsonObject) reader.read();
                        }
                    }

                }
                currentEntry = tarInput.getNextTarEntry();

            }
        }

        return null;
    }
 
Example #14
Source File: StashSplitIterator.java    From emodb with Apache License 2.0 6 votes vote down vote up
StashSplitIterator(AmazonS3 s3, String bucket, String key) {
    InputStream rawIn = new RestartingS3InputStream(s3, bucket, key);
    try {
        // File is gzipped
        // Note:
        //   Because the content may be concatenated gzip files we cannot use the default GZIPInputStream.
        //   GzipCompressorInputStream supports concatenated gzip files.
        GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(rawIn, true);
        _in = new BufferedReader(new InputStreamReader(gzipIn, Charsets.UTF_8));
        // Create a line reader
        _reader = new LineReader(_in);
    } catch (Exception e) {
        try {
            Closeables.close(rawIn, true);
        } catch (IOException ignore) {
            // Won't happen, already caught and logged
        }
        throw Throwables.propagate(e);
    }
}
 
Example #15
Source File: DatabaseMigrationAcceptanceTest.java    From besu with Apache License 2.0 6 votes vote down vote up
private static void extract(final Path path, final String destDirectory) throws IOException {
  try (TarArchiveInputStream fin =
      new TarArchiveInputStream(
          new GzipCompressorInputStream(new FileInputStream(path.toAbsolutePath().toString())))) {
    TarArchiveEntry entry;
    while ((entry = fin.getNextTarEntry()) != null) {
      if (entry.isDirectory()) {
        continue;
      }
      final File curfile = new File(destDirectory, entry.getName());
      final File parent = curfile.getParentFile();
      if (!parent.exists()) {
        parent.mkdirs();
      }
      IOUtils.copy(fin, new FileOutputStream(curfile));
    }
  }
}
 
Example #16
Source File: CompressedDirectoryTest.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testFileWithEmptyDirectory() throws Exception {
  Path tempDir = Files.createTempDirectory("dockerDirectoryEmptySubdirectory");
  tempDir.toFile().deleteOnExit();
  assertThat(new File(tempDir.toFile(), "emptySubDir").mkdir(), is(true));

  try (CompressedDirectory dir = CompressedDirectory.create(tempDir);
       BufferedInputStream fileIn = new BufferedInputStream(Files.newInputStream(dir.file()));
       GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(fileIn);
       TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {

    final List<String> names = new ArrayList<>();
    TarArchiveEntry entry;
    while ((entry = tarIn.getNextTarEntry()) != null) {
      final String name = entry.getName();
      names.add(name);
    }
    assertThat(names, contains("emptySubDir/"));
  }
}
 
Example #17
Source File: CompressedDirectoryTest.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testFileWithIgnore() throws Exception {
  // note: Paths.get(someURL.toUri()) is the platform-neutral way to convert a URL to a Path
  final URL dockerDirectory = Resources.getResource("dockerDirectoryWithIgnore");
  try (CompressedDirectory dir = CompressedDirectory.create(Paths.get(dockerDirectory.toURI()));
       BufferedInputStream fileIn = new BufferedInputStream(Files.newInputStream(dir.file()));
       GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(fileIn);
       TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {

    final List<String> names = new ArrayList<>();
    TarArchiveEntry entry;
    while ((entry = tarIn.getNextTarEntry()) != null) {
      final String name = entry.getName();
      names.add(name);
    }
    assertThat(names, containsInAnyOrder("Dockerfile", "bin/", "bin/date.sh", "subdir2/",
                                         "subdir2/keep.me", "subdir2/do-not.ignore",
                                         "subdir3/do.keep", ".dockerignore"));
  }
}
 
Example #18
Source File: CompressedDirectoryTest.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testFile() throws Exception {
  // note: Paths.get(someURL.toUri()) is the platform-neutral way to convert a URL to a Path
  final URL dockerDirectory = Resources.getResource("dockerDirectory");
  try (CompressedDirectory dir = CompressedDirectory.create(Paths.get(dockerDirectory.toURI()));
       BufferedInputStream fileIn = new BufferedInputStream(Files.newInputStream(dir.file()));
       GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(fileIn);
       TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {

    final List<String> names = new ArrayList<>();
    TarArchiveEntry entry;
    while ((entry = tarIn.getNextTarEntry()) != null) {
      final String name = entry.getName();
      names.add(name);
    }
    assertThat(names,
               containsInAnyOrder("Dockerfile", "bin/", "bin/date.sh",
                                  "innerDir/", "innerDir/innerDockerfile"));
  }
}
 
Example #19
Source File: TarUtils.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
public static void decompress(String in, File out) throws IOException {
  FileInputStream fileInputStream = new FileInputStream(in);
  GzipCompressorInputStream gzipInputStream = new GzipCompressorInputStream(fileInputStream);

  try (TarArchiveInputStream fin = new TarArchiveInputStream(gzipInputStream)){
    TarArchiveEntry entry;
    while ((entry = fin.getNextTarEntry()) != null) {
      if (entry.isDirectory()) {
        continue;
      }
      File curfile = new File(out, entry.getName());
      File parent = curfile.getParentFile();
      if (!parent.exists()) {
        parent.mkdirs();
      }
      IOUtils.copy(fin, new FileOutputStream(curfile));
    }
  }
}
 
Example #20
Source File: HeliumBundleFactory.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
private static List<String> unTgz(File tarFile, File directory) throws IOException {
  List<String> result = new ArrayList<>();
  try (TarArchiveInputStream in = new TarArchiveInputStream(
          new GzipCompressorInputStream(new FileInputStream(tarFile)))) {
    TarArchiveEntry entry = in.getNextTarEntry();
    while (entry != null) {
      if (entry.isDirectory()) {
        entry = in.getNextTarEntry();
        continue;
      }
      File curfile = new File(directory, entry.getName());
      File parent = curfile.getParentFile();
      if (!parent.exists()) {
        parent.mkdirs();
      }
      try (OutputStream out = new FileOutputStream(curfile)) {
        IOUtils.copy(in, out);
      }
      result.add(entry.getName());
      entry = in.getNextTarEntry();
    }
  }
  return result;
}
 
Example #21
Source File: GeoIPServiceImpl.java    From entrada with GNU General Public License v3.0 6 votes vote down vote up
private void extractDatabase(InputStream in, String database) throws IOException {
  GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(in);
  try (TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {
    TarArchiveEntry entry;

    while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
      if (StringUtils.endsWith(entry.getName(), database)) {
        int count;
        byte[] data = new byte[4096];

        String outFile = Paths.get(entry.getName()).getFileName().toString();
        FileOutputStream fos =
            new FileOutputStream(FileUtil.appendPath(location, outFile), false);
        try (BufferedOutputStream dest = new BufferedOutputStream(fos, 4096)) {
          while ((count = tarIn.read(data, 0, 4096)) != -1) {
            dest.write(data, 0, count);
          }
        }
      }
    }
  }
}
 
Example #22
Source File: Extractor.java    From incubator-heron with Apache License 2.0 6 votes vote down vote up
static void extract(InputStream in, Path destination) throws IOException {
  try (
      final BufferedInputStream bufferedInputStream = new BufferedInputStream(in);
      final GzipCompressorInputStream gzipInputStream =
          new GzipCompressorInputStream(bufferedInputStream);
      final TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzipInputStream)
  ) {
    final String destinationAbsolutePath = destination.toFile().getAbsolutePath();

    TarArchiveEntry entry;
    while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) {
      if (entry.isDirectory()) {
        File f = Paths.get(destinationAbsolutePath, entry.getName()).toFile();
        f.mkdirs();
      } else {
        Path fileDestinationPath = Paths.get(destinationAbsolutePath, entry.getName());

        Files.copy(tarInputStream, fileDestinationPath, StandardCopyOption.REPLACE_EXISTING);
      }
    }
  }
}
 
Example #23
Source File: IOHelper.java    From spring-boot-cookbook with Apache License 2.0 6 votes vote down vote up
public static void printTarGzFile(File tarFile) throws IOException {
    BufferedInputStream bin = new BufferedInputStream(FileUtils.openInputStream(tarFile));
    CompressorInputStream cis = new GzipCompressorInputStream(bin);

    try (TarArchiveInputStream tais = new TarArchiveInputStream(cis)) {
        TarArchiveEntry entry;
        while ((entry = tais.getNextTarEntry()) != null) {
            if (entry.isDirectory()) {
                LOGGER.warn("dir:{}", entry.getName());
            } else {
                int size = (int) entry.getSize();
                byte[] content = new byte[size];
                int readCount = tais.read(content, 0, size);
                LOGGER.info("fileName:{}", entry.getName());
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content, 0, readCount);
                try (LineIterator iterator = IOUtils.lineIterator(byteArrayInputStream, Charset.forName("utf-8"))) {
                    while (iterator.hasNext()) {
                        LOGGER.info("line:{}", iterator.nextLine());
                    }
                }
            }
        }
        LOGGER.info("===============finish===============");
    }
}
 
Example #24
Source File: LifecycleChaincodePackage.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
public byte[] getChaincodePayloadBytes() throws IOException {

        try (TarArchiveInputStream tarInput = new TarArchiveInputStream(new GzipCompressorInputStream(new ByteArrayInputStream(pBytes)))) {

            TarArchiveEntry currentEntry = tarInput.getNextTarEntry();
            while (currentEntry != null) {
                if (!currentEntry.getName().equals("metadata.json")) { // right now anything but this
                    byte[] buf = new byte[(int) currentEntry.getSize()];
                    tarInput.read(buf, 0, (int) currentEntry.getSize());

                    return buf;

                }
                currentEntry = tarInput.getNextTarEntry();
            }
        }

        return null;
    }
 
Example #25
Source File: FileExtractorImpl.java    From webdriverextensions-maven-plugin with Apache License 2.0 5 votes vote down vote up
private void extractGzFile(Path file, Path toDirectory) throws IOException {
    String extractedFilename = FilenameUtils.getBaseName(file.toString());
    Path fileToExtract = toDirectory.resolve(extractedFilename);
    try (FileInputStream fin = new FileInputStream(file.toFile())) {
        try (BufferedInputStream bin = new BufferedInputStream(fin)) {
            try (GzipCompressorInputStream gzipArchive = new GzipCompressorInputStream(bin)) {
                Files.copy(gzipArchive, fileToExtract);
            }
        }
    }
}
 
Example #26
Source File: SinFile.java    From Flashtool with GNU General Public License v3.0 5 votes vote down vote up
public TarArchiveInputStream getTarInputStream() throws FileNotFoundException, IOException {
	if (sinv4.isGZipped())
		return new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(sinfile)));
	else
		return new TarArchiveInputStream(new FileInputStream(sinfile));

}
 
Example #27
Source File: InitAndArchiveIT.java    From digdag with Apache License 2.0 5 votes vote down vote up
private void forEachArchiveEntry(IoBiConsumer<TarArchiveEntry, InputStream> consumer)
        throws IOException
{
    try (TarArchiveInputStream s = new TarArchiveInputStream(
            new GzipCompressorInputStream(Files.newInputStream(archive)))) {
        while (true) {
            TarArchiveEntry entry = s.getNextTarEntry();
            if (entry == null) {
                break;
            }
            consumer.accept(entry, s);
        }
    }
}
 
Example #28
Source File: DefaultArchiveExtractor.java    From flow with Apache License 2.0 5 votes vote down vote up
private void extractGzipTarArchive(File archive, File destinationDirectory)
        throws IOException {
    // TarArchiveInputStream can be constructed with a normal FileInputStream if
    // we ever need to extract regular '.tar' files.

    try (FileInputStream fis = new FileInputStream(archive);
         GzipCompressorInputStream gis = new GzipCompressorInputStream(fis);
         TarArchiveInputStream tarIn = new TarArchiveInputStream(gis)) {

        TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
        String canonicalDestinationDirectory = destinationDirectory
                .getCanonicalPath();
        while (tarEntry != null) {
            // Create a file for this tarEntry
            final File destPath = new File(
                    destinationDirectory + File.separator + tarEntry
                            .getName());
            prepDestination(destPath, tarEntry.isDirectory());

            if (!startsWithPath(destPath.getCanonicalPath(),
                    canonicalDestinationDirectory)) {
                throw new IOException("Expanding " + tarEntry.getName()
                        + " would create file outside of "
                        + canonicalDestinationDirectory);
            }

            copyTarFileContents(tarIn, tarEntry, destPath);
            tarEntry = tarIn.getNextTarEntry();
        }
    }
}
 
Example #29
Source File: GZipFileSystem.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private FileCacheEntry getPayloadFileCacheEntry(TaskMonitor monitor)
		throws CancelledException, IOException {
	UnknownProgressWrappingTaskMonitor upwtm =
		new UnknownProgressWrappingTaskMonitor(monitor, containerSize);
	FileCacheEntry derivedFile = fsService.getDerivedFile(containerFSRL, payloadKey,
		(srcFile) -> new GzipCompressorInputStream(new FileInputStream(srcFile)), upwtm);
	return derivedFile;
}
 
Example #30
Source File: SchemaStore.java    From gcp-ingestion with Mozilla Public License 2.0 5 votes vote down vote up
private void loadAllSchemas() throws IOException {
  final Map<String, T> tempSchemas = new HashMap<>();
  final Set<String> tempDirs = new HashSet<>();
  InputStream inputStream;
  try {
    inputStream = open.apply(schemasLocation);
  } catch (IOException e) {
    throw new IOException("Exception thrown while fetching from configured schemasLocation", e);
  }
  try (InputStream bi = new BufferedInputStream(inputStream);
      InputStream gzi = new GzipCompressorInputStream(bi);
      ArchiveInputStream i = new TarArchiveInputStream(gzi);) {
    ArchiveEntry entry;
    while ((entry = i.getNextEntry()) != null) {
      if (!i.canReadEntryData(entry)) {
        LOG.warn("Unable to read tar file entry: " + entry.getName());
        continue;
      }
      String[] components = entry.getName().split("/");
      if (components.length > 2 && "schemas".equals(components[1])) {
        String name = getAndCacheNormalizedPath(
            Arrays.copyOfRange(components, 2, components.length));
        if (entry.isDirectory()) {
          tempDirs.add(name);
          continue;
        }
        if (name.endsWith(schemaSuffix())) {
          tempSchemas.put(name, loadSchemaFromArchive(i));
        }
      }
    }
  }
  schemas = tempSchemas;
  dirs = tempDirs;
}