如何在 Java 中提取 tar 文件?

新手上路,请多包涵

如何用 Java 提取 tar(或 tar.gz 或 tar.bz2)文件?

原文由 skiphoppy 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 845
2 个回答

注意: 此功能后来通过一个单独的项目 Apache Commons Compress 发布,如 另一个答案中所述。 这个答案已经过时了。


我没有直接使用 tar API,但 tar 和 bzip2 是在 Ant 中实现的;你可以借用他们的实现,或者可能使用 Ant 来做你需要的。

Gzip 是 Java SE 的一部分(我猜 Ant 实现遵循相同的模型)。

GZIPInputStream 只是一个 InputStream 装饰器。例如,您可以将 FileInputStream 包装在 GZIPInputStream 中,并以与使用任何 InputStream 相同的方式使用它

InputStream is = new GZIPInputStream(new FileInputStream(file));

(请注意,GZIPInputStream 有自己的内部缓冲区,因此将 — 包装在 FileInputStream BufferedInputStream 可能会降低性能。)

原文由 erickson 发布,翻译遵循 CC BY-SA 3.0 许可协议

您可以使用 Apache Commons Compress 库执行此操作。您可以从 http://mvnrepository.com/artifact/org.apache.commons/commons-compress/1.2 下载 1.2 版本。

这里有两种方法:一种解压缩文件,另一种解压缩文件。因此,对于文件 tar.gz,您需要先将其解压缩,然后再解压缩。请注意,tar 存档也可能包含文件夹,在这种情况下,它们需要在本地文件系统上创建。

享受。

 /** 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
 */
private static List<File> unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException {

    LOG.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()) {
            LOG.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
            if (!outputFile.exists()) {
                LOG.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 {
            LOG.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;
}

/**
 * Ungzip an input file into an output file.
 * <p>
 * The output file is created in the output folder, having the same name
 * as the input file, minus the '.gz' extension.
 *
 * @param inputFile     the input .gz file
 * @param outputDir     the output directory file.
 * @throws IOException
 * @throws FileNotFoundException
 *
 * @return  The {@File} with the ungzipped content.
 */
private static File unGzip(final File inputFile, final File outputDir) throws FileNotFoundException, IOException {

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

    final File outputFile = new File(outputDir, inputFile.getName().substring(0, inputFile.getName().length() - 3));

    final GZIPInputStream in = new GZIPInputStream(new FileInputStream(inputFile));
    final FileOutputStream out = new FileOutputStream(outputFile);

    IOUtils.copy(in, out);

    in.close();
    out.close();

    return outputFile;
}

原文由 Dan Borza 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题