Example #1
0
  /**
   * Extracts the given archive file into the given directory
   *
   * @param sourceArchive Archive to extract
   * @param destinationDirectory Directory to extract archive to
   */
  private static void extractFiles(File sourceArchive, File destinationDirectory) {
    ArchiveInputStream archiveInputStream = null;
    try {
      archiveInputStream = createArchiveInputStream(sourceArchive);
      ArchiveEntry zipEntry;
      while ((zipEntry = archiveInputStream.getNextEntry()) != null) {
        // Validate entry name before extracting
        String validatedEntryName = validateEntryName(zipEntry.getName());

        if (StringUtils.isNotBlank(validatedEntryName)) {
          extractFile(
              sourceArchive,
              destinationDirectory,
              archiveInputStream,
              validatedEntryName,
              zipEntry.getLastModifiedDate(),
              zipEntry.isDirectory());
        }
      }

    } catch (IOException ioe) {
      throw new RuntimeException("Error while extracting " + sourceArchive.getPath(), ioe);
    } finally {
      IOUtils.closeQuietly(archiveInputStream);
    }
  }