-
Notifications
You must be signed in to change notification settings - Fork 0
/
zipfile
35 lines (29 loc) · 1.52 KB
/
zipfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public static void zipFolder(String folderPath, String zipFilePath) throws IOException {
Path sourceDir = Paths.get(folderPath);
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFilePath));
try {
// Walk through all files and directories in the folder
Files.walk(sourceDir).forEach(path -> {
try {
// Only process files, skip directories
if (!Files.isDirectory(path)) {
// Create a ZipEntry with the relative path of the file
String relativePath = sourceDir.relativize(path).toString();
ZipEntry zipEntry = new ZipEntry(relativePath);
// Add the zip entry to the zip output stream
zipOutputStream.putNextEntry(zipEntry);
// Copy the file content to the zip output stream
Files.copy(path, zipOutputStream);
// Close the current zip entry
zipOutputStream.closeEntry();
}
} catch (IOException e) {
System.err.println("Error zipping file: " + path + " - " + e.getMessage());
}
});
} finally {
// Ensure the zip output stream is closed
zipOutputStream.close();
}
}
logging.level.jakarta.validation=DEBUG