/
fs_ext.clj
50 lines (44 loc) · 1.36 KB
/
fs_ext.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
(ns common.fs-ext
(:require [clojure.java.io :as io]
[me.raynes.fs :as fs]
[taoensso.timbre :as log]
[clojure.string :as str]
[common.var :refer [pull-all]]))
(defn file-open
"读取文件,如果找不到文件则读取资源文件"
[file-path]
(if (fs/file? file-path)
(fs/file file-path)
(io/resource file-path)))
(defn file-dir
"获取文件的目录,如果是目录则返回目录本身"
[file-path]
(if (fs/directory? file-path)
file-path
(str (fs/parent file-path))))
(defn file-istream
"获取文件的输入流"
[file-path]
(some-> (file-open file-path)
io/input-stream))
(defn join-files!
"连接多个文件到out-file"
[out-file & files]
(with-open [w (io/output-stream out-file)]
(doseq [f files]
(io/copy (io/input-stream f) w))))
(defn file-content-equal?
"两个文件内容是否相同"
[file1 file2]
(= (slurp file1) (slurp file2)))
(defn extract-resource!
"提取资源文件到当前目录"
([filename] (extract-resource! filename nil))
([filename overwrite]
(let [o-file (io/file filename)]
(when (or overwrite
(not (.exists o-file)))
(log/info :extract-refource filename)
(when-let [res-file (io/resource filename)]
(with-open [in (io/input-stream res-file)]
(io/copy in o-file)))))))