Created
October 17, 2019 15:55
-
-
Save team172011/f0a7816f455d29a67da046d2b1bca95e to your computer and use it in GitHub Desktop.
Create a zip file with several entries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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