-
Notifications
You must be signed in to change notification settings - Fork 3
/
file_util.gradle
172 lines (153 loc) · 5.31 KB
/
file_util.gradle
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
ext {
def copyFile = { src, dest ->
copy2(src, dest)
}
}
static boolean copy2(final File src,
final File dest) {
if (src == null) return false;
if (src.isDirectory()) {
return copyDir(src, dest);
}
return copyFile(src, dest);
}
private static boolean copyDir(final File srcDir,
final File destDir) {
return copyOrMoveDir(srcDir, destDir, false);
}
private static boolean copyFile(final File srcFile,
final File destFile) {
return copyOrMoveFile(srcFile, destFile, false);
}
private static boolean copyOrMoveDir(final File srcDir,
final File destDir,
final boolean isMove) {
if (srcDir == null || destDir == null) return false;
// destDir's path locate in srcDir's path then return false
String srcPath = srcDir.getPath() + File.separator;
String destPath = destDir.getPath() + File.separator;
if (destPath.contains(srcPath)) return false;
if (!srcDir.exists() || !srcDir.isDirectory()) return false;
if (!createOrExistsDir(destDir)) return false;
File[] files = srcDir.listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
File oneDestFile = new File(destPath + file.getName());
if (file.isFile()) {
if (!copyOrMoveFile(file, oneDestFile, isMove)) return false;
} else if (file.isDirectory()) {
if (!copyOrMoveDir(file, oneDestFile, isMove)) return false;
}
}
}
return !isMove || deleteDir(srcDir);
}
private static boolean deleteDir(final File dir) {
if (dir == null) return false;
// dir doesn't exist then return true
if (!dir.exists()) return true;
// dir isn't a directory then return false
if (!dir.isDirectory()) return false;
File[] files = dir.listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
if (file.isFile()) {
if (!file.delete()) return false;
} else if (file.isDirectory()) {
if (!deleteDir(file)) return false;
}
}
}
return dir.delete();
}
private static boolean copyOrMoveFile(final File srcFile,
final File destFile,
final boolean isMove) {
if (srcFile == null || destFile == null) return false;
// srcFile equals destFile then return false
if (srcFile.equals(destFile)) return false;
// srcFile doesn't exist or isn't a file then return false
if (!srcFile.exists() || !srcFile.isFile()) return false;
if (destFile.exists()) {
if (!destFile.delete()) {
// unsuccessfully delete then return false
return false;
} else {
return true;
}
}
if (!createOrExistsDir(destFile.getParentFile())) return false;
try {
return writeFileFromIS(destFile.getAbsolutePath(), new FileInputStream(srcFile))
&& !(isMove && !deleteFile(srcFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
}
private static boolean createOrExistsDir(final File file) {
return file != null && (file.exists() ? file.isDirectory() : file.mkdirs());
}
private static boolean writeFileFromIS(final String filePath, final InputStream is) {
return writeFileFromIS(getFileByPath(filePath), is, false);
}
private static boolean writeFileFromIS(final File file,
final InputStream is,
final boolean append) {
int sBufferSize = 524288;
if (is == null || !createOrExistsFile(file)) {
println("create file <" + file + "> failed.");
return false;
}
OutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(file, append), sBufferSize);
byte[] data = new byte[sBufferSize];
for (int len; (len = is.read(data)) != -1;) {
os.write(data, 0, len);
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static boolean createOrExistsFile(final File file) {
if (file == null) return false;
if (file.exists()) return file.isFile();
if (!createOrExistsDir(file.getParentFile())) return false;
try {
return file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
private static File getFileByPath(final String filePath) {
return isSpace(filePath) ? null : new File(filePath);
}
private static boolean isSpace(final String s) {
if (s == null) return true;
for (int i = 0, len = s.length(); i < len; ++i) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}
return true;
}
private static boolean deleteFile(final File file) {
return file != null && (!file.exists() || file.isFile() && file.delete());
}