Thanks to others for posting their questions and answers. I ended up struggling with this one and doing a fair amount of Googlig to get a solution that verified.
The tricky bit was not using the File System, so as others have pointed out creating a temporary file won't verify.
For others that are still struggling the following code snippets might help.
Extract zip file contents to byte arrays
public static Map<String, byte[]> saveFilesInMap(ZipInputStream zipInputStream) {
Map<String, byte[]> archiveContents = new HashMap<>();
ZipEntry ze;
try {
while ((ze = zipInputStream.getNextEntry()) != null) {
int len;
byte[] buffer = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((len = zipInputStream.read(buffer)) > 0) {
baos.write(buffer, 0, len);
}
archiveContents.put(ze.getName(), baos.toByteArray());
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return archiveContents;
}
Save byte arrays to zip file
public static void addByteArraysToArchive(Map<String, byte[]> byteArrays, ZipOutputStream zipOutputStream) {
for (Map.Entry<String, byte[]> entry : byteArrays.entrySet()) {
String name = entry.getKey();
byte[] bytes = entry.getValue();
try {
ZipEntry zipEntry = new ZipEntry(name);
zipEntry.setSize(bytes.length);
zipOutputStream.putNextEntry(zipEntry);
zipOutputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Save file to zip file
public static void addFileToArchive(String fileToBeAdded, ZipOutputStream zipOutputStream) {
try {
zipOutputStream.putNextEntry(new ZipEntry("new/" + Paths.get(fileToBeAdded).getFileName()));
Files.copy(Paths.get(fileToBeAdded), zipOutputStream);
} catch (IOException e) {
e.printStackTrace();
}
}