Example #1
0
  private static void listFilesRecursive(String path, LinkedHashMap<String, File> files, File dir) {
    if (!path.isEmpty() && !path.endsWith("/")) {
      path = path + "/";
    }

    for (File file : firstNonNull(dir.listFiles(), new File[0])) {
      String filePath = path + file.getName();
      if (file.isDirectory()) {
        files.put(filePath, file);
        listFilesRecursive(filePath, files, file);
      } else {
        files.put(filePath, file);
      }
    }
  }
Example #2
0
 private static void zipDirectory(ZipOutputStream out, String rootPath, File... inputDirs)
     throws IOException {
   LinkedHashMap<String, File> files = newLinkedHashMap();
   for (File inputDir : Lists.reverse(Arrays.asList(inputDirs))) {
     listFilesRecursive(rootPath, files, inputDir);
   }
   for (Entry<String, File> entry : files.entrySet()) {
     String path = entry.getKey();
     File file = entry.getValue();
     if (file.isDirectory()) {
       if (!path.endsWith("/")) {
         path = path + "/";
       }
       ZipEntry dirEntry = new ZipEntry(path);
       dirEntry.setTime(file.lastModified());
       out.putNextEntry(dirEntry);
     } else {
       ZipEntry fileEntry = new ZipEntry(path);
       fileEntry.setTime(file.lastModified());
       out.putNextEntry(fileEntry);
       Files.copy(file, out);
     }
   }
 }