Java Code Examples for org.apache.commons.compress.archivers.tar.TarArchiveEntry#getName()

The following examples show how to use org.apache.commons.compress.archivers.tar.TarArchiveEntry#getName() . 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: TarUnpackerSequenceFileWriter.java    From localization_nifi with Apache License 2.0 7 votes vote down vote up
@Override
protected void processInputStream(final InputStream stream, final FlowFile tarArchivedFlowFile, final Writer writer) throws IOException {
    try (final TarArchiveInputStream tarIn = new TarArchiveInputStream(new BufferedInputStream(stream))) {
        TarArchiveEntry tarEntry;
        while ((tarEntry = tarIn.getNextTarEntry()) != null) {
            if (tarEntry.isDirectory()) {
                continue;
            }
            final String key = tarEntry.getName();
            final long fileSize = tarEntry.getSize();
            final InputStreamWritable inStreamWritable = new InputStreamWritable(tarIn, (int) fileSize);
            writer.append(new Text(key), inStreamWritable);
            logger.debug("Appending FlowFile {} to Sequence File", new Object[]{key});
        }
    }
}
 
Example 2
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 3
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 4
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 5
Source File: ProjectGeneratorTest.java    From syndesis with Apache License 2.0 6 votes vote down vote up
private void generate(Path destination, Integration integration, ProjectGeneratorConfiguration generatorConfiguration, TestResourceManager resourceManager) throws IOException {
    final IntegrationProjectGenerator generator = new ProjectGenerator(generatorConfiguration, resourceManager,TestConstants.MAVEN_PROPERTIES);

    try (InputStream is = generator.generate(integration, errors::add)) {
        try (TarArchiveInputStream tis = new TarArchiveInputStream(is)) {
            TarArchiveEntry tarEntry = tis.getNextTarEntry();

            // tarIn is a TarArchiveInputStream
            while (tarEntry != null) {
                // create a file with the same name as the tarEntry
                File destPath = new File(destination.toFile(), tarEntry.getName());
                if (tarEntry.isDirectory()) {
                    destPath.mkdirs();
                } else {
                    destPath.getParentFile().mkdirs();
                    destPath.createNewFile();

                    try (BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath))) {
                        IOUtils.copy(tis, bout);
                    }
                }
                tarEntry = tis.getNextTarEntry();
            }
        }
    }
}
 
Example 6
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 7
Source File: ExtractUtils.java    From MSPaintIDE with MIT License 6 votes vote down vote up
public static void untar(TarArchiveInputStream tarIn, File targetDir) throws IOException {
    byte[] b = new byte[4096];
    TarArchiveEntry tarEntry;
    while ((tarEntry = tarIn.getNextTarEntry()) != null) {
        final File file = new File(targetDir, tarEntry.getName());
        if (tarEntry.isDirectory()) {
            if (!file.mkdirs()) {
                throw new IOException("Unable to create folder " + file.getAbsolutePath());
            }
        } else {
            final File parent = file.getParentFile();
            if (!parent.exists()) {
                if (!parent.mkdirs()) {
                    throw new IOException("Unable to create folder " + parent.getAbsolutePath());
                }
            }
            try (FileOutputStream fos = new FileOutputStream(file)) {
                int r;
                while ((r = tarIn.read(b)) != -1) {
                    fos.write(b, 0, r);
                }
            }
        }
    }
}
 
Example 8
Source File: CompressionUtils.java    From waterdrop with Apache License 2.0 5 votes vote down vote up
/** Untar an input file into an output file.

     * The output file is created in the output folder, having the same name
     * as the input file, minus the '.tar' extension.
     *
     * @param inputFile     the input .tar file
     * @param outputDir     the output directory file.
     * @throws IOException
     * @throws FileNotFoundException
     *
     * @return  The {@link List} of {@link File}s with the untared content.
     * @throws ArchiveException
     */
    public static List<File> unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException {

        logger.info(String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));

        final List<File> untaredFiles = new LinkedList<File>();
        final InputStream is = new FileInputStream(inputFile);
        final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
        TarArchiveEntry entry = null;
        while ((entry = (TarArchiveEntry)debInputStream.getNextEntry()) != null) {
            final File outputFile = new File(outputDir, entry.getName());
            if (entry.isDirectory()) {
                logger.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
                if (!outputFile.exists()) {
                    logger.info(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath()));
                    if (!outputFile.mkdirs()) {
                        throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                    }
                }
            } else {
                logger.info(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
                final OutputStream outputFileStream = new FileOutputStream(outputFile);
                IOUtils.copy(debInputStream, outputFileStream);
                outputFileStream.close();
            }
            untaredFiles.add(outputFile);
        }
        debInputStream.close();

        return untaredFiles;
    }
 
Example 9
Source File: Utils.java    From jstorm with Apache License 2.0 5 votes vote down vote up
private static void unpackEntries(TarArchiveInputStream tis,
                                  TarArchiveEntry entry, File outputDir) throws IOException {
    if (entry.isDirectory()) {
        File subDir = new File(outputDir, entry.getName());
        if (!subDir.mkdirs() && !subDir.isDirectory()) {
            throw new IOException("Mkdirs failed to create tar internal dir "
                    + outputDir);
        }
        for (TarArchiveEntry e : entry.getDirectoryEntries()) {
            unpackEntries(tis, e, subDir);
        }
        return;
    }
    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        if (!outputFile.getParentFile().mkdirs()) {
            throw new IOException("Mkdirs failed to create tar internal dir "
                    + outputDir);
        }
    }
    int count;
    byte data[] = new byte[2048];
    BufferedOutputStream outputStream = new BufferedOutputStream(
            new FileOutputStream(outputFile));

    while ((count = tis.read(data)) != -1) {
        outputStream.write(data, 0, count);
    }
    outputStream.flush();
    outputStream.close();
}
 
Example 10
Source File: IOHelper.java    From spring-boot-cookbook with Apache License 2.0 5 votes vote down vote up
public static void uncompressTarGZ(File tarFile, File dest) throws IOException {
        boolean mkdirs = dest.mkdirs();
        if (!mkdirs) {
            LOGGER.warn("Unable to create directory '{}'", dest.getAbsolutePath());
            return;
        }

        BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(tarFile));
        GzipCompressorInputStream gcis = new GzipCompressorInputStream(inputStream);
        try (TarArchiveInputStream tais = new TarArchiveInputStream(gcis)) {
            TarArchiveEntry entry;
            while ((entry = tais.getNextTarEntry()) != null) {// create a file with the same name as the entry
                File desFile = new File(dest, entry.getName());
                if (entry.isDirectory()) {
                    boolean mkDirs = desFile.mkdirs();
                    if (!mkDirs) {
                        LOGGER.warn("Unable to create directory '{}'", desFile.getAbsolutePath());
                    }
                } else {
                    boolean createNewFile = desFile.createNewFile();
                    if (!createNewFile) {
                        LOGGER.warn("Unable to create file '{}'", desFile.getCanonicalPath());
                        continue;
                    }
                    try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desFile));) {
//                        IOUtils.copy(tais, bos);
                        byte[] btoRead = new byte[1024];
                        int len;
                        while ((len = tais.read(btoRead)) != -1) {
                            bos.write(btoRead, 0, len);
                        }
                    }
                }
            }
            LOGGER.info("Untar completed successfully!");
        }
    }
 
Example 11
Source File: FileUtil.java    From big-c with Apache License 2.0 5 votes vote down vote up
private static void unpackEntries(TarArchiveInputStream tis,
    TarArchiveEntry entry, File outputDir) throws IOException {
  if (entry.isDirectory()) {
    File subDir = new File(outputDir, entry.getName());
    if (!subDir.mkdirs() && !subDir.isDirectory()) {
      throw new IOException("Mkdirs failed to create tar internal dir "
          + outputDir);
    }

    for (TarArchiveEntry e : entry.getDirectoryEntries()) {
      unpackEntries(tis, e, subDir);
    }

    return;
  }

  File outputFile = new File(outputDir, entry.getName());
  if (!outputFile.getParentFile().exists()) {
    if (!outputFile.getParentFile().mkdirs()) {
      throw new IOException("Mkdirs failed to create tar internal dir "
          + outputDir);
    }
  }

  int count;
  byte data[] = new byte[2048];
  BufferedOutputStream outputStream = new BufferedOutputStream(
      new FileOutputStream(outputFile));

  while ((count = tis.read(data)) != -1) {
    outputStream.write(data, 0, count);
  }

  outputStream.flush();
  outputStream.close();
}
 
Example 12
Source File: RNNodeService.java    From react-native-node with MIT License 5 votes vote down vote up
private static List<File> unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException {
    Log.i(TAG, String.format("Untaring %s to dir %s", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));

    if (!outputDir.exists()) {
        outputDir.mkdirs();
    }
    final List<File> untaredFiles = new LinkedList<File>();
    final InputStream is = new FileInputStream(inputFile);
    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry)debInputStream.getNextEntry()) != null) {
        final File outputFile = new File(outputDir, entry.getName());
        if (entry.isDirectory()) {
            if (!outputFile.exists()) {
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                }
            }
        } else {
            final OutputStream outputFileStream = new FileOutputStream(outputFile);
            IOUtils.copy(debInputStream, outputFileStream);
            outputFileStream.close();
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close();

    return untaredFiles;
}
 
Example 13
Source File: DockerContainerClient.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private Set<File> download(final ContainerInfo containerInfo, final String fromContainerPath, final File toLocal) {
  String containerId = containerInfo.id();
  String shortId = left(containerId, SHORT_ID_LENGTH);

  ImmutableSet.Builder<File> files = ImmutableSet.builder();

  log.info("Attempting to download from path '{}' in container '{}' for image '{}'",
      fromContainerPath, shortId, image);

  try (final TarArchiveInputStream tarStream = new TarArchiveInputStream(
      dockerClient.archiveContainer(containerId, fromContainerPath))) {

    TarArchiveEntry entry;
    while ((entry = tarStream.getNextTarEntry()) != null) {
      log.info("Downloading entry '{}' in container '{}' for image '{}'", entry.getName(), shortId, image);

      String entryName = entry.getName();
      entryName = entryName.substring(entryName.indexOf('/') + 1);
      if (entryName.isEmpty()) {
        continue;
      }

      File file = (toLocal.exists() && toLocal.isDirectory()) ? new File(toLocal, entryName) : toLocal;
      files.add(file);

      try (OutputStream outStream = new FileOutputStream(file)) {
        copy(tarStream, outStream);
      }
    }
  }
  catch (Exception e) {
    log.error("Failed to download from path '{}' in container '{}' for image '{}'",
        fromContainerPath, shortId, image, e);
  }

  return files.build();
}
 
Example 14
Source File: IOUtils.java    From senti-storm with Apache License 2.0 5 votes vote down vote up
public static void extractTarGz(InputStream inputTarGzStream, String outDir,
    boolean logging) {
  try {
    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(
        inputTarGzStream);
    TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);

    // read Tar entries
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
      if (logging) {
        LOG.info("Extracting: " + outDir + File.separator + entry.getName());
      }
      if (entry.isDirectory()) { // create directory
        File f = new File(outDir + File.separator + entry.getName());
        f.mkdirs();
      } else { // decompress file
        int count;
        byte data[] = new byte[EXTRACT_BUFFER_SIZE];

        FileOutputStream fos = new FileOutputStream(outDir + File.separator
            + entry.getName());
        BufferedOutputStream dest = new BufferedOutputStream(fos,
            EXTRACT_BUFFER_SIZE);
        while ((count = tarIn.read(data, 0, EXTRACT_BUFFER_SIZE)) != -1) {
          dest.write(data, 0, count);
        }
        dest.close();
      }
    }

    // close input stream
    tarIn.close();

  } catch (IOException e) {
    LOG.error("IOException: " + e.getMessage());
  }
}
 
Example 15
Source File: FileUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@CheckForNull
public static String decompressTarGz(File archive, File destination, boolean skipArchiveRoot) throws IOException {
    String archiveRoot = null;
    try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(archive)))) {
        int archiveRootLength = -1;
        TarArchiveEntry tarEntry = tarInputStream.getNextTarEntry();
        if (tarEntry != null) {
            archiveRoot = tarEntry.getName();
            if (skipArchiveRoot) {
                archiveRootLength = archiveRoot.length();
                tarEntry = tarInputStream.getNextTarEntry();
            }
        }
        while (tarEntry != null) {
            String name = tarEntry.getName();
            if (skipArchiveRoot) {
                name = name.substring(archiveRootLength);
            }
            File destPath = new File(destination, name);
            if (tarEntry.isDirectory()) {
                if (!destPath.isDirectory()
                        && !destPath.mkdirs()) {
                    throw new IOException("Cannot create directory " + destPath);
                }
            } else {
                if (!destPath.isFile()
                        && !destPath.createNewFile()) {
                    throw new IOException("Cannot create new file " + destPath);
                }
                try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(destPath))) {
                    FileUtil.copy(tarInputStream, outputStream);
                }
            }
            tarEntry = tarInputStream.getNextTarEntry();
        }
    }
    return archiveRoot;
}
 
Example 16
Source File: OmnisharpStreamConnectionProvider.java    From aCute with Eclipse Public License 2.0 5 votes vote down vote up
/**
 *
 * @return path to server, unzipping it if necessary. Can be null is fragment is missing.
 */
private @Nullable File getServer() throws IOException {
	File serverPath = new File(AcutePlugin.getDefault().getStateLocation().toFile(), "omnisharp-roslyn"); //$NON-NLS-1$
	if (!serverPath.exists()) {
		serverPath.mkdirs();
		try (
			InputStream stream = FileLocator.openStream(AcutePlugin.getDefault().getBundle(), new Path("omnisharp-roslyn.tar"), true); //$NON-NLS-1$
			TarArchiveInputStream tarStream = new TarArchiveInputStream(stream);
		) {
			TarArchiveEntry entry = null;
			while ((entry = tarStream.getNextTarEntry()) != null) {
				if (!entry.isDirectory()) {
					File targetFile = new File(serverPath, entry.getName());
					targetFile.getParentFile().mkdirs();
					InputStream in = new BoundedInputStream(tarStream, entry.getSize()); // mustn't be closed
					try (
						FileOutputStream out = new FileOutputStream(targetFile);
					) {
						IOUtils.copy(in, out);
						if (!Platform.OS_WIN32.equals(Platform.getOS())) {
							int xDigit = entry.getMode() % 10;
							targetFile.setExecutable(xDigit > 0, (xDigit & 1) == 1);
							int wDigit = (entry.getMode() / 10) % 10;
							targetFile.setWritable(wDigit > 0, (wDigit & 1) == 1);
							int rDigit = (entry.getMode() / 100) % 10;
							targetFile.setReadable(rDigit > 0, (rDigit & 1) == 1);
						}
					}
				}
			}
		}
	}
	return serverPath;
}
 
Example 17
Source File: TarGzExtractorProvider.java    From appengine-plugins-core with Apache License 2.0 4 votes vote down vote up
@Override
public void extract(Path archive, Path destination, ProgressListener progressListener)
    throws IOException {

  progressListener.start(
      "Extracting archive: " + archive.getFileName(), ProgressListener.UNKNOWN);

  String canonicalDestination = destination.toFile().getCanonicalPath();

  GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(Files.newInputStream(archive));
  try (TarArchiveInputStream in = new TarArchiveInputStream(gzipIn)) {
    TarArchiveEntry entry;
    while ((entry = in.getNextTarEntry()) != null) {
      Path entryTarget = destination.resolve(entry.getName());

      String canonicalTarget = entryTarget.toFile().getCanonicalPath();
      if (!canonicalTarget.startsWith(canonicalDestination + File.separator)) {
        throw new IOException("Blocked unzipping files outside destination: " + entry.getName());
      }

      progressListener.update(1);
      logger.fine(entryTarget.toString());

      if (entry.isDirectory()) {
        if (!Files.exists(entryTarget)) {
          Files.createDirectories(entryTarget);
        }
      } else if (entry.isFile()) {
        if (!Files.exists(entryTarget.getParent())) {
          Files.createDirectories(entryTarget.getParent());
        }
        try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(entryTarget))) {
          IOUtils.copy(in, out);
          PosixFileAttributeView attributeView =
              Files.getFileAttributeView(entryTarget, PosixFileAttributeView.class);
          if (attributeView != null) {
            attributeView.setPermissions(PosixUtil.getPosixFilePermissions(entry.getMode()));
          }
        }
      } else {
        // we don't know what kind of entry this is (we only process directories and files).
        logger.warning("Skipping entry (unknown type): " + entry.getName());
      }
    }
    progressListener.done();
  }
}
 
Example 18
Source File: ProjectArchives.java    From digdag with Apache License 2.0 4 votes vote down vote up
private static void extractArchive(Path destDir, TarArchiveInputStream archive, ExtractListener listener)
      throws IOException
  {
      String prefix = destDir.toString();
      TarArchiveEntry entry;
      while (true) {
          entry = archive.getNextTarEntry();
          if (entry == null) {
              break;
          }
          Path path = destDir.resolve(entry.getName()).normalize();
          if (!path.toString().startsWith(prefix)) {
              throw new RuntimeException("Archive includes an invalid entry: " + entry.getName());
          }
          if (entry.isDirectory()) {
              Files.createDirectories(path);
          }
          else if (entry.isSymbolicLink()) {
              Files.createDirectories(path.getParent());
              String dest = entry.getLinkName();
              Path destAbsPath = path.getParent().resolve(dest).normalize();
              if (!destAbsPath.normalize().toString().startsWith(prefix)) {
                  throw new RuntimeException("Archive includes an invalid symlink: " + entry.getName() + " -> " + dest);
              }
              if (listener != null) {
                  listener.symlink(destDir.relativize(path), dest);
              }
              Files.createSymbolicLink(path, Paths.get(dest));
          }
          else {
              Files.createDirectories(path.getParent());
              if (listener != null) {
                  listener.file(destDir.relativize(path));
              }
              try (OutputStream out = Files.newOutputStream(path)) {
                  ByteStreams.copy(archive, out);
              }
          }
          if (!Files.isSymbolicLink(path) && isPosixCompliant()) {
// Files.setPosixFilePermissions doesn't work on Windows: java.lang.UnsupportedOperationException
              Files.setPosixFilePermissions(path, getPosixFilePermissions(entry));
          }
      }
  }
 
Example 19
Source File: ArchiveUtils.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
 * Extracts files to the specified destination
 *
 * @param file the file to extract to
 * @param dest the destination directory
 * @throws IOException
 */
public static void unzipFileTo(String file, String dest) throws IOException {
    File target = new File(file);
    if (!target.exists())
        throw new IllegalArgumentException("Archive doesnt exist");
    FileInputStream fin = new FileInputStream(target);
    int BUFFER = 2048;
    byte data[] = new byte[BUFFER];

    if (file.endsWith(".zip") || file.endsWith(".jar")) {
        try(ZipInputStream zis = new ZipInputStream(fin)) {
            //get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();

            while (ze != null) {
                String fileName = ze.getName();
                File newFile = new File(dest + File.separator + fileName);

                if (ze.isDirectory()) {
                    newFile.mkdirs();
                    zis.closeEntry();
                    ze = zis.getNextEntry();
                    continue;
                }

                FileOutputStream fos = new FileOutputStream(newFile);

                int len;
                while ((len = zis.read(data)) > 0) {
                    fos.write(data, 0, len);
                }

                fos.close();
                ze = zis.getNextEntry();
                log.debug("File extracted: " + newFile.getAbsoluteFile());
            }

            zis.closeEntry();
        }
    } else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) {

        BufferedInputStream in = new BufferedInputStream(fin);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);

        TarArchiveEntry entry;
        /* Read the tar entries using the getNextEntry method **/
        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
            log.info("Extracting: " + entry.getName());
            /* If the entry is a directory, create the directory. */

            if (entry.isDirectory()) {
                File f = new File(dest + File.separator + entry.getName());
                f.mkdirs();
            }
            /*
             * If the entry is a file,write the decompressed file to the disk
             * and close destination stream.
             */
            else {
                int count;
                try(FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName());
                    BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER);) {
                    while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                        destStream.write(data, 0, count);
                    }

                    destStream.flush();
                    IOUtils.closeQuietly(destStream);
                }
            }
        }

        // Close the input stream
        tarIn.close();
    } else if (file.endsWith(".gz")) {
        File extracted = new File(target.getParent(), target.getName().replace(".gz", ""));
        if (extracted.exists())
            extracted.delete();
        extracted.createNewFile();
        try(GZIPInputStream is2 = new GZIPInputStream(fin); OutputStream fos = FileUtils.openOutputStream(extracted)) {
            IOUtils.copyLarge(is2, fos);
            fos.flush();
        }
    } else {
        throw new IllegalStateException("Unable to infer file type (compression format) from source file name: " +
                file);
    }
    target.delete();
}
 
Example 20
Source File: TarGzs.java    From tomee with Apache License 2.0 4 votes vote down vote up
public static void untargz(final InputStream read, final File destination, final boolean noparent, final FileFilter fileFilter) throws IOException {
        Objects.requireNonNull(fileFilter, "'fileFilter' is required.");

        Files.dir(destination);
        Files.writable(destination);

        try {
            GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(read);
            TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn);

            TarArchiveEntry entry;
            while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
                String path = entry.getName();
                if (noparent) {
                    path = path.replaceFirst("^[^/]+/", "");
                }

                File file = new File(destination, path);
                if (!fileFilter.accept(file)) continue;

                if (entry.isDirectory()) {
                    Files.mkdir(file);
                } else {
                    Files.mkdir(file.getParentFile());
                    IO.copy(tarIn, file);
                    long lastModified = entry.getLastModifiedDate().getTime();
                    if (lastModified > 0L) {
                        file.setLastModified(lastModified);
                    }

//                    if (entry.getMode() != 420) System.out.printf("%s  %s%n", entry.getMode(), entry.getName());
                    // DMB: I have no idea how to respect the mod.
                    // Elasticsearch tar has entries with 33261 that are executable
                    if (33261 == entry.getMode()) {
                        file.setExecutable(true);
                    }
                    // DMB: I have no idea how to respect the mod.
                    // Kibana tar has entries with 493 that are executable
                    if (493 == entry.getMode()) {
                        file.setExecutable(true);
                    }
                }
            }

            tarIn.close();
        } catch (IOException var9) {
            throw new IOException("Unable to unzip " + read, var9);
        }
    }