Skip to content

Instantly share code, notes, and snippets.

@team172011
Created October 17, 2019 15:55
Show Gist options
  • Save team172011/f0a7816f455d29a67da046d2b1bca95e to your computer and use it in GitHub Desktop.
Save team172011/f0a7816f455d29a67da046d2b1bca95e to your computer and use it in GitHub Desktop.
Create a zip file with several entries
private byte[] createZipAsByteArray(Path path, Map<String, File> files) throws IOException {
String pathToZip = path.toString()+File.separator+"content.zip";
FileOutputStream fileOutputStream = new FileOutputStream(pathToZip);
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
byte[] buffer = new byte[1024];
int len;
for(Map.Entry<String, File> file: files.entrySet()) {
FileInputStream fileInputStream2 = new FileInputStream(file.getValue());
ZipEntry zipEntry = new ZipEntry(file.getKey());
zipOutputStream.putNextEntry(zipEntry);
while ((len = fileInputStream2.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, len);
}
fileInputStream2.close();
zipOutputStream.closeEntry();
}
zipOutputStream.close();
return Files.readAllBytes(new File(pathToZip).toPath());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment