28
28
public class FLUFiles {
29
29
private static FLUFilesInternal internal = new FLUFilesInternal ();
30
30
private static FLUProgression progression ;
31
+ private static final String FILE_SEPARATOR = "/" ;
31
32
32
33
private FLUFiles () {} // hide constructor
33
34
@@ -71,10 +72,12 @@ public static boolean unzip(String source, String destination, String directoryI
71
72
public static boolean unzip (String source , String destination ) { return unzip (source , destination , "" ); }
72
73
73
74
public static boolean download (String url , String destination ) { return internal .download (url , destination ); }
74
- public static boolean downloadAndUnzip (String url , String destination , String directoryInsideZipToGet ) { return false ; }
75
+ public static boolean downloadAndUnzip (String url , String destination , String directoryInsideZipToGet ) {
76
+ return internal .downloadAndUnzip (url , destination , directoryInsideZipToGet );
77
+ }
75
78
public static boolean downloadAndUnzip (String url , String destination ) { return downloadAndUnzip (url , destination , "" ); }
76
- public static String downloadAndRead (String url ) { return null ; }
77
- public static int countEntryOfZipFile (String url ) { return - 1 ; }
79
+ public static String downloadAndRead (String url ) { return readFileFromWeb ( url ) ; }
80
+ public static int countEntryOfZipFile (String url ) { return internal . countEntryOfZipFile ( url ) ; }
78
81
public static long getSize (String path ) { return -1 ; }
79
82
80
83
public static boolean setMaxPermission (String path , boolean recursive ) { return false ; }
@@ -125,7 +128,7 @@ private boolean delete(String path) {
125
128
File f = new File (path );
126
129
if (f .isDirectory ()) {
127
130
for (String subPath : f .list ()) {
128
- delete (path + File . separator + subPath );
131
+ delete (path + FILE_SEPARATOR + subPath );
129
132
}
130
133
}
131
134
return f .delete ();
@@ -147,7 +150,7 @@ private boolean copy(String source, String destination) {
147
150
if (sourceFile .isDirectory ()) {
148
151
destinationFile .mkdirs ();
149
152
for (String subPath : sourceFile .list ()) {
150
- copy (source + File . separator + subPath , destination + File . separator + subPath );
153
+ copy (source + FILE_SEPARATOR + subPath , destination + FILE_SEPARATOR + subPath );
151
154
}
152
155
return true ;
153
156
}
@@ -297,7 +300,7 @@ private boolean zip(String source, String destination) {
297
300
}
298
301
private void zipFile (File fileToZip , String fileName , String destination , ZipOutputStream zos ) throws IOException {
299
302
if (fileToZip .isDirectory ()) {
300
- fileName = FLUStrings .addAtTheEndIfNeeded (fileName , File . separator );
303
+ fileName = FLUStrings .addAtTheEndIfNeeded (fileName , FILE_SEPARATOR );
301
304
zos .putNextEntry (new ZipEntry (fileName ));
302
305
zos .closeEntry ();
303
306
for (File file : fileToZip .listFiles ()) {
@@ -317,15 +320,30 @@ private void createParents(File file) {
317
320
}
318
321
319
322
private boolean unzip (String source , String destination , String directoryInsideZipToGet ) {
320
- if (isAValidePath (source ) && isAValidePath ( destination ) ) {
323
+ if (isAValidePath (source )) {
321
324
source = FLUStrings .addAtTheEndIfNeeded (source , ".zip" );
325
+ try {
326
+ return unzip (Files .newInputStream (Paths .get (source )), destination , directoryInsideZipToGet );
327
+ } catch (IOException e ) {
328
+ return false ;
329
+ }
330
+ } else {
331
+ return false ;
332
+ }
333
+ }
334
+
335
+ private boolean unzip (InputStream source , String destination , String directoryInsideZipToGet ) {
336
+ if (isAValidePath (destination )) {
322
337
File destinationFile = new File (destination );
323
338
createParents (destinationFile );
324
- try (ZipInputStream zis = new ZipInputStream (Files .newInputStream (Paths .get (source )))) {
339
+ try (ZipInputStream zis = new ZipInputStream (source )) {
340
+ boolean allOk = true ;
325
341
for (ZipEntry entry = zis .getNextEntry (); entry != null ; entry = zis .getNextEntry ()) {
326
- createZipEntry (destination , directoryInsideZipToGet , zis , entry );
342
+ if (!createZipEntry (destination , directoryInsideZipToGet , zis , entry )) {
343
+ allOk = false ;
344
+ }
327
345
}
328
- return true ;
346
+ return allOk ;
329
347
} catch (Exception e ) {
330
348
System .out .println (e );
331
349
e .printStackTrace ();
@@ -335,26 +353,35 @@ private boolean unzip(String source, String destination, String directoryInsideZ
335
353
return false ;
336
354
}
337
355
}
338
- private void createZipEntry (String destination , String directoryInsideZipToGet , ZipInputStream zis , ZipEntry entry )
356
+ private boolean createZipEntry (String destination , String directoryInsideZipToGet , ZipInputStream zis , ZipEntry entry )
339
357
throws IOException {
358
+ directoryInsideZipToGet = FLUStrings .removeAtTheEndIfNeeded (directoryInsideZipToGet .replace ('\\' , '/' ), FILE_SEPARATOR );
340
359
File destinationFile = new File (destination );
341
- String absoluteDestinationPath = FLUStrings .addAtTheEndIfNeeded (destinationFile .getAbsolutePath (), File .separator );
342
- // System.out.println("absoluteDestinationPath: " + absoluteDestinationPath);
343
- String entryName = entry .getName ();
344
- // System.out.println("entryName before: " + entryName);
345
- if (entryName .startsWith (directoryInsideZipToGet )) {
346
- entryName = entryName .substring (Math .max (0 , directoryInsideZipToGet .length () - 1 ));
360
+ String absoluteDestinationPath = FLUStrings .addAtTheEndIfNeeded (destinationFile .getAbsolutePath ().replace ('\\' , '/' ),
361
+ FILE_SEPARATOR );
362
+ String entryName = entry .getName ().replace ('\\' , '/' );
363
+ if (directoryInsideZipToGet .isEmpty () || directoryInsideZipToGet .equals ("." ) || entryName .startsWith (directoryInsideZipToGet )) {
364
+ // Remove part of the path that we don't want
365
+ int charToCut = directoryInsideZipToGet .lastIndexOf ('/' );
366
+ entryName = FLUStrings .removeAtTheBeginningIfNeeded (entryName .substring (Math .max (0 , charToCut )), FILE_SEPARATOR );
367
+
368
+ if (entryName .isEmpty ()) {
369
+ return true ;
370
+ }
347
371
File fileToCreate = new File (destination , entryName );
348
- if (fileToCreate .getAbsolutePath ().startsWith (absoluteDestinationPath )) {
349
- // System.out.println("entryName: " + entryName);
372
+ if (fileToCreate .getAbsolutePath ().replace ('\\' , '/' ).startsWith (absoluteDestinationPath )) {
350
373
if (entry .isDirectory ()) {
351
- createDirectory (absoluteDestinationPath + entryName );
374
+ if (!createDirectory (absoluteDestinationPath + entryName )) {
375
+ return false ;
376
+ }
377
+ return true ;
352
378
} else {
353
379
createParents (absoluteDestinationPath + entryName );
354
- Files .copy (zis , Paths .get (absoluteDestinationPath + entryName ));
380
+ return Files .copy (zis , Paths .get (absoluteDestinationPath + entryName )) >= 0L ;
355
381
}
356
382
}
357
383
}
384
+ return true ;
358
385
}
359
386
360
387
private boolean download (String url , String destination ) {
@@ -373,6 +400,37 @@ private boolean download(String url, String destination) {
373
400
return false ;
374
401
}
375
402
}
403
+
404
+ private boolean downloadAndUnzip (String url , String destination , String directoryInsideZipToGet ) {
405
+ if (isAValidePath (url )) {
406
+ url = FLUStrings .addAtTheEndIfNeeded (url , ".zip" );
407
+ try {
408
+ return unzip (URI .create (url ).toURL ().openStream (), destination , directoryInsideZipToGet );
409
+ } catch (IOException e ) {
410
+ return false ;
411
+ }
412
+ } else {
413
+ return false ;
414
+ }
415
+ }
416
+
417
+ private int countEntryOfZipFile (String url ) {
418
+ if (isAValidePath (url )) {
419
+ try (ZipInputStream zis = new ZipInputStream (URI .create (url ).toURL ().openStream ())) {
420
+ int count = 0 ;
421
+ while (zis .getNextEntry () != null ) {
422
+ count ++;
423
+ }
424
+ return count ;
425
+ } catch (IOException e ) {
426
+ return -1 ;
427
+ }
428
+ } else {
429
+ return -1 ;
430
+ }
431
+ }
432
+
433
+
376
434
class FLUProgressionThread extends Thread {
377
435
private File fileOut ;
378
436
private long fileToDowloadSize ;
@@ -432,5 +490,4 @@ public void run() {
432
490
}
433
491
}
434
492
}
435
-
436
493
}
0 commit comments