Recursive zip file processing using apache commons compress library

506 Views Asked by At

I have been trying but unsuccessfully to recursively access a archive files using Apache commons compress in java.

I have a recursive zip file with the inner zips being of any type, like tar, jar, bz2, gzip etc and of any depth

eg: zip -> rar -> bz2 ->tar
tar -> bz2 -> rar -> zip -> gzip

Below is the code I have developed.

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

My issue is i am unable to process/convert ZipArchiveEntry to stream. I am getting the below error.

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

I am not able to proceed forward. any help is appreciated.

0

There are 0 best solutions below