public static List<File> unTar(final String inputFileName, final String outputDirName,
      boolean flatten) throws FileNotFoundException, IOException, ArchiveException {

    File inputFile = new File(inputFileName);
    File outputDir = new File(outputDirName);

    final List<File> untarredFiles = new LinkedList<File>();
    InputStream is = null;

    try {
      if (inputFileName.endsWith(".gz")) {
        is = new GzipCompressorInputStream(new FileInputStream(inputFile));
      } else {
        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 (!outputFile.toPath().toAbsolutePath().normalize()
                .startsWith(outputDir.toPath().toAbsolutePath().normalize())) {
          throw new IOException("Untarred file is not under the output directory");
        }
        if (entry.isDirectory()) {
          if (flatten) {
            // no sub-directories
            continue;
          }
          LOG.debug("Attempting to write output directory {}.", outputFile.getAbsolutePath());
          if (!outputFile.exists()) {
            LOG.debug("Attempting to create output directory {}.", outputFile.getAbsolutePath());
            if (!outputFile.mkdirs()) {
              throw new IllegalStateException("Couldn't create directory %s.".formatted(
                  outputFile.getAbsolutePath()));
            }
          }
        } else {
          final OutputStream outputFileStream;
          if (flatten) {
            File flatOutputFile = new File(outputDir, outputFile.getName());
            LOG.debug("Creating flat output file {}.", flatOutputFile.getAbsolutePath());
            outputFileStream = new FileOutputStream(flatOutputFile);
          } else if (!outputFile.getParentFile().exists()) {
            LOG.debug("Attempting to create output directory {}.", outputFile.getParentFile().getAbsoluteFile());
            if (!outputFile.getParentFile().getAbsoluteFile().mkdirs()) {
              throw new IllegalStateException("Couldn't create directory %s.".formatted(
                  outputFile.getParentFile().getAbsolutePath()));
            }
            LOG.debug("Creating output file {}.", outputFile.getAbsolutePath());
            outputFileStream = new FileOutputStream(outputFile);
          } else {
            outputFileStream = new FileOutputStream(outputFile);
          }
          IOUtils.copy(debInputStream, outputFileStream);
          outputFileStream.close();
        }
        untarredFiles.add(outputFile);
      }
      debInputStream.close();
      return untarredFiles;

    } finally {
      if (is != null)  is.close();
    }
  }
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 inputFileName the input .tar file
@param outputDirName the output directory file.
@throws IOException
@throws FileNotFoundException

@return The {@link List} of {@link File}s with the untarred content.
@throws ArchiveException
common/src/java/org/apache/hadoop/hive/common/CompressionUtils.java