简体   繁体   English

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

[英]getInputStream for a ZipEntry from ZipInputStream (without using the ZipFile class)

如何在不使用ZipFile类的情况下从ZipInputStream获取ZipEntryInputStream

it works this way 它以这种方式工作

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

Err, the ZipInputStream already is an InputStream. 错误, ZipInputStream已经是一个InputStream. You don't need another one. 你不需要另一个。 Getting the next ZipEntry positions the stream at the beginning of the entry. 获取下一个ZipEntry将流定位在条目的开头。 See the Javadoc. 见Javadoc。

To return a List of Input Streams that can be used later I used the following 要返回稍后可以使用的输入流列表,我使用了以下内容

public static List<InputStream> listResourcesInJar(URL jar) throws IOException{
    ZipInputStream zipInputStream = new ZipInputStream(jar.openStream());
    ZipEntry zipEntry = null;

    List<InputStream> inputStreams = new ArrayList<>();

    while ((zipEntry = zipInputStream.getNextEntry()) != null) {
        String entryName = zipEntry.getName();
        if (entryName.endsWith(".xsd")) {
            inputStreams.add(convertToInputStream(zipInputStream));
        }
    }
    return inputStreams;
}

private static InputStream convertToInputStream(final ZipInputStream inputStreamIn) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IOUtils.copy(inputStreamIn, out);
    return new ByteArrayInputStream(out.toByteArray());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法读取 ZipFile.getInputStream(ZipEntry) 方法返回的 ZipInputStream - Not able to read ZipInputStream returned by ZipFile.getInputStream(ZipEntry) method 关于从Java中的ZipInputStream / ZipEntry读取的问题 - Question about reading from a ZipInputStream/ZipEntry in java 是否有可能从zipinputstream获得zipentry的输入流? - is it possible to get a zipentry's inputstream from a zipinputstream? 如何在不写入outputstream的情况下从ZipInputStream获取每个ZipFile条目的字节/内容? - How to get bytes/contents of each ZipFile entry from ZipInputStream without writing to outputstream? 使用 ZipEntry.getInputStream(entry) 获得的流的异常 - Exception from stream obtained with ZipEntry.getInputStream(entry) Java ZipInputStream无法读取整个ZipEntry - Java ZipInputStream not reading entire ZipEntry 如何在不使用 ByteArrayOutputStream 的情况下从 ZipEntry 获取字节 - How to get bytes from ZipEntry without using ByteArrayOutputStream ZipInputStream和JarInputStream的ZipEntry大小之间的不一致 - Inconsistency between ZipEntry size for ZipInputStream and JarInputStream Android ZipInputStream ZipEntry不读取到文件末尾 - Android ZipInputStream ZipEntry does not read to end of file 如何使用ZipEntry读取位于另一个zip文件中的一个zip文件中的数据? - How to read data in one zip file located in another zipfile using ZipEntry?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM