getInputStream for a ZipEntry from ZipInputStream(不使用 ZipFile 类)

2022-09-01 07:42:53

如何在不使用该类的情况下从 a 获取 a for a?InputStreamZipEntryZipInputStreamZipFile


答案 1

它以这种方式工作

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

答案 2

呃,已经是一个你不需要另一个。获取下一个位置将流定位在条目的开头。请参阅 Javadoc。ZipInputStreamInputStream.ZipEntry


推荐