Java 从 InputStream 解压缩文件并返回另一个 InputStream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2063099/
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-13 03:08:10  来源:igfitidea点击:

Unzipping a file from InputStream and returning another InputStream

javazipstream

提问by Baishampayan Ghose

I am trying to write a function which will accept an InputStreamwith zipped file data and would return another InputStreamwith unzipped data.

我正在尝试编写一个函数,该函数将接受一个InputStream带有压缩文件数据的函数,并返回另一个InputStream带有解压缩数据的函数。

The zipped file will only contain a single file and thus there is no requirement of creating directories, etc...

压缩文件将只包含一个文件,因此不需要创建目录等...

I tried looking at ZipInputStreamand others but I am confused by so many different types of streams in Java.

我尝试查看ZipInputStream和其他人,但我对 Java 中这么多不同类型的流感到困惑。

采纳答案by helios

Concepts

概念

GZipinputstream is for streams (or files) ziped as gzip (".gz" extension). It doesn't have any header information.

GZipinputstream 用于压缩为 gzip(“.gz”扩展名)的流(或文件)。它没有任何标题信息。

GZipInputStream is for [zippeddata]

GZipInputStream 用于 [zippeddata]

If you have a real zip file, you have to user ZipFile to open the file, ask for the list of files (one in your example) and ask for the decompressed input stream.

如果你有一个真正的 zip 文件,你必须使用 ZipFile 来打开文件,询问文件列表(在你的例子中是一个)并询问解压缩的输入流。

ZipFile is for a file with [header information + zippeddata]

ZipFile 用于带有 [header information + zippeddata] 的文件

Your method, if you have the file, would be something like:

如果您有文件,您的方法将类似于:

// ITS PSEUDOCODE!!

private InputStream extractOnlyFile(String path) {
   ZipFile zf = new ZipFile(path);
   Enumeration e = zf.entries();
   ZipEntry entry = (ZipEntry) e.nextElement(); // your only file
   return zf.getInputStream(entry);
}

Reading an InputStream with the content of a .zip file

使用 .zip 文件的内容读取 InputStream

Ok, if you have an InputStream you can use (as @cletus says) ZipInputStream. It reads a stream including header data.

好的,如果您有 InputStream,则可以使用(如@cletus 所说)ZipInputStream。它读取包含头数据的流。

ZipInputStream is for a stream with [header information + zippeddata]

ZipInputStream 用于带有 [header information + zippeddata] 的流

Important: if you have the file in your PC you can use ZipFileclass to access it randomly

重要提示:如果您的 PC 中有该文件,则可以使用ZipFileclass 随机访问它

This is a sample of reading a zip-file through an InputStream:

这是通过 InputStream 读取 zip 文件的示例:

import java.io.FileInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;


public class Main {
    public static void main(String[] args) throws Exception
    {
        FileInputStream fis = new FileInputStream("c:/inas400.zip");

        // this is where you start, with an InputStream containing the bytes from the zip file
        ZipInputStream zis = new ZipInputStream(fis);
        ZipEntry entry;
            // while there are entries I process them
        while ((entry = zis.getNextEntry()) != null)
        {
            System.out.println("entry: " + entry.getName() + ", " + entry.getSize());
                    // consume all the data from this entry
            while (zis.available() > 0)
                zis.read();
                    // I could close the entry, but getNextEntry does it automatically
                    // zis.closeEntry()
        }
    }
}

回答by cletus

Unless I'm missing something, you should absolutely try and get ZipInputStreamto work and there's no reason it shouldn't (I've certainly used it on several occasions).

除非我遗漏了什么,否则您绝对应该尝试开始ZipInputStream工作,并且没有理由不这样做(我当然已经多次使用它)。

What you should do is try and get ZipInputStreamto work and if you can't, post the code and we'll help you with whatever problems you're having.

您应该做的是尝试开始ZipInputStream工作,如果不能,请发布代码,我们将帮助您解决遇到的任何问题。

Whatever you do though, don't try and reinvent its functionality.

不管你做什么,都不要试图重新发明它的功能。

回答by nanda

If you can change the input data I would suggested you to use GZIPInputStream.

如果您可以更改输入数据,我建议您使用GZIPInputStream.

GZipInputStreamis different from ZipInputStreamsince you only have one data inside it. So the whole input stream represents the whole file. In ZipInputStreamthe whole stream contains also the structure of the file(s) inside it, which can be many.

GZipInputStream不同于ZipInputStream因为里面只有一个数据。所以整个输入流代表整个文件。在ZipInputStream整个流中还包含其中文件的结构,可以是很多。

回答by Roman Kazanovskyi

It is on scala syntax:

它是在 Scala 语法上的:

def unzipByteArray(input: Array[Byte]): String = {
    val zipInputStream = new ZipInputStream(new ByteArrayInputStream(input))
    val entry = zipInputStream.getNextEntry
    IOUtils.toString(zipInputStream, StandardCharsets.UTF_8)
}