如何将InputStream转换为ZIP格式?

发布于 2024-10-01 16:32:18 字数 1385 浏览 3 评论 0原文

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

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

就我而言,fileTransfer 对象也保存 zip 文件以及 InputStream 对象。 我已经在谷歌上做了很多搜索。但我找不到一个示例来说明 InputStream 到 zip 转换。

更新

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

我通过典型的文件复制程序尝试了这段代码。我知道这是假的,只是尝试过。它给了我java.util.zip.ZipException:ZIP文件必须至少有一个条目

任何建议将非常感激!!!!

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.

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

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.

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.

Any suggestion would be really appreciative!!!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘寄 2024-10-08 16:32:18

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

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

If your input is a an InputStream from 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 InputStream and write to a FileOutputStream, more or less as you're doing, but without worrying about wrapping either stream in a zip-aware stream.

ZipInputStream is 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, ZipOutputStream is used if your have the contents and need to combine them into a zip file.

空心↖ 2024-10-08 16:32:18

请参阅示例 java2s、输入输出。如果您有更多问题,请随时询问他们:)

为了清楚起见,请参阅 此输入示例 你应该这样做:

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

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

此外,如果您确实希望提取 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()) {
  ...

请注意 getNextEntry() 的 javadoc

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

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

我确实相信你不小心删除了第一个 int:

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

我相信你混合了 2 个习惯用法:

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

和:

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

我认为你可以看到区别:)

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

For clarity, in this input example you 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.

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:

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

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

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:

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文