从InputStream解压缩文件并返回另一个InputStream [英] Unzipping a file from InputStream and returning another InputStream

查看:222
本文介绍了从InputStream解压缩文件并返回另一个InputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

推荐答案

概念

GZipinputstream用于作为gzip ziped的流(或文件) (。gz扩展名)。它没有任何标题信息。

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

GZipInputStream is for [zippeddata]

如果您有一个真正的zip文件,您必须使用ZipFile打开该文件,询问文件列表(示例中为一个)并询问对于解压缩的输入流。

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.

ZipFile is for a file with [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);
}

读取包含.zip文件内容的InputStream

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

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

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

重要提示:如果您的电脑中有该文件,您可以使用 ZipFile 随机访问它的类

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

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

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

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

这篇关于从InputStream解压缩文件并返回另一个InputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆