7
7
import java .nio .file .Files ;
8
8
import java .nio .file .Paths ;
9
9
import java .nio .file .StandardOpenOption ;
10
+ import java .util .Arrays ;
10
11
import java .util .List ;
12
+ import java .util .zip .ZipEntry ;
13
+ import java .util .zip .ZipOutputStream ;
11
14
12
15
/**
13
16
* A utility class to manipulate files.
@@ -37,9 +40,9 @@ private FLUFiles() {} // hide constructor
37
40
public static boolean writeFile (String path , String content ) { return internal .writeFile (path , content ); }
38
41
public static boolean appendToFile (String path , String content ) { return internal .appendToFile (path , content ); }
39
42
40
- public static List <String > listFiles (String path ) { return null ; }
43
+ public static List <String > listFiles (String path ) { return internal . listFiles ( path ) ; }
41
44
42
- public static boolean zip (String source , String destination ) { return false ; }
45
+ public static boolean zip (String source , String destination ) { return internal . zip ( source , destination ) ; }
43
46
public static boolean unzip (String source , String destination , String folderIncideZipToGet ) { return false ; }
44
47
public static boolean unzip (String source , String destination ) { return unzip (source , destination , "." ); }
45
48
@@ -66,7 +69,7 @@ private boolean createFile(String path) {
66
69
if (isAValidePath (path )) {
67
70
try {
68
71
File file = new File (path );
69
- file . getParentFile (). mkdirs ( );
72
+ createParents ( file );
70
73
return file .createNewFile ();
71
74
} catch (IOException e ) {
72
75
return false ;
@@ -103,7 +106,7 @@ private boolean delete(String path) {
103
106
private boolean copy (String source , String destination ) {
104
107
if (isAValidePath (source ) && isAValidePath (destination )) {
105
108
File destinationFile = new File (destination );
106
- destinationFile . getParentFile (). mkdirs ( );
109
+ createParents ( destinationFile );
107
110
File sourceFile = new File (source );
108
111
if (!sourceFile .exists ()) {
109
112
return false ;
@@ -131,7 +134,7 @@ private boolean copy(String source, String destination) {
131
134
private boolean move (String source , String destination ) {
132
135
if (isAValidePath (source ) && isAValidePath (destination )) {
133
136
File destinationFile = new File (destination );
134
- destinationFile . getParentFile (). mkdirs ( );
137
+ createParents ( destinationFile );
135
138
return new File (source ).renameTo (destinationFile );
136
139
} else {
137
140
return false ;
@@ -178,7 +181,7 @@ private String readFileFromWeb(String urlString) {
178
181
*/
179
182
private boolean writeFile (String path , String content ) {
180
183
if (isAValidePath (path )) {
181
- new File (path ). getParentFile (). mkdirs ( );
184
+ createParents (path );
182
185
try {
183
186
Files .writeString (Paths .get (path ), content , StandardOpenOption .CREATE , StandardOpenOption .WRITE );
184
187
return true ;
@@ -195,7 +198,7 @@ private boolean writeFile(String path, String content) {
195
198
*/
196
199
private boolean appendToFile (String path , String content ) {
197
200
if (isAValidePath (path )) {
198
- new File (path ). getParentFile (). mkdirs ( );
201
+ createParents (path );
199
202
try {
200
203
Files .writeString (Paths .get (path ), content , StandardOpenOption .CREATE , StandardOpenOption .APPEND );
201
204
return true ;
@@ -206,5 +209,49 @@ private boolean appendToFile(String path, String content) {
206
209
return false ;
207
210
}
208
211
}
212
+
213
+ private List <String > listFiles (String path ) {
214
+ if (isAValidePath (path )) {
215
+ String [] list = new File (path ).list ();
216
+ return list == null ? null : Arrays .asList (list );
217
+ } else {
218
+ return null ;
219
+ }
220
+ }
221
+
222
+ private boolean zip (String source , String destination ) {
223
+ if (isAValidePath (source ) && isAValidePath (destination )) {
224
+ destination = FLUStrings .addAtTheEndIfNeeded (destination , ".zip" );
225
+ createParents (destination );
226
+ File sourceFile = new File (source );
227
+ if (!sourceFile .exists ()) {
228
+ return false ;
229
+ }
230
+ try (ZipOutputStream zos = new ZipOutputStream (Files .newOutputStream (Paths .get (destination )))) {
231
+ zipFile (sourceFile , sourceFile .getName (), destination , zos );
232
+ return true ;
233
+ } catch (IOException e ) {
234
+ return false ;
235
+ }
236
+ } else {
237
+ return false ;
238
+ }
239
+ }
240
+ private void zipFile (File fileToZip , String fileName , String destination , ZipOutputStream zos ) throws IOException {
241
+ if (fileToZip .isDirectory ()) {
242
+ fileName = FLUStrings .addAtTheEndIfNeeded (fileName , File .separator );
243
+ zos .putNextEntry (new ZipEntry (fileName ));
244
+ zos .closeEntry ();
245
+ for (File file : fileToZip .listFiles ()) {
246
+ zipFile (file , fileName + file .getName (), destination , zos );
247
+ }
248
+ } else {
249
+ zos .putNextEntry (new ZipEntry (fileName ));
250
+ Files .copy (fileToZip .toPath (), zos );
251
+ zos .closeEntry ();
252
+ }
253
+ }
254
+ private void createParents (String path ) { createParents (new File (path )); }
255
+ private void createParents (File file ) { file .getParentFile ().mkdirs (); }
209
256
}
210
257
}
0 commit comments