How to create a zip file from binary data without knowing contents of zip file in java?

584 Views Asked by At

I am listening to binary data from queues which is actually a zip file data. I want to reconstruct the zip file using java code but I do not have any information about contents of zip file such as no of files, names of files and size of each file in zip file. I think this information is required when we create a zip file using ZipOutputStream and ZipEntry in java. Also, it is certain that zip file contain more than one file. Is there any way to construct zip file from this binary feed of data without knowing details about its content?

I have tried putting ZipEntry for only one file in ZipOutputStream but that only gives me garbage file and zip creation and successive uzipping does not work.

byte[] data = {}; // data received over mq
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File("ZipFile.zip"))); // zip file to be created
ZipEntry ze = new ZipEntry("Output.txt");  
// Creating zipentry for one file in zip 
ze.setSize(data.length);
zos.putNextEntry(ze);  
zos.write(data); // writing da
ta to zip output stream
zos.closeEntry();
zos.close();
1

There are 1 best solutions below

3
Игорь Ходыко On

Try to create real file output.txt (with real size, real name, with your binary data) than add it to zip. I guess, you only write it is a file. You are not putting a file, but the binary data, aren't you? (zip thinks that part of your data is meta information of txt file).