如何在 Java 中访问 .tar 文件中的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11851075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:39:14  来源:igfitidea点击:

How to Access files in a .tar file in Java

javatar

提问by joshualan

Possible Duplicate:
How do I extract a tar file in Java?

可能的重复:
如何在 Java 中提取 tar 文件?

I'm working on an application that needs to access files within a .tar. What is the easiest way to do this? I just started Java around a week ago with a C/C++ background so I'm not sure if I can implement complicated solutions.

我正在开发一个需要访问 .tar 文件的应用程序。什么是最简单的方法来做到这一点?大约一周前,我刚开始学习 Java,有 C/C++ 背景,所以我不确定我是否可以实现复杂的解决方案。

Also, can you treat .tgz files as zip entries?

另外,您可以将 .tgz 文件视为 zip 条目吗?

Thanks!

谢谢!

回答by gbtimmon

Java inclides built in function for handling bot zipped and gziped libraries. http://docs.oracle.com/javase/6/docs/api/java/util/zip/package-summary.html.

Java 包含用于处理 bot 压缩和 gzip 库的内置函数。 http://docs.oracle.com/javase/6/docs/api/java/util/zip/package-summary.html

This can be used turn your .tgzinto a regular .tarwithout much trouble. And no you cannot treat .tgzas regular zips. They are first achived in a tar and then compressed with gzip. Even if you gunzip it, you will still need to unpack the tar archive to get any of the file out of it.

这可以用来把你.tgz变成一个普通的,.tar没有太多麻烦。不,您不能将其.tgz视为普通拉链。它们首先在 tar 中实现,然后用 gzip 压缩。即使您对它进行了 gunzip,您仍然需要解压缩 tar 存档以从中取出任何文件。

Handling tar files is a bit more difficult. This previous question might help : How do I extract a tar file in Java?

处理 tar 文件有点困难。上一个问题可能会有所帮助: 如何在 Java 中提取 tar 文件?

From these solutions I strongly recommend the apache commons solution : http://commons.apache.org/vfs/filesystems.html

从这些解决方案中,我强烈推荐 apache commons 解决方案:http: //commons.apache.org/vfs/filesystems.html

It will allow you to read from your tar as if it were a filesystem without doing any writes to your hard drive. You will have to know what you're looking for before you go in to it, but i doubt that will hinder you much

它将允许您从 tar 中读取,就像它是一个文件系统一样,而无需对硬盘进行任何写入。在你进入它之前你必须知道你在寻找什么,但我怀疑这会阻碍你

回答by Reimeus

Tar file access can be done using TarArchiveEntryin the Apache Commons library.

可以使用Apache Commons 库中的TarArchiveEntry来访问 Tar 文件。