|
1 | 1 | package util.file_managers;
|
2 | 2 |
|
3 |
| -import net.lingala.zip4j.ZipFile; |
4 |
| - |
5 | 3 | import java.io.*;
|
6 | 4 | import java.net.URL;
|
7 | 5 | import java.nio.channels.Channels;
|
8 | 6 | import java.nio.channels.ReadableByteChannel;
|
9 |
| -import java.nio.file.Files; |
10 |
| -import java.nio.file.Path; |
11 |
| -import java.nio.file.Paths; |
12 |
| -import java.nio.file.StandardCopyOption; |
| 7 | +import java.nio.file.*; |
| 8 | +import java.util.Enumeration; |
| 9 | +import java.util.zip.ZipEntry; |
| 10 | +import java.util.zip.ZipFile; |
13 | 11 |
|
14 | 12 | public class ResourceManager {
|
15 | 13 |
|
@@ -91,8 +89,7 @@ private static void downloadResourcesFromGitHubToDataDirectory() {
|
91 | 89 |
|
92 | 90 | System.out.println(String.format("Extracting file: %s, to: %s", outputFile.getAbsolutePath(), DIRECTORY));
|
93 | 91 |
|
94 |
| - ZipFile zipFile = new ZipFile(outputFile); |
95 |
| - zipFile.extractAll(DIRECTORY); |
| 92 | + unzipArchive(outputFile, new File(DIRECTORY)); |
96 | 93 |
|
97 | 94 | outputFile.delete();
|
98 | 95 | } catch (IOException e) {
|
@@ -127,4 +124,45 @@ private static synchronized boolean saveFileToDataDirectory(final InputStream in
|
127 | 124 | }
|
128 | 125 | return true;
|
129 | 126 | }
|
| 127 | + |
| 128 | + private static synchronized void unzipArchive(final File archive, final File destinationDir) { |
| 129 | + try(ZipFile zipFile = new ZipFile(archive)) |
| 130 | + { |
| 131 | + FileSystem fileSystem = FileSystems.getDefault(); |
| 132 | + |
| 133 | + destinationDir.mkdirs(); |
| 134 | + |
| 135 | + Enumeration<? extends ZipEntry> entries = zipFile.entries(); |
| 136 | + |
| 137 | + while (entries.hasMoreElements()) |
| 138 | + { |
| 139 | + ZipEntry entry = entries.nextElement(); |
| 140 | + |
| 141 | + Path filePath = fileSystem.getPath(destinationDir.getAbsolutePath(), entry.getName()); |
| 142 | + |
| 143 | + System.out.println("Unzipping file to: " + filePath.toString()); |
| 144 | + |
| 145 | + if (entry.isDirectory()) |
| 146 | + { |
| 147 | + Files.createDirectories(filePath); |
| 148 | + } |
| 149 | + else |
| 150 | + { |
| 151 | + InputStream is = zipFile.getInputStream(entry); |
| 152 | + BufferedInputStream bis = new BufferedInputStream(is); |
| 153 | + Files.createFile(filePath); |
| 154 | + FileOutputStream fileOutput = new FileOutputStream(filePath.toFile()); |
| 155 | + while (bis.available() > 0) |
| 156 | + { |
| 157 | + fileOutput.write(bis.read()); |
| 158 | + } |
| 159 | + fileOutput.close(); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + catch(IOException e) |
| 164 | + { |
| 165 | + e.printStackTrace(); |
| 166 | + } |
| 167 | + } |
130 | 168 | }
|
0 commit comments