I have a clas开发者_如何学Gos 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.
How about this?
ZipFile zipFile = new ZipFile("file.zip");
ZipEntry zipEntry = zipFile.getEntry("fileName.txt");
InputStream inputStream = zipFile.getInputStream(zipEntry);
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.
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);
..
精彩评论