Java 如何从 ZipEntry 创建输入流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1723026/
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
How do I create an input stream from a ZipEntry
提问by Stephen
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.
我有一个包装 ZipEntrys 的类,但我正在努力了解如何编写一个从任何一个 ZipEntry 返回输入流的方法。我设法编写了一些可以为 ZipFile 返回输入流数组的东西,但我需要一种方法来仅从一个 ZipEntry 获取输入流。
采纳答案by Malcolm Featonby
Do you not have the ZipFile instance from which the ZipEntry was sourced? If you do you could use ZipFile.getInputStream(ZipEntry).
您没有从中获取 ZipEntry 的 ZipFile 实例吗?如果你这样做,你可以使用 ZipFile.getInputStream(ZipEntry)。
https://docs.oracle.com/javase/8/docs/api/java/util/zip/ZipFile.html
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.
附注。快速浏览一下代码,ZipEntry 不是 zip 文件中基础数据的包装器。据我所知,它只是条目的“占位符”(即压缩文件属性而不是数据)。实际流是通过 ZipFile 类中的 JNI 调用创建的。这意味着我不相信您可以以实际的方式做您想做的事情。
回答by Ovi Tisler
How about this?
这个怎么样?
ZipFile zipFile = new ZipFile("file.zip");
ZipEntry zipEntry = zipFile.getEntry("fileName.txt");
InputStream inputStream = zipFile.getInputStream(zipEntry);
回答by Alexandre Marechal Ferrant
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)
在这里找到:
getInputStream for a ZipEntry from ZipInputStream(不使用 ZipFile 类)
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);
..
误解了从 zip 文件打开的输入流是什么。解决方案:从 zip 文件 ZipInputStream 打开输入流zipInputStream = ZipInputStream(new FileInputStream(zipfile)
,运行 cycle zipInputStream.getNextEntry()
。对于每一轮,您都有当前条目的输入流(之前为 zip 文件打开);..