首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据提取焦油和7z

数据提取焦油和7z
EN

Stack Overflow用户
提问于 2019-12-12 13:11:42
回答 1查看 183关注 0票数 0

我有一个.tar文件,其中包含许多文件夹和子文件夹。在这许多文件夹中,除了其他文件之外,还有.7z文件。我想搜索这些文件夹/子文件夹并找到.7z文件(将它们分配给数组?)并将它们提取到各自的位置。

我正在使用Apache : 1) org.apache.commons.compress.archivers.sevenz提供了使用7z格式读取和写入档案的类。2) org.apache.commons.compress.archivers.tar

提供流类,用于使用TAR格式读取和写入档案。

  1. 步骤我想要提取.tar文件
  2. 步骤,我想递归地遍历已提取的.tar文件文件夹及其子文件夹,并找到.7z文件。在第3步中,
  3. 要将我找到的.7z文件数组提供给数组,并将它们提取到它们各自的位置。

我在3.数组调用/分配的步骤中遇到了问题:/您能帮忙吗?(非常感谢:)

代码语言:javascript
复制
    /**
     * uncompresses .tar file
     * @param in
     * @param out
     * @throws IOException
     */
    public static void decompressTar(String in, File out) throws IOException {
        try (TarArchiveInputStream tin = new TarArchiveInputStream(new FileInputStream(in))){
            TarArchiveEntry entry;
            while ((entry = tin.getNextTarEntry()) != null) {
                if (entry.isDirectory()) {
                    continue;
                }
                File curfile = new File(out, entry.getName());
                File parent = curfile.getParentFile();
                if (!parent.exists()) {
                    parent.mkdirs();
                }
                IOUtils.copy(tin, new FileOutputStream(curfile));
            }
        }
    }

    /**
     * uncompresses .7z file
     * @param in
     * @param destination
     * @throws IOException
     */
    public static void decompressSevenz(String in, File destination) throws IOException {
        //@SuppressWarnings("resource")
        SevenZFile sevenZFile = new SevenZFile(new File(in));
        SevenZArchiveEntry entry;
        while ((entry = sevenZFile.getNextEntry()) != null){
            if (entry.isDirectory()){
                continue;
            }
            File curfile = new File(destination, entry.getName());
            File parent = curfile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
            FileOutputStream out = new FileOutputStream(curfile);
            byte[] content = new byte[(int) entry.getSize()];
            sevenZFile.read(content, 0, content.length);
            out.write(content);
            out.close();
        }
        sevenZFile.close();
    }

    public void run()
    {
        //1) uncompress .tar
        try {
            JThreadTar.decompressTar(RECURSIVE_DIRECTORY_PATH, new File(RECURSIVE_DIRECTORY));
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        //2) go through the extracted .tar file directory and look for .7z (recursively?)
        File[] files = new File(RECURSIVE_DIRECTORY).listFiles();

        for (File file : files) {
                if (file.isDirectory()) {

                    File[] matches = file.listFiles(new FilenameFilter()
                    {
                      public boolean accept(File dir, String name)
                      {
                         return name.endsWith(".7z");
                      }
                    });

                    for (File element: matches) {
                        System.out.println(element);
                        }
                }
                else {
                    continue;
                }
        }

        //3) Feed the array above to decompressSevenz method

        for (int i = 0; i < matches.length; i++)
        {
            if (matches[i].isFile())
            {      
                try {
                JThreadTar.decompressSevenz(matches[i].toString(), new File(RECURSIVE_DIRECTORY));
                } 
                catch (IOException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
                }
            }
        }

我的问题是:我不能引用步骤3中的[]匹配,我没有正确地使用它。我只想为.7z文件匹配创建一个数组[]匹配。每次找到一个.7z时,我都想将它添加到这个数组中。在第3步中,我想将每个.7z提取到它的相对位置。

我走得更远了一点:

代码语言:javascript
复制
    //1) uncompress .tar
        try {
            JThreadTar.decompressTar(RECURSIVE_DIRECTORY_PATH, new File(RECURSIVE_DIRECTORY));
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        //2) go through the extracted .tar file directory and look for .7z (recursively?)
        File dir = new File(RECURSIVE_DIRECTORY);
        File[] dirFiles = dir.listFiles();
        ArrayList<File> matches2 = new ArrayList<File>();

        for (File file : dirFiles) {
                if (file.isDirectory()) {
                    File[] matches = dir.listFiles(new FilenameFilter()
                    {
                      public boolean accept(File dir, String name)
                      {
                         return name.endsWith(".7z");
                      }
                    });
                    matches2.addAll(Arrays.asList(matches));
                }
                else if (file.isFile()) {
                    if (file.getName().endsWith(".7z")){
                    matches2.add(file);
                    };
                    }
                };


            //3) Feed the arraylist above to decompressSevenz method   
            for (int counter = 0; counter < matches2.size(); counter++) {
            if (matches2.get(counter).isFile())
            {  
                try {
                JThreadTar.decompressSevenz(matches2.get(counter).toString(), new File(RECURSIVE_DIRECTORY));
                } 
                catch (IOException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
                }
            }
            }

下面是@Joop Eggen第2步和第3步的最后形式

代码语言:javascript
复制
        Path topDir = Paths.get(RECURSIVE_DIRECTORY);
        try {
            Files.walk(topDir)
                .filter(path -> path.getFileName().toString().endsWith(".7z"))
                .forEach(path -> {
                    try {
                        JThreadTar.decompressSevenz(path.toString(), topDir.toFile());
                    } catch (IOException e2) {
                        e2.printStackTrace();
                    }
            });
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

recursively:

  1. step

代码语言:javascript
复制
        Path toptopDir = Paths.get(RECURSIVE_DIRECTORY_PATH);
        try {
            Files.walk(toptopDir)
                .filter(path -> path.getFileName().toString().endsWith(".tar"))
                .forEach(path -> {
                    try {
                        JThreadTar.decompressTar(RECURSIVE_DIRECTORY_PATH, new File(RECURSIVE_DIRECTORY));
                    } catch (IOException e2) {
                        e2.printStackTrace();
                    }
            });
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-12 15:54:17

我利用这个机会使用了更新的路径和文件。Files.listFiles()可能返回null。而Arrays.asList等的使用会导致大量的数据。

所有这些都将简化为:

代码语言:javascript
复制
    Path topDir = Paths.get(RECURSIVE_DIRECTORY);
    Files.walk(topDir)
        .filter(path -> path.getFileName().toString().endsWith(".7z"))
        .forEach(path -> {
            try {
                JThreadTar.decompressSevenz(path.toString(), topDir.toFile());
            } catch (IOException e2) {
                e2.printStackTrace();
            }
    });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59305412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档