|
2 | 2 |
|
3 | 3 | import java.io.File;
|
4 | 4 | import java.io.IOException;
|
| 5 | +import java.io.InputStream; |
5 | 6 | import java.net.URI;
|
6 | 7 | import java.net.URL;
|
7 | 8 | import java.nio.file.Files;
|
|
10 | 11 | import java.util.Arrays;
|
11 | 12 | import java.util.List;
|
12 | 13 | import java.util.zip.ZipEntry;
|
| 14 | +import java.util.zip.ZipInputStream; |
13 | 15 | import java.util.zip.ZipOutputStream;
|
14 | 16 |
|
15 | 17 | /**
|
@@ -43,7 +45,9 @@ private FLUFiles() {} // hide constructor
|
43 | 45 | public static List<String> listFiles(String path) { return internal.listFiles(path); }
|
44 | 46 |
|
45 | 47 | public static boolean zip(String source, String destination) { return internal.zip(source, destination); }
|
46 |
| - public static boolean unzip(String source, String destination, String folderIncideZipToGet) { return false; } |
| 48 | + public static boolean unzip(String source, String destination, String folderIncideZipToGet) { |
| 49 | + return internal.unzip(source, destination, folderIncideZipToGet); |
| 50 | + } |
47 | 51 | public static boolean unzip(String source, String destination) { return unzip(source, destination, "."); }
|
48 | 52 |
|
49 | 53 | public static boolean download(String url, String destination, boolean withProgressInfo) { return false; }
|
@@ -129,6 +133,29 @@ private boolean copy(String source, String destination) {
|
129 | 133 | return false;
|
130 | 134 | }
|
131 | 135 | }
|
| 136 | + // private boolean copy(File source, OutputStream destination) { |
| 137 | + // if (isAValidePath(source.getName())) { |
| 138 | + // if (source == null || !source.exists()) { |
| 139 | + // return false; |
| 140 | + // } |
| 141 | + // if (source.isDirectory()) { |
| 142 | + // boolean flag = true; |
| 143 | + // for (String subPath : source.list()) { |
| 144 | + // if (!copy(new File(source, subPath), destination)) { |
| 145 | + // flag = false; |
| 146 | + // } |
| 147 | + // } |
| 148 | + // return flag; |
| 149 | + // } |
| 150 | + // try { |
| 151 | + // return Files.copy(source.toPath(), destination) > 0; |
| 152 | + // } catch (IOException e) { |
| 153 | + // return false; |
| 154 | + // } |
| 155 | + // } else { |
| 156 | + // return false; |
| 157 | + // } |
| 158 | + // } |
132 | 159 |
|
133 | 160 |
|
134 | 161 | private boolean move(String source, String destination) {
|
@@ -170,7 +197,9 @@ private String readFileFromWeb(String urlString) {
|
170 | 197 | }
|
171 | 198 | try {
|
172 | 199 | URL url = URI.create(urlString).toURL();
|
173 |
| - return new String(url.openStream().readAllBytes()); |
| 200 | + try (InputStream is = url.openStream()) { |
| 201 | + return new String(is.readAllBytes()); |
| 202 | + } |
174 | 203 | } catch (IOException e) {
|
175 | 204 | return null;
|
176 | 205 | }
|
@@ -253,5 +282,35 @@ private void zipFile(File fileToZip, String fileName, String destination, ZipOut
|
253 | 282 | }
|
254 | 283 | private void createParents(String path) { createParents(new File(path)); }
|
255 | 284 | private void createParents(File file) { file.getParentFile().mkdirs(); }
|
| 285 | + |
| 286 | + private boolean unzip(String source, String destination, String folderIncideZipToGet) { |
| 287 | + if (isAValidePath(source) && isAValidePath(destination)) { |
| 288 | + source = FLUStrings.addAtTheEndIfNeeded(source, ".zip"); |
| 289 | + File destinationFile = new File(destination); |
| 290 | + createParents(destinationFile); |
| 291 | + try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(Paths.get(source)))) { |
| 292 | + for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) { |
| 293 | + createZipEntry(destination, folderIncideZipToGet, zis, entry); |
| 294 | + } |
| 295 | + return false; |
| 296 | + } catch (Exception e) { |
| 297 | + return false; |
| 298 | + } |
| 299 | + } else { |
| 300 | + return false; |
| 301 | + } |
| 302 | + } |
| 303 | + private void createZipEntry(String destination, String folderIncideZipToGet, ZipInputStream zis, ZipEntry entry) |
| 304 | + 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)); |
| 312 | + } |
| 313 | + } |
| 314 | + } |
256 | 315 | }
|
257 | 316 | }
|
0 commit comments