我正在使用 Apache Commons 1.4.1 库来解压缩“.tar”文件。
问题:我不必提取所有文件。我必须从 tar 存档中的特定位置提取特定文件。我只需要提取几个 .xml 文件,因为 TAR 文件的大小约为 300 MB,解压缩整个内容会浪费资源。
我很困惑,不知道我是否必须进行嵌套目录比较,或者有什么办法吗?
注意:.XML(所需文件)的位置始终相同。
TAR 的结构是:
directory:E:\Root\data
file:E:\Root\datasheet.txt
directory:E:\Root\map
file:E:\Root\mapers.txt
directory:E:\Root\ui
file:E:\Root\ui\capital.txt
file:E:\Root\ui\info.txt
directory:E:\Root\ui\sales
file:E:\Root\ui\sales\Reqest_01.xml
file:E:\Root\ui\sales\Reqest_02.xml
file:E:\Root\ui\sales\Reqest_03.xml
file:E:\Root\ui\sales\Reqest_04.xml
directory:E:\Root\ui\sales\stores
directory:E:\Root\ui\stores
directory:E:\Root\urls
directory:E:\Root\urls\fullfilment
file:E:\Root\urls\fullfilment\Cams_01.xml
file:E:\Root\urls\fullfilment\Cams_02.xml
file:E:\Root\urls\fullfilment\Cams_03.xml
file:E:\Root\urls\fullfilment\Cams_04.xml
directory:E:\Root\urls\fullfilment\profile
directory:E:\Root\urls\fullfilment\registration
file:E:\Root\urls\options.txt
directory:E:\Root\urls\profile
约束:我不能使用 JDK 7 并且必须坚持使用 Apache 公共(public)库。
我目前的解决方案:
public static void untar(File[] files) throws Exception {
String path = files[0].toString();
File tarPath = new File(path);
TarEntry entry;
TarInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = new TarInputStream(new FileInputStream(tarPath));
while (null != (entry = inputStream.getNextEntry())) {
int bytesRead;
System.out.println("tarpath:" + tarPath.getName());
System.out.println("Entry:" + entry.getName());
String pathWithoutName = path.substring(0, path.indexOf(tarPath.getName()));
System.out.println("pathname:" + pathWithoutName);
if (entry.isDirectory()) {
File directory = new File(pathWithoutName + entry.getName());
directory.mkdir();
continue;
}
byte[] buffer = new byte[1024];
outputStream = new FileOutputStream(pathWithoutName + entry.getName());
while ((bytesRead = inputStream.read(buffer, 0, 1024)) > -1) {
outputStream.write(buffer, 0, bytesRead);
}
System.out.println("Extracted " + entry.getName());
}
}
最佳答案
TAR文件格式被设计为作为流写入或读取(即,到磁带驱动器/从磁带驱动器读取),并且没有集中的 header 。所以不,没有办法通过读取整个文件来提取单个条目。
如果要随机访问,应该使用ZIP格式,使用JDK的ZipFile
打开。假设您有足够的虚拟内存,该文件将被内存映射,从而使随机访问非常快(我没有查看如果无法内存映射它是否会使用随机访问文件)。
关于java - 如何使用 apache commons 从 TAR 解压缩特定文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14464518/