|
| 1 | +package processing.app.tools; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileOutputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.util.Enumeration; |
| 8 | +import java.util.Random; |
| 9 | +import java.util.zip.ZipEntry; |
| 10 | +import java.util.zip.ZipException; |
| 11 | +import java.util.zip.ZipFile; |
| 12 | + |
| 13 | +public class ZipDeflater { |
| 14 | + |
| 15 | + private final ZipFile zipFile; |
| 16 | + private final File destFolder; |
| 17 | + |
| 18 | + public ZipDeflater(File file, File destFolder) throws ZipException, IOException { |
| 19 | + this.destFolder = destFolder; |
| 20 | + this.zipFile = new ZipFile(file); |
| 21 | + } |
| 22 | + |
| 23 | + public void deflate() throws IOException { |
| 24 | + String folderName = tempFolderNameFromZip(); |
| 25 | + |
| 26 | + File folder = new File(destFolder, folderName); |
| 27 | + |
| 28 | + if (!folder.mkdir()) { |
| 29 | + throw new IOException("Unable to create folder " + folderName); |
| 30 | + } |
| 31 | + |
| 32 | + Enumeration<? extends ZipEntry> entries = zipFile.entries(); |
| 33 | + while (entries.hasMoreElements()) { |
| 34 | + ZipEntry entry = entries.nextElement(); |
| 35 | + ensureFoldersOfEntryExist(folder, entry); |
| 36 | + File entryFile = new File(folder, entry.getName()); |
| 37 | + if (entry.isDirectory()) { |
| 38 | + entryFile.mkdir(); |
| 39 | + } else { |
| 40 | + FileOutputStream fos = null; |
| 41 | + InputStream zipInputStream = null; |
| 42 | + try { |
| 43 | + fos = new FileOutputStream(entryFile); |
| 44 | + zipInputStream = zipFile.getInputStream(entry); |
| 45 | + byte[] buffer = new byte[1024 * 4]; |
| 46 | + int len = -1; |
| 47 | + while ((len = zipInputStream.read(buffer)) != -1) { |
| 48 | + fos.write(buffer, 0, len); |
| 49 | + } |
| 50 | + } finally { |
| 51 | + if (fos != null) { |
| 52 | + fos.close(); |
| 53 | + } |
| 54 | + if (zipInputStream != null) { |
| 55 | + zipInputStream.close(); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Test.zip may or may not contain Test folder. We use zip name to create libraries folder. Therefore, a contained Test folder is useless and must be removed |
| 62 | + ensureOneLevelFolder(folder); |
| 63 | + } |
| 64 | + |
| 65 | + private void ensureFoldersOfEntryExist(File folder, ZipEntry entry) { |
| 66 | + String[] parts = entry.getName().split("/"); |
| 67 | + File current = folder; |
| 68 | + for (int i = 0; i < parts.length - 1; i++) { |
| 69 | + current = new File(current, parts[i]); |
| 70 | + current.mkdir(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private void ensureOneLevelFolder(File folder) { |
| 75 | + File[] files = folder.listFiles(); |
| 76 | + if (files.length == 1 && files[0].isDirectory()) { |
| 77 | + File tempFile = new File(files[0].getPath() + new Random().nextInt(1000)); |
| 78 | + files[0].renameTo(tempFile); |
| 79 | + for (File file : tempFile.listFiles()) { |
| 80 | + file.renameTo(new File(folder, file.getName())); |
| 81 | + } |
| 82 | + tempFile.delete(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private String tempFolderNameFromZip() { |
| 87 | + String folderName = zipFile.getName(); |
| 88 | + if (folderName.lastIndexOf(".") != -1) { |
| 89 | + folderName = folderName.substring(0, folderName.lastIndexOf(".")); |
| 90 | + } |
| 91 | + if (folderName.lastIndexOf(File.separator) != -1) { |
| 92 | + folderName = folderName.substring(folderName.lastIndexOf(File.separator) + 1); |
| 93 | + } |
| 94 | + return folderName; |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments