我一直在尝试使用 java 中的 Apache commons compress 递归访问存档文件,但没有成功。
我有一个递归 zip 文件,内部 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
我无法继续前进。任何帮助表示赞赏。