简体   繁体   中英

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.

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.

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. 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.

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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