Exemplo n.º 1
0
  /**
   * Extracts zip file to the target directory. If patterns are provided only matched paths are
   * extracted.
   *
   * @param zipFile zip file
   * @param destDir destination directory
   * @param patterns optional wildcard patterns of files to extract, may be <code>null</code>
   */
  public static void unzip(File zipFile, File destDir, String... patterns) throws IOException {
    ZipFile zip = new ZipFile(zipFile);
    Enumeration zipEntries = zip.entries();

    while (zipEntries.hasMoreElements()) {
      ZipEntry entry = (ZipEntry) zipEntries.nextElement();
      String entryName = entry.getName();

      if (patterns != null && patterns.length > 0) {
        if (Wildcard.matchPathOne(entryName, patterns) == -1) {
          continue;
        }
      }

      File file = (destDir != null) ? new File(destDir, entryName) : new File(entryName);
      if (entry.isDirectory()) {
        if (!file.mkdirs()) {
          if (file.isDirectory() == false) {
            throw new IOException("Failed to create directory: " + file);
          }
        }
      } else {
        File parent = file.getParentFile();
        if (parent != null && !parent.exists()) {
          if (!parent.mkdirs()) {
            if (file.isDirectory() == false) {
              throw new IOException("Failed to create directory: " + parent);
            }
          }
        }

        InputStream in = zip.getInputStream(entry);
        OutputStream out = null;
        try {
          out = new FileOutputStream(file);
          StreamUtil.copy(in, out);
        } finally {
          StreamUtil.close(out);
          StreamUtil.close(in);
        }
      }
    }

    close(zip);
  }
Exemplo n.º 2
0
  @Override
  protected boolean acceptFile(File file) {
    String path = getMatchingFilePath(file);

    return Wildcard.matchPath(path, pattern);
  }
Exemplo n.º 3
0
 public static boolean validate(Object value, String pattern) {
   if (value == null) {
     return true;
   }
   return Wildcard.matchPath(value.toString(), pattern);
 }