77import java .nio .file .Files ;
88import java .nio .file .Paths ;
99import java .nio .file .StandardOpenOption ;
10+ import java .util .Arrays ;
1011import java .util .List ;
12+ import java .util .zip .ZipEntry ;
13+ import java .util .zip .ZipOutputStream ;
1114
1215/**
1316 * A utility class to manipulate files.
@@ -37,9 +40,9 @@ private FLUFiles() {} // hide constructor
3740 public static boolean writeFile (String path , String content ) { return internal .writeFile (path , content ); }
3841 public static boolean appendToFile (String path , String content ) { return internal .appendToFile (path , content ); }
3942
40- public static List <String > listFiles (String path ) { return null ; }
43+ public static List <String > listFiles (String path ) { return internal . listFiles ( path ) ; }
4144
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 ) ; }
4346 public static boolean unzip (String source , String destination , String folderIncideZipToGet ) { return false ; }
4447 public static boolean unzip (String source , String destination ) { return unzip (source , destination , "." ); }
4548
@@ -66,7 +69,7 @@ private boolean createFile(String path) {
6669 if (isAValidePath (path )) {
6770 try {
6871 File file = new File (path );
69- file . getParentFile (). mkdirs ( );
72+ createParents ( file );
7073 return file .createNewFile ();
7174 } catch (IOException e ) {
7275 return false ;
@@ -103,7 +106,7 @@ private boolean delete(String path) {
103106 private boolean copy (String source , String destination ) {
104107 if (isAValidePath (source ) && isAValidePath (destination )) {
105108 File destinationFile = new File (destination );
106- destinationFile . getParentFile (). mkdirs ( );
109+ createParents ( destinationFile );
107110 File sourceFile = new File (source );
108111 if (!sourceFile .exists ()) {
109112 return false ;
@@ -131,7 +134,7 @@ private boolean copy(String source, String destination) {
131134 private boolean move (String source , String destination ) {
132135 if (isAValidePath (source ) && isAValidePath (destination )) {
133136 File destinationFile = new File (destination );
134- destinationFile . getParentFile (). mkdirs ( );
137+ createParents ( destinationFile );
135138 return new File (source ).renameTo (destinationFile );
136139 } else {
137140 return false ;
@@ -178,7 +181,7 @@ private String readFileFromWeb(String urlString) {
178181 */
179182 private boolean writeFile (String path , String content ) {
180183 if (isAValidePath (path )) {
181- new File (path ). getParentFile (). mkdirs ( );
184+ createParents (path );
182185 try {
183186 Files .writeString (Paths .get (path ), content , StandardOpenOption .CREATE , StandardOpenOption .WRITE );
184187 return true ;
@@ -195,7 +198,7 @@ private boolean writeFile(String path, String content) {
195198 */
196199 private boolean appendToFile (String path , String content ) {
197200 if (isAValidePath (path )) {
198- new File (path ). getParentFile (). mkdirs ( );
201+ createParents (path );
199202 try {
200203 Files .writeString (Paths .get (path ), content , StandardOpenOption .CREATE , StandardOpenOption .APPEND );
201204 return true ;
@@ -206,5 +209,49 @@ private boolean appendToFile(String path, String content) {
206209 return false ;
207210 }
208211 }
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 (); }
209256 }
210257}
0 commit comments