Skip to content

Instantly share code, notes, and snippets.

@tobias
Created October 16, 2013 16:47
Show Gist options
  • Save tobias/7011001 to your computer and use it in GitHub Desktop.
Save tobias/7011001 to your computer and use it in GitHub Desktop.
(import '[java.nio.file Path Files FileSystem FileSystems StandardCopyOption FileVisitor FileVisitResult])
(defn ->path [path]
(if (instance? Path path)
path
(.getPath (FileSystems/getDefault) path (into-array String []))))
(defn copy [from to]
(Files/copy (->path from) (->path to) (into-array [StandardCopyOption/COPY_ATTRIBUTES])))
(defn copying-visitor [source dest]
(reify FileVisitor
(preVisitDirectory [_ dir _]
(copy dir (.resolve dest (.relativize source dir)))
FileVisitResult/CONTINUE)
(visitFile [_ file _]
(copy file (.resolve dest (.relativize source file)))
FileVisitResult/CONTINUE)
(postVisitDirectory [_ dir _]
FileVisitResult/CONTINUE)))
(defn copy-dir [source dest]
(let [source (->path source)
dest (->path dest)]
(Files/walkFileTree source (copying-visitor source dest))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment