0

Need some help to extract ZIP folder which contains inside one more zip file, jar files and folders using java code

public void unzip(String zipFilePath, String destDirectory) throws IOException {
    File destDir = new File(destDirectory);
    if (!destDir.exists()) {
        destDir.mkdir();
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    // iterates over entries in the zip file
    while (entry != null) {
        String filePath = destDirectory + File.separator + entry.getName();
        System.out.println("filePath:"+filePath);
        if (!entry.isDirectory()) {
            // if the entry is a file, extracts it
            extractFile(zipIn, filePath);
        } else {
            // if the entry is a directory, make the directory
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
    byte[] bytesIn = new byte[BUFFER_SIZE];
    int read = 0;
    while ((read = zipIn.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);
    }
    bos.close();
}

Zip File is compressed folder

Test is one more zip folder in zip file

I need code for extract outside zip file and inside zip folder using java code.

I was getting fileNotFoundException while executing above code

java.io.FileNotFoundException: C:\siva\Test.zip (The system cannot find the path specified)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at com.main.UnzipUtility.extractFile(UnzipUtility.java:62)
at com.main.UnzipUtility.unzip(UnzipUtility.java:40)
at com.main.MainClass.main(MainClass.java:14)
tsr_qa
  • 633
  • 3
  • 9
  • 27
  • Look into this answer https://stackoverflow.com/questions/981578/how-to-unzip-files-recursively-in-java – Ismail Aug 14 '19 at 10:42
  • This appears to have nothing to do with zip/unzip. Change `new FileInputStream(zipFilePath)` to `new BufferedInputStream(Files.newInputStream(Paths.get(zipFilePath)))`. That should provide you with a more informative exception, which I suspect will tell you that you don’t have permission to read the file. – VGR Aug 14 '19 at 15:48

0 Answers0