示例#1
0
 public static void load(Collection<FileDesc> files, Path root, int blocSize, Pattern pattern)
     throws IOException {
   root = root.toAbsolutePath().normalize();
   Visitor visitor = new Visitor(root, blocSize, pattern);
   Files.walkFileTree(root, visitor);
   for (Future<FileDesc> future : visitor.futures()) {
     try {
       files.add(future.get());
     } catch (Exception e) {
       log.error("", e);
     }
   }
 }
  /**
   * Compresses the specified directory to a zip file
   *
   * @param dir the directory to compress
   * @return the compressed file
   * @throws IOException
   */
  public static Path compress(Path dir) throws IOException {
    Assert.isTrue(Files.exists(dir), "The directory does not exist: " + dir.toAbsolutePath());
    Assert.isTrue(Files.isDirectory(dir), "Should be a directory: " + dir.toAbsolutePath());

    Path result = Paths.get(dir.toAbsolutePath() + FileType.DOT_ZIP);
    try (final ZipOutputStream out =
        new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(result.toFile())))) {
      // out.setMethod(ZipOutputStream.DEFLATED);
      final byte data[] = new byte[BUFFER];
      // get a list of files from current directory
      Files.walkFileTree(
          dir,
          new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(final Path path, final BasicFileAttributes attrs)
                throws IOException {
              final File file = path.toFile();

              // compress to relative directory, not absolute
              final String root = StringUtils.substringAfter(file.getParent(), dir.toString());
              try (final BufferedInputStream origin =
                  new BufferedInputStream(new FileInputStream(file), BUFFER)) {
                final ZipEntry entry = new ZipEntry(root + File.separator + path.getFileName());
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                  out.write(data, 0, count);
                }
              }

              return FileVisitResult.CONTINUE;
            }
          });
    }

    return result;
  }
示例#3
0
文件: IO.java 项目: nremond/boon
  public static List<String> listByGlob(Path pathFromFileSystem, String glob) {

    List<String> result = new ArrayList<>();

    try {
      try (DirectoryStream<Path> stream = Files.newDirectoryStream(pathFromFileSystem, glob)) {
        for (Path entry : stream) {
          result.add(entry.toAbsolutePath().toString());
        }
      }
      return result;
    } catch (IOException ex) {
      return Exceptions.handle(List.class, ex);
    }
  }
示例#4
0
文件: IO.java 项目: nremond/boon
  public static List<String> list(final Path path) {

    List<String> result = new ArrayList<>();

    try {
      try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path)) {
        for (Path entry : directoryStream) {
          result.add(entry.toAbsolutePath().toString());
        }
      }
      return result;
    } catch (IOException ex) {
      return Exceptions.handle(List.class, ex);
    }
  }
示例#5
0
文件: IO.java 项目: nremond/boon
  public static List<String> listByFileExtension(final Path pathFromFileSystem, final String ext) {
    final String extToLookForGlob = "*." + ext;

    List<String> result = new ArrayList<>();

    try {
      try (DirectoryStream<Path> stream =
          Files.newDirectoryStream(pathFromFileSystem, extToLookForGlob)) {
        for (Path entry : stream) {
          result.add(entry.toAbsolutePath().toString());
        }
      }
      return result;
    } catch (IOException ex) {
      return Exceptions.handle(List.class, ex);
    }
  }
示例#6
0
文件: IO.java 项目: nremond/boon
  private static List<String> doListByFileExtensionRecursive(
      final List<String> result, final Path pathFromFileSystem, final String glob) {

    try {
      try (DirectoryStream<Path> stream = Files.newDirectoryStream(pathFromFileSystem, glob)) {
        for (Path entry : stream) {
          result.add(entry.toAbsolutePath().toString());
        }
      }
      try (DirectoryStream<Path> stream = Files.newDirectoryStream(pathFromFileSystem)) {
        for (Path entry : stream) {
          if (Files.isDirectory(entry)) {
            doListByFileExtensionRecursive(result, entry, glob);
          }
        }
      }

      return result;
    } catch (IOException ex) {
      return Exceptions.handle(List.class, ex);
    }
  }