使用apache commons压缩库进行递归zip文件处理

时间:2017-07-26 21:33:09

标签: java recursion apache-commons-compress

我一直在努力但是在Java中使用Apache commons compress递归访问存档文件却没有成功。

我有一个递归的zip文件,内部拉链是任何类型的,如tar,jar,bz2,gzip等,任何深度

例如:zip - > rar - > bz2 - > tar
       tar - > bz2 - > rar - > zip - > gzip的

以下是我开发的代码。

public final class zipLister{
private static final ArchiveStreamFactory factory = new ArchiveStreamFactory();

public static void main( String[] args) throws Exception {
    File f = new File("three.zip");
    processZip(f,Optional.of(""));
}

private static void processZip(File f2, Optional<String> of) throws ArchiveException, IOException {

        File f = f2;
        String Prefix = f2.getParent();
        if (!f.isFile()) {
            System.err.println(f + " doesn't exist or is a directory");
        }
        InputStream fis = new BufferedInputStream(new FileInputStream(f));
        ArchiveInputStream ais;
        ais = factory.createArchiveInputStream(fis);

        System.out.println("Created "+ais.toString());
        ArchiveEntry ae;
        while((ae=ais.getNextEntry()) != null){
            if (ae.getName().endsWith(".xml"))
                System.out.println(ae.getName());
            else{
                processFile(ae,Prefix); 
        }
        ais.close();
        fis.close();



    }
}

private static void processFile(ArchiveEntry ae2, String fileprefix) throws IOException, ArchiveException {

        ArchiveEntry ae;
        while ((ae = ((ArchiveInputStream) ae2).getNextEntry()) != null) {
            if (ae.getName().endsWith(".xml"))
                System.out.println(fileprefix + "\\\\" + ae.getName());
            else{
                fileprefix = fileprefix + "\\\\" + ae.getName();
                processFile(ae,fileprefix);
        }
    }
}

我的问题是我无法处理/转换ZipArchiveEntry到流。 我收到以下错误。

 org.apache.commons.compress.archivers.zip.ZipArchiveEntry cannot be cast to org.apache.commons.compress.archivers.ArchiveInputStream

我无法前进。任何帮助表示赞赏。

0 个答案:

没有答案