title: Is there a way to unzip a file with various subfolders?
body: <p>Is there a way to unzip a ZIP file with various subfolders in Java without needing to install any additional plugin.</p>
  
  <p>For example, I would like to unzip the file <a href="https://services.gradle.org/distributions/gradle-6.4.1-bin.zip" rel="nofollow noreferrer">https://services.gradle.org/distributions/gradle-6.4.1-bin.zip</a> with this code:</p>
  
  <pre><code>import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import java.util.zip.ZipEntry;
  import java.util.zip.ZipInputStream;
  
  public class unzipfile {
  
      private static void unzip(String zipFilePath, String zipFileName, String destDir) {
          System.out.print("Extracting " + zipFileName + "...");
          File dir = new File(destDir);
          // create output directory if it doesn't exist
          if(!dir.exists()) dir.mkdirs();
          FileInputStream fis;
          //buffer for read and write data to file
          byte[] buffer = new byte[1024];
          try {
              fis = new FileInputStream(zipFilePath);
              try (ZipInputStream zis = new ZipInputStream(fis)) {
                  ZipEntry ze = zis.getNextEntry();
                  while(ze != null){
                      String fileName = ze.getName();
                      File newFile = new File(destDir + File.separator + fileName);
                      //create directories for sub directories in zip
                      new File(newFile.getParent()).mkdirs();
                      try (FileOutputStream fos = new FileOutputStream(newFile)) {
                          int len;
                          while ((len = zis.read(buffer)) &gt; 0) {
                              fos.write(buffer, 0, len);
                          }
                      }
                      //close this ZipEntry
                      ze = zis.getNextEntry();
                  }
              }
              fis.close();
              System.out.println(" Done!");
          } catch (IOException e) {
              System.out.println(" Error while extracting " + zipFileName);
              Logger.getLogger(unzipfile.class.getName()).log(Level.SEVERE, null, e);
          }
      }
  
      public static void main(String[] args) {
          try{
              unzip(args[0], args[1], args[2]);
          } catch (ArrayIndexOutOfBoundsException e) {
              System.out.println("No valid arguments specified. Usage:\n");
              System.out.println("\'java unzipfile [ZIP file path with file name] [file name only] [target directory]\'");
              System.exit(1);
          }
      }
  
  }
  </code></pre>
  
  <p>I run:</p>
  
  <pre><code>java unzipfile path\to\file\gradle-6.4.1-bin.zip gradle-6.4.1-bin.zip .
  </code></pre>
  
  <p>But when I run the full application it gives me the message:</p>
  
  <pre><code>Extracting gradle-6.4.1-bin.zip... Error while extracting gradle-6.4.1-bin.zip
  may. 27, 2020 5:44:37 P.áM. unzipfile unzip
  SEVERE: null
  java.io.FileNotFoundException: .\gradle-6.4.1\README (El sistema no puede encontrar la ruta especificada)
          at java.base/java.io.FileOutputStream.open0(Native Method)
          at java.base/java.io.FileOutputStream.open(FileOutputStream.java:291)
          at java.base/java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:234)
          at java.base/java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:184)
          at unzipfile.unzip(unzipfile.java:29)
          at unzipfile.main(unzipfile.java:50)
  </code></pre>
  
  <p>Thanks for your time.</p>