Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用apachecommons从TAR解压缩特定文件?_Java_File_Tar_Apache Commons - Fatal编程技术网

Java 如何使用apachecommons从TAR解压缩特定文件?

Java 如何使用apachecommons从TAR解压缩特定文件?,java,file,tar,apache-commons,Java,File,Tar,Apache Commons,我正在使用ApacheCommons1.4.1库来解压缩“.tar”文件 问题:我不必提取所有文件。我必须从tar存档中的特定位置提取特定文件。我只需要提取几个.xml文件,因为TAR文件的大小约为300mb&解压缩整个内容是浪费资源 我被卡住了&不知道我是必须进行嵌套目录比较,还是有其他办法 注意:.XML(必需文件)的位置始终相同 焦油的结构是: directory:E:\Root\data file:E:\Root\datasheet.txt directory:E:\Root\map

我正在使用ApacheCommons1.4.1库来解压缩“.tar”文件

问题:我不必提取所有文件。我必须从tar存档中的特定位置提取特定文件。我只需要提取几个.xml文件,因为TAR文件的大小约为300mb&解压缩整个内容是浪费资源

我被卡住了&不知道我是必须进行嵌套目录比较,还是有其他办法

注意:.XML(必需文件)的位置始终相同

焦油的结构是:

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 commons库

我当前的解决方案:

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());
            }

        }
文件格式设计为以流的形式写入或读取(即,到/从磁带机),并且没有集中的头。因此,没有办法读取整个文件来提取单个条目


如果想要随机访问,应该使用ZIP格式,并使用JDK的
ZipFile
打开。假设您有足够的虚拟内存,该文件将被内存映射,从而使随机访问非常快(我没有查看如果无法映射内存,它是否会使用随机访问文件)。

您的标题是“ant”,但您的问题涉及使用Apache commons。哪一个是正确的?@parsifal Thank现在更改了标题。我没有选择在这里使用zip文件。@Wills-在这种情况下,您只能阅读整个文件。您可以尝试在
FileInputStream
周围添加
BufferedInputStream
,以提高性能(尽管我怀疑
TarInputStream
内部缓冲区)。既然您已经在使用Jakarta Commons,我建议将您的复制循环替换为
IOUtils.copy()