Java 如何将 InputStream 转换为 ZIP 格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4159689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:10:23  来源:igfitidea点击:

How to convert a InputStream to ZIP format?

javazipdwr

提问by

I am having a InputStream Object which is actually a zip file. I want to change it back to zip file and save it. I am using DWR's FileTransfer class object to receive the uploaded data from client.

我有一个 InputStream 对象,它实际上是一个 zip 文件。我想把它改回 zip 文件并保存。我正在使用 DWR 的 FileTransfer 类对象从客户端接收上传的数据。

FileTransferhave 3 methods, getInputStream()is one of them. It returns InputStream from FileTransfer object.

FileTransfer有 3 个方法,getInputStream()是其中之一。它从 FileTransfer 对象返回 InputStream。

In my case, fileTransfer object holds zip file and as well as InputStream object too. I have done, lot of searches in google. But i am not able to find one example, that illustrates InputStream to zip conversion.

就我而言, fileTransfer 对象包含 zip 文件以及 InputStream 对象。我已经完成了,在谷歌中进行了大量搜索。但我找不到一个例子,说明 InputStream 到 zip 的转换。

Update

更新

String zipName = file.getName();
String zipType = file.getMimeType();
InputStream zipStream = file.getInputStream();
ZipInputStream zis = new ZipInputStream(zipStream);
System.out.println("File Name: "+zipName+"\n"+"File Type: "+zipType);
int c;
File f2 = new File(DATA_STORE_LOC+dat+".zip");
path.setPath2(DATA_STORE_LOC+dat+".zip");
FileOutputStream fos = new FileOutputStream(f2);
ZipOutputStream zos = new ZipOutputStream(fos);
c = zis.read();
System.out.println(c);
while ((c = zis.read(BUFFER)) != -1) {
zos.write(BUFFER, 0, c);
}
zos.close();
zis.close();

I tried this code, by thought of a typical file copy program. I know it is false, just tried. It gives me java.util.zip.ZipException: ZIP file must have at least one entry.

我想到了一个典型的文件复制程序,尝试了这段代码。我知道这是假的,只是试过了。它给了我java.util.zip.ZipException: ZIP file must have at least one entry

Any suggestion would be really appreciative!!!!!

任何建议都会非常感激!!!!!

采纳答案by extraneon

See the examples java2s, inputand output. If you have more questions feel free to ask them :)

请参阅示例 java2s, inputoutput。如果您有更多问题,请随时问他们:)

For clarity, in this input exampleyou should do something like:

为清楚起见,在此输入示例中,您应该执行以下操作:

// FileInputStream fin = new FileInputStream(args[i]);
ZipInputStream zin = new ZipInputStream(ft.getInputStream());

As Don Roby correctly said, if you just want to copy you need not know the file structure and you could use for example static IOUtils.copy(in, out)to copy the file.

正如 Don Roby 所说的那样,如果你只是想复制,你不需要知道文件结构,你可以使用例如静态 IOUtils.copy(in, out)来复制文件。

Further, if you do wish to extract the ZIP file contents, you should not plainly copy bytes. The ZIP file has a structure, and you extract Entries from the ZIP file, and not just bytes (see the example). Every Entry is a (compressed) file (or the data thereof) with the original name:

此外,如果您确实希望提取 ZIP 文件内容,则不应简单地复制字节。ZIP 文件具有结构,您可以从 ZIP 文件中提取条目,而不仅仅是字节(参见示例)。每个条目都是具有原始名称的(压缩)文件(或其数据):

ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
  System.out.println("Unzipping " + ze.getName());
  FileOutputStream fout = new FileOutputStream(ze.getName());
  for (int c = zin.read(); c != -1; c = zin.read()) {
  ...

Please note the javadoc of getNextEntry():

请注意getNextEntry()javadoc

Reads the next ZIP file entry and positions the stream at the beginning of the entry data.

读取下一个 ZIP 文件条目并将流定位在条目数据的开头。

This positioning is crucial to get to the zipped file contents, and not the metadata.

这种定位对于获取压缩文件内容至关重要,而不是元数据。

And I do believe that you accidentally remove the first int:

而且我相信您不小心删除了第一个 int:

c = zis.read(); // removing the first
while ((c = zis.read(BUFFER)) != -1) { // so you start with the second?

I believe you mix 2 idioms:

我相信你混合了两个习语:

c = zis.read();
while(c != -1) {
   ...
   c = zis.read();
}

and:

和:

int c;
while ((c = zis.read(BUFFER)) != -1) { // so you start with the second?
  ...
}

I think you can see the difference :)

我想你可以看到区别:)

回答by Don Roby

If your input is a an InputStreamfrom a zip file and your desired output is still a zip file with the same contents, you're just doing a file copy operation and shouldn't have to worry about zip at all. You just need to read from the InputStreamand write to a FileOutputStream, more or less as you're doing, but without worrying about wrapping either stream in a zip-aware stream.

如果您的输入是InputStream来自 zip 文件的 an并且您想要的输出仍然是具有相同内容的 zip 文件,那么您只是在执行文件复制操作,根本不必担心 zip。您只需要在执行过程中或多或少地从 读取InputStream和写入FileOutputStream,而不必担心将任一流包装在可识别 zip 的流中。

ZipInputStreamis useful if you have to extract the contents of the zip file as separate files, i.e., to programmatically unzip. And on the other side, ZipOutputStreamis used if your have the contents and need to combine them into a zip file.

ZipInputStream如果您必须将 zip 文件的内容提取为单独的文件,即以编程方式解压缩,则很有用。另一方面,ZipOutputStream如果您有内容并且需要将它们组合成一个 zip 文件,则使用。