title: How to unzip all the password protected zip files in a directory using Java
body: <p>I am new to java and trying to write an program which will unzip all the password protected zip files in an directory, I am able to unzip all the normal zip files (Without password) but I am not sure how to unzip  password protected files.
  Note: All zip files have same password</p>
  
  <pre><code>import java.io.OutputStream;
  import java.nio.file.Files;
  import java.nio.file.Path;
  import java.nio.file.PathMatcher;
  import java.nio.file.Paths;
  import java.util.List;
  import java.util.stream.Stream;
  import java.util.zip.ZipEntry;
  import net.lingala.zip4j.core.ZipFile;
  import net.lingala.zip4j.model.FileHeader;
  
  import java.util.zip.*;
  
  public class Extraction {
      // public Extraction() {
      //
      // try {
      //
      // ZipFile zipFile = new
      // ZipFile("C:\\Users\\Desktop\\ZipFile\\myzip.zip");
      //
      // if (zipFile.isEncrypted()) {
      //
      // zipFile.setPassword("CLAIMS!");
      // }
      //
      // List fileHeaderList = zipFile.getFileHeaders();
      //
      // for (int i = 0; i &lt; fileHeaderList.size(); i++) {
      // FileHeader fileHeader = (FileHeader) fileHeaderList.get(i);
      //
      // zipFile.extractFile(fileHeader, "C:\\Users\\Desktop\\ZipFile");
      // System.out.println("Extracted");
      // }
      //
      // } catch (Exception e) {
      // System.out.println("Please Try Again");
      // }
      //
      // }
      //
      // public static void main(String[] args) {
      // new Extraction();
      //
      // }
      // }
  
      public static void main(String[] args) {
          Extraction unzipper = new Extraction();
          unzipper.unzipZipsInDirTo(Paths.get("C:\\Users\\Desktop\\ZipFile"),
                  Paths.get("C:\\Users\\Desktop\\ZipFile\\Unziped"));
  
      }
  
      public void unzipZipsInDirTo(Path searchDir, Path unzipTo) {
  
          final PathMatcher matcher = searchDir.getFileSystem().getPathMatcher("glob:**/*.zip");
          try (final Stream&lt;Path&gt; stream = Files.list(searchDir)) {
              stream.filter(matcher::matches).forEach(zipFile -&gt; unzip(zipFile, unzipTo));
          } catch (Exception e) {
              System.out.println("Something went wrong, Please try again!!");
          }
      }
  
      public void unzip(Path zipFile, Path outputPath) {
          try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(zipFile))) {
  
              ZipEntry entry = zis.getNextEntry();
  
              while (entry != null) {
  
                  Path newFilePath = outputPath.resolve(entry.getName());
                  if (entry.isDirectory()) {
                      Files.createDirectories(newFilePath);
                  } else {
                      if (!Files.exists(newFilePath.getParent())) {
                          Files.createDirectories(newFilePath.getParent());
                      }
                      try (OutputStream bos = Files.newOutputStream(outputPath.resolve(newFilePath))) {
                          byte[] buffer = new byte[Math.toIntExact(entry.getSize())];
  
                          int location;
  
                          while ((location = zis.read(buffer)) != -1) {
                              bos.write(buffer, 0, location);
                          }
                      }
                  }
                  entry = zis.getNextEntry();
              }
          } catch (Exception e1) {
              System.out.println("Please try again");
          }
      }
  
  }
  </code></pre>