Reputation: 21
I am creating a ZipOutputStream and adding files to it this is being done in memory , later I want to be able to get/read files from this. I have tried multiple permutations and combinations but am not able to get it done. I know if I use FileOutpuStream and attach a physical file to it I will be able to use it but I am not allowed to create physical file.
Current Code:-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(bos);
ZipEntry ze = new ZipEntry(“Temp.txt”);
zos.putNextEntry(ze);
FileInputStream fis = new FileInputStream("Test/File1.txt");
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) > 0) {
//System.out.println(new String(buffer));
zos.write(buffer, 0, len);
}
Upvotes: 2
Views: 5033
Reputation: 2897
You're on the right track! To read the file back, simply use:
byte[] zipBytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(zipBytes);
ZipInputStream zis = new ZipInputStream(bis);
Upvotes: 2