zipentry read java inputstream zipfile zipinputstream

java - read - getInputStream para un ZipEntry desde ZipInputStream(sin usar la clase ZipFile)



zipinputstream c# (3)

Err, el ZipInputStream ya es un InputStream. No necesitas otro. Al obtener la siguiente ZipEntry coloca la secuencia al comienzo de la entrada. Ver el Javadoc.

¿Cómo puedo obtener un InputStream para un ZipEntry de un ZipInputStream sin usar la clase ZipFile ?


Para devolver una lista de flujos de entrada que se pueden usar más adelante, utilicé lo siguiente

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


funciona de esta manera

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