如何从 ZipEntry 创建输入流

发布于 2024-08-11 04:11:46 字数 123 浏览 3 评论 0原文

我有一个包装 ZipEntry 的类,但我正在努力了解如何编写一种从任何一个 ZipEntry 返回输入流的方法。我设法编写了一些可以返回 ZipFile 输入流数组的东西,但我需要一种方法来仅从一个 ZipEntry 获取输入流。

I have a class which wraps ZipEntrys, but I'm struggling to see how I could then write a method that returns an input stream from any one ZipEntry. I managed to write something that could return an array of input streams for a ZipFile, but I need a way to get an input stream from just one ZipEntry.

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

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

发布评论

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

评论(3

时光无声 2024-08-18 04:11:46

这个怎么样?

ZipFile zipFile = new ZipFile("file.zip");
ZipEntry zipEntry = zipFile.getEntry("fileName.txt");       
InputStream inputStream = zipFile.getInputStream(zipEntry);

How about this?

ZipFile zipFile = new ZipFile("file.zip");
ZipEntry zipEntry = zipFile.getEntry("fileName.txt");       
InputStream inputStream = zipFile.getInputStream(zipEntry);
梦回旧景 2024-08-18 04:11:46

您是否没有 ZipEntry 来源的 ZipFile 实例?如果你这样做,你可以使用 ZipFile.getInputStream(ZipEntry)。

https://docs.oracle.com/javase /8/docs/api/java/util/zip/ZipFile.html

PS。快速浏览一下代码,您会发现 ZipEntry 并不是 zip 文件中基础数据的包装器。据我所知,它只是条目的“占位符”(即压缩文件属性而不是数据)。实际的流是通过 ZipFile 类中的 JNI 调用创建的。这意味着我不相信你能以实际的方式做你想做的事。

Do you not have the ZipFile instance from which the ZipEntry was sourced? If you do you could use ZipFile.getInputStream(ZipEntry).

https://docs.oracle.com/javase/8/docs/api/java/util/zip/ZipFile.html

PS. Just had a quick look at the code and a ZipEntry is not a wrapper for the underlying data in the zip file. It is just a "place holder" for the entry as far as I can see (i.e. zipped file attributes not the data). The actual stream is created through a JNI call in the ZipFile class. Meaning that I do not believe you can do what you are looking to do in a practical way.

凉风有信 2024-08-18 04:11:46
 static void printInputStream(File zip) throws IOException
    {
        ZipInputStream zin = new ZipInputStream(new FileInputStream(zip));
        for (ZipEntry zipEntry;(zipEntry = zin.getNextEntry()) != null; )
        {
            System.out.println("reading zipEntry " + zipEntry.getName());
            Scanner sc = new Scanner(zin);
            while (sc.hasNextLine())
            {
                System.out.println(sc.nextLine());
            }
            System.out.println("reading " + zipEntry.getName() + " completed");
        }
       zin.close();
    }

在这里找到的:
从 ZipInputStream 获取 ZipEntry 的输入流(不使用 ZipFile 类) )

对从 zip 文件打开的输入流是什么的误解。
解决方案:从 zip 文件打开输入流
ZipInputStream zipInputStream = ZipInputStream(new FileInputStream(zipfile),
运行循环zipInputStream.getNextEntry()
对于每一轮,您都有当前条目的输入流(之前为 zip 文件打开);
..

 static void printInputStream(File zip) throws IOException
    {
        ZipInputStream zin = new ZipInputStream(new FileInputStream(zip));
        for (ZipEntry zipEntry;(zipEntry = zin.getNextEntry()) != null; )
        {
            System.out.println("reading zipEntry " + zipEntry.getName());
            Scanner sc = new Scanner(zin);
            while (sc.hasNextLine())
            {
                System.out.println(sc.nextLine());
            }
            System.out.println("reading " + zipEntry.getName() + " completed");
        }
       zin.close();
    }

It was found here:
getInputStream for a ZipEntry from ZipInputStream (without using the ZipFile class)

Misunderstanding in what is the input stream that is opened from zip file.
Solution: open input stream from zip file
ZipInputStream zipInputStream = ZipInputStream(new FileInputStream(zipfile),
run cycle zipInputStream.getNextEntry().
For every round you have the inputstream for current entry (opened for zip file before);
..

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