我正在使用 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 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());
}
}