| 56 | } |
| 57 | |
| 58 | private static void addToZip(String basePath, ZipOutputStream zos, File toAdd) |
| 59 | throws IOException { |
| 60 | Path dirPath = toAdd.toPath(); |
| 61 | |
| 62 | if (Files.isDirectory(dirPath)) { |
| 63 | try (DirectoryStream<Path> stream = Files.newDirectoryStream(dirPath)) { |
| 64 | for (Path path : stream) { |
| 65 | addToZip(basePath, zos, path.toFile()); |
| 66 | } |
| 67 | } catch (IOException e) { |
| 68 | LOG.warning(() -> String.format("Failed to read directory %s for zipping: %s", toAdd, e)); |
| 69 | } |
| 70 | } else { |
| 71 | try (FileInputStream fis = new FileInputStream(toAdd)) { |
| 72 | String name = toAdd.getAbsolutePath().substring(basePath.length() + 1); |
| 73 | |
| 74 | ZipEntry entry = new ZipEntry(name.replace('\\', '/')); |
| 75 | entry.setTime(toAdd.lastModified()); |
| 76 | entry.setLastModifiedTime(FileTime.fromMillis(toAdd.lastModified())); |
| 77 | zos.putNextEntry(entry); |
| 78 | |
| 79 | int len; |
| 80 | byte[] buffer = new byte[4096]; |
| 81 | while ((len = fis.read(buffer)) != -1) { |
| 82 | zos.write(buffer, 0, len); |
| 83 | } |
| 84 | |
| 85 | zos.closeEntry(); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | public static File unzipToTempDir(String source, String prefix, String suffix) |
| 91 | throws IOException { |