简体   繁体   English

Java解压缩.zip文件,不包含子文件夹

[英]Java unzip .zip file without subfolder

I am using zip4j to do decompression, but now I need to decompress the .zip file without original folder structure. 我正在使用zip4j进行解压缩,但是现在我需要在没有原始文件夹结构的情况下解压缩.zip文件。

e.g.
desktop/abc.zip/
               /a
               /b/
                 /x.txt
                 /y.txt
                 /z.txt

I want to extract all files inside the abc.zip directly to desktop. 我想将abc.zip中的所有文件直接提取到桌面。

e.g.
desktop/x.txt
       /y.txt
       /z.txt

Since the .zip file will be protected by password, I cannot do this by java.util.zip library. 由于.zip文件将受密码保护,因此java.util.zip库无法执行此操作。 I did some research on my requirement but in vain. 我根据自己的要求做了一些研究,但徒劳无功。 I also tried to review/rewrite the source code of zip4j, but it seems to be beyond my capability. 我也尝试查看/重写zip4j的源代码,但这似乎超出了我的能力范围。

Do I miss any setting from zip4j which could help me to achieve it easily or is there any other java library suitable on this requirement? 我会错过zip4j的任何设置吗,它可以帮助我轻松实现它,还是有其他适合此要求的Java库?

Below is the source code of part of my program: 以下是我程序的一部分的源代码:

public class FileDecompressor {

    void decompressFiles(String sourceFile, String fileDestination) {
        decompressFiles(sourceFile, fileDestination, "");
    }

    void decompressFiles(String sourceFile, String fileDestination, String zipPassword) {
        try {
            ZipFile zipFile = new ZipFile(sourceFile);
            if (zipFile.isEncrypted()) {
                zipFile.setPassword(zipPassword);
            }
            zipFile.extractAll(fileDestination);
        } catch (ZipException e) {
            e.printStackTrace();
        }
    }
}

It isn't "direct" as such but you could unzip to a temp directory and move the contents to the desktop. 它不是“直接”的,但是您可以解压缩到临时目录并将内容移动到桌面。

An advantage would be that if you use move on the same drive the entire directory would appear on the desktop at one time instead of it appearing part by part. 一个优点是,如果在同一驱动器上使用移动,则整个目录将一次显示在桌面上,而不是部分显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM