简体   繁体   English

如何在不知道 java 中 zip 文件内容的情况下从二进制数据创建 zip 文件?

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

I am listening to binary data from queues which is actually a zip file data.我正在听队列中的二进制数据,它实际上是一个 zip 文件数据。 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.我想使用 zip 代码重建 zip 文件,但我没有关于 zip 文件内容的任何信息,例如文件数量、文件名称和 zip 文件中每个文件的大小。 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.我认为当我们在 java 中使用 ZipOutputStream 和 ZipEntry 创建一个 zip 文件时需要这些信息。另外,可以肯定的是 zip 文件包含多个文件。 Is there any way to construct zip file from this binary feed of data without knowing details about its content?有什么方法可以在不知道其内容详细信息的情况下从这个二进制数据源构建 zip 文件?

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.我曾尝试将 ZipEntry 仅用于 ZipOutputStream 中的一个文件,但这只会给我垃圾文件和 zip 创建和连续 uzipping 不起作用。

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();

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.尝试创建真实文件 output.txt(具有真实大小、真实名称和二进制数据),而不是将其添加到 zip。我猜,你只写它是一个文件。 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). (zip认为你的部分数据是txt文件的元信息)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM