@@ -45,15 +45,15 @@ private FLUFiles() {} // hide constructor
45
45
public static List <String > listFiles (String path ) { return internal .listFiles (path ); }
46
46
47
47
public static boolean zip (String source , String destination ) { return internal .zip (source , destination ); }
48
- public static boolean unzip (String source , String destination , String folderIncideZipToGet ) {
49
- return internal .unzip (source , destination , folderIncideZipToGet );
48
+ public static boolean unzip (String source , String destination , String directoryInsideZipToGet ) {
49
+ return internal .unzip (source , destination , directoryInsideZipToGet );
50
50
}
51
- public static boolean unzip (String source , String destination ) { return unzip (source , destination , ". " ); }
51
+ public static boolean unzip (String source , String destination ) { return unzip (source , destination , "" ); }
52
52
53
53
public static boolean download (String url , String destination , boolean withProgressInfo ) { return false ; }
54
54
public static boolean download (String url , String destination ) { return download (url , destination , false ); }
55
- public static boolean downloadAndUnzip (String url , String destination , String folderIncideZipToGet ) { return false ; }
56
- public static boolean downloadAndUnzip (String url , String destination ) { return downloadAndUnzip (url , destination , ". " ); }
55
+ public static boolean downloadAndUnzip (String url , String destination , String directoryInsideZipToGet ) { return false ; }
56
+ public static boolean downloadAndUnzip (String url , String destination ) { return downloadAndUnzip (url , destination , "" ); }
57
57
public static String downloadAndRead (String url ) { return null ; }
58
58
public static int countEntryOfZipFile (String url ) { return -1 ; }
59
59
public static long getSize (String path ) { return -1 ; }
@@ -67,7 +67,7 @@ public static boolean unzip(String source, String destination, String folderInci
67
67
private static class FLUFilesInternal {
68
68
private FLUFilesInternal () {} // hide constructor
69
69
70
- private static boolean isAValidePath (String path ) { return path != null && ! path . isBlank () ; }
70
+ private boolean isAValidePath (String path ) { return path != null ; }
71
71
72
72
private boolean createFile (String path ) {
73
73
if (isAValidePath (path )) {
@@ -281,35 +281,51 @@ private void zipFile(File fileToZip, String fileName, String destination, ZipOut
281
281
}
282
282
}
283
283
private void createParents (String path ) { createParents (new File (path )); }
284
- private void createParents (File file ) { file .getParentFile ().mkdirs (); }
284
+ private void createParents (File file ) {
285
+ if (file != null ) {
286
+ file .getParentFile ().mkdirs ();
287
+ }
288
+ }
285
289
286
- private boolean unzip (String source , String destination , String folderIncideZipToGet ) {
290
+ private boolean unzip (String source , String destination , String directoryInsideZipToGet ) {
287
291
if (isAValidePath (source ) && isAValidePath (destination )) {
288
292
source = FLUStrings .addAtTheEndIfNeeded (source , ".zip" );
289
293
File destinationFile = new File (destination );
290
294
createParents (destinationFile );
291
295
try (ZipInputStream zis = new ZipInputStream (Files .newInputStream (Paths .get (source )))) {
292
296
for (ZipEntry entry = zis .getNextEntry (); entry != null ; entry = zis .getNextEntry ()) {
293
- createZipEntry (destination , folderIncideZipToGet , zis , entry );
297
+ createZipEntry (destination , directoryInsideZipToGet , zis , entry );
294
298
}
295
- return false ;
299
+ return true ;
296
300
} catch (Exception e ) {
301
+ System .out .println (e );
302
+ e .printStackTrace ();
297
303
return false ;
298
304
}
299
305
} else {
300
306
return false ;
301
307
}
302
308
}
303
- private void createZipEntry (String destination , String folderIncideZipToGet , ZipInputStream zis , ZipEntry entry )
309
+ private void createZipEntry (String destination , String directoryInsideZipToGet , ZipInputStream zis , ZipEntry entry )
304
310
throws IOException {
305
- if (entry .getName ().startsWith (folderIncideZipToGet )) {
306
- String filePath = destination + File .separator + entry .getName ();
307
- if (entry .isDirectory ()) {
308
- createDirectory (filePath );
309
- } else {
310
- createParents (filePath );
311
- Files .copy (zis , Paths .get (filePath ));
311
+ File destinationFile = new File (destination );
312
+ String absoluteDestinationPath = FLUStrings .addAtTheEndIfNeeded (destinationFile .getAbsolutePath (), File .separator );
313
+ // System.out.println("absoluteDestinationPath: " + absoluteDestinationPath);
314
+ String entryName = entry .getName ();
315
+ // System.out.println("entryName before: " + entryName);
316
+ if (entryName .startsWith (directoryInsideZipToGet )) {
317
+ entryName = entryName .substring (Math .max (0 , directoryInsideZipToGet .length () - 1 ));
318
+ File fileToCreate = new File (destination , entryName );
319
+ if (fileToCreate .getAbsolutePath ().startsWith (absoluteDestinationPath )) {
320
+ // System.out.println("entryName: " + entryName);
321
+ if (entry .isDirectory ()) {
322
+ createDirectory (absoluteDestinationPath + entryName );
323
+ } else {
324
+ createParents (absoluteDestinationPath + entryName );
325
+ Files .copy (zis , Paths .get (absoluteDestinationPath + entryName ));
326
+ }
312
327
}
328
+
313
329
}
314
330
}
315
331
}
0 commit comments