来自ZipInputStream的ZipEntry的getInputStream(不使用ZipFile类)

Moh*_*any 25 java inputstream zipfile zipinputstream

如何在不使用课程的情况下获得InputStreama ?ZipEntryZipInputStreamZipFile

use*_*421 18

呃,ZipInputStream已经是一个InputStream.你不需要另一个.ZipEntry在条目开头获取下一个流的位置.见Javadoc.


Evg*_*eev 18

它以这种方式工作

static InputStream getInputStream(File zip, String entry) throws IOException {
    ZipInputStream zin = new ZipInputStream(new FileInputStream(zip));
    for (ZipEntry e; (e = zin.getNextEntry()) != null;) {
        if (e.getName().equals(entry)) {
            return zin;
        }
    }
    throw new EOFException("Cannot find " + entry);
}

public static void main(String[] args) throws Exception {
    InputStream in = getInputStream(new File("f:/1.zip"), "launch4j/LICENSE.txt");
    Scanner sc = new Scanner(in);
    while(sc.hasNextLine()) {
        System.out.println(sc.nextLine());
    }
    in.close();
}
Run Code Online (Sandbox Code Playgroud)