/**
  * Copies all files matching the <code>filter</code> from <code>fromDir</code> to <code>toDir
  * </code>.
  *
  * @param requestor any object to control who called this method. Note that it is considered to be
  *     an external change if <code>requestor</code> is <code>null</code>. See {@link
  *     VirtualFileEvent#getRequestor}
  * @param fromDir the directory to copy from
  * @param toDir the directory to copy to
  * @param filter {@link VirtualFileFilter}
  * @throws IOException if files failed to be copied
  */
 public static void copyDirectory(
     Object requestor,
     @NotNull VirtualFile fromDir,
     @NotNull VirtualFile toDir,
     @Nullable VirtualFileFilter filter)
     throws IOException {
   VirtualFile[] children = fromDir.getChildren();
   for (VirtualFile child : children) {
     if (filter == null || filter.accept(child)) {
       if (!child.isDirectory()) {
         copyFile(requestor, child, toDir);
       } else {
         VirtualFile newChild = toDir.findChild(child.getName());
         if (newChild == null) {
           newChild = toDir.createChildDirectory(requestor, child.getName());
         }
         copyDirectory(requestor, child, newChild, filter);
       }
     }
   }
 }
  public void testFindFileByUrl() throws Exception {
    File file1 = new File(PathManagerEx.getTestDataPath());
    file1 = new File(file1, "vfs");
    file1 = new File(file1, "findFileByUrl");
    VirtualFile file0 = VfsUtil.findFileByURL(file1.toURI().toURL());
    assertNotNull(file0);
    assertTrue(file0.isDirectory());
    final VirtualFile[] children = file0.getChildren();
    List<VirtualFile> list = new ArrayList<VirtualFile>();
    final VirtualFileFilter fileFilter =
        new VirtualFileFilter() {
          @Override
          public boolean accept(VirtualFile file) {
            return !file.getName().endsWith(".new");
          }
        };
    for (VirtualFile child : children) {
      if (fileFilter.accept(child)) {
        list.add(child);
      }
    }
    assertEquals(2, list.size()); // "CVS" dir ignored

    File file2 = new File(file1, "test.zip");
    URL url2 = file2.toURI().toURL();
    url2 = new URL("jar", "", url2.toExternalForm() + "!/");
    url2 = new URL(url2, "com/intellij/installer");
    url2 = new URL(url2.toExternalForm());
    file0 = VfsUtil.findFileByURL(url2);
    assertNotNull(file0);
    assertTrue(file0.isDirectory());

    File file3 = new File(file1, "1.txt");
    file0 = VfsUtil.findFileByURL(file3.toURI().toURL());
    String content = VfsUtilCore.loadText(file0);
    assertNotNull(file0);
    assertFalse(file0.isDirectory());
    assertEquals(content, "test text");
  }
Beispiel #3
0
 /**
  * Copies all files matching the <code>filter</code> from <code>fromDir</code> to <code>toDir
  * </code>. Symlinks end special files are ignored.
  *
  * @param requestor any object to control who called this method. Note that it is considered to be
  *     an external change if <code>requestor</code> is <code>null</code>. See {@link
  *     VirtualFileEvent#getRequestor}
  * @param fromDir the directory to copy from
  * @param toDir the directory to copy to
  * @param filter {@link VirtualFileFilter}
  * @throws IOException if files failed to be copied
  */
 public static void copyDirectory(
     Object requestor,
     @NotNull VirtualFile fromDir,
     @NotNull VirtualFile toDir,
     @Nullable VirtualFileFilter filter)
     throws IOException {
   @SuppressWarnings("UnsafeVfsRecursion")
   VirtualFile[] children = fromDir.getChildren();
   for (VirtualFile child : children) {
     if (!child.isSymLink()
         && !child.isSpecialFile()
         && (filter == null || filter.accept(child))) {
       if (!child.isDirectory()) {
         copyFile(requestor, child, toDir);
       } else {
         VirtualFile newChild = toDir.findChild(child.getName());
         if (newChild == null) {
           newChild = toDir.createChildDirectory(requestor, child.getName());
         }
         copyDirectory(requestor, child, newChild, filter);
       }
     }
   }
 }