zipinputstream

How can I convert ZipInputStream to InputStream?

和自甴很熟 提交于 2019-12-05 12:39:13
I have code, where ZipInputSream is converted to byte[], but I don't know how I can convert that to inputstream. private void convertStream(String encoding, ZipInputStream in) throws IOException, UnsupportedEncodingException { final int BUFFER = 1; @SuppressWarnings("unused") int count = 0; byte data[] = new byte[BUFFER]; while ((count = in.read(data, 0, BUFFER)) != -1) { // How can I convert data to InputStream here ? } } ZipInputStream is a subclass of InputStream. http://download.oracle.com/javase/6/docs/api/java/util/zip/ZipInputStream.html Here is how I solved this problem. Now I can get

Java create InputStream from ZipInputStream entry

风格不统一 提交于 2019-12-05 01:28:27
问题 I would like to write a method that read several XML files inside a ZIP, from a single InputStream. The method would open a ZipInputStream, and on each xml file, get the corresponding InputStream, and give it to my XML parser. Here is the skeleton of the method : private void readZip(InputStream is) throws IOException { ZipInputStream zis = new ZipInputStream(is); ZipEntry entry = zis.getNextEntry(); while (entry != null) { if (entry.getName().endsWith(".xml")) { // READ THE STREAM } entry =

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

前提是你 提交于 2019-12-04 15:25:55
问题 How can I get an InputStream for a ZipEntry from a ZipInputStream without using the ZipFile class? 回答1: 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 =

extracting contents of ZipFile entries when read from byte[] (Java)

五迷三道 提交于 2019-12-04 11:06:22
I have a zip file whose contents are presented as byte[] but the original file object is not accessible . I want to read the contents of each of the entries. I am able to create a ZipInputStream from a ByteArrayInputStream of the bytes and can read the entries and their names. However I cannot see an easy way to extract the contents of each entry. (I have looked at Apache Commons but cannot see an easy way there either). UPDATE @Rich's code seems to solve the problem, thanks QUERY why do both examples have a multiplier of * 4 (128/512 and 1024*4) ? Rich Seller If you want to process nested zip

Error “must not be null” in Kotlin

二次信任 提交于 2019-12-04 02:52:56
问题 There are multiple files in a .zip file, which I'm trying to get. Trying to unzip the files provides a java.lang.IllegalStateException: zis.nextEntry must not be null. How to do it the right way? @Throws(IOException::class) fun unzip(zipFile: File, targetDirectory: File) { val zis = ZipInputStream( BufferedInputStream(FileInputStream(zipFile))) try { var ze: ZipEntry var count: Int val buffer = ByteArray(8192) ze = zis.nextEntry while (ze != null) { val file = File(targetDirectory, ze.name)

How can I avoid mutable variables in Scala when using ZipInputStreams and ZipOutpuStreams?

不羁岁月 提交于 2019-12-03 23:04:16
I'm trying to read a zip file, check that it has some required files, and then write all valid files out to another zip file. The basic introduction to java.util.zip has a lot of Java-isms and I'd love to make my code more Scala-native. Specifically, I'd like to avoid the use of vars . Here's what I have: val fos = new FileOutputStream("new.zip"); val zipOut = new ZipOutputStream(new BufferedOutputStream(fos)); while (zipIn.available == 1) { val entry = zipIn.getNextEntry if (entryIsValid(entry)) { zipOut.putNewEntry(new ZipEntry("subdir/" + entry.getName()) // read data into the data Array

How to create compressed Zip archive using ZipOutputStream so that method getSize() of ZipEntry returns correct size?

馋奶兔 提交于 2019-12-03 15:50:44
Consider the code example that put a single file test_file.pdf into zip archive test.zip and then read this archive: import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; public class Main { public static void main(String[] args) { File infile = new File("test_file.pdf"); try ( FileInputStream fis = new FileInputStream(infile); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("test.zip")); ) { int bytesRead; byte[] buffer = new byte[1024]; ZipEntry entry = new ZipEntry("data"); entry.setSize(infile.length());

Java create InputStream from ZipInputStream entry

北慕城南 提交于 2019-12-03 15:45:15
I would like to write a method that read several XML files inside a ZIP, from a single InputStream. The method would open a ZipInputStream, and on each xml file, get the corresponding InputStream, and give it to my XML parser. Here is the skeleton of the method : private void readZip(InputStream is) throws IOException { ZipInputStream zis = new ZipInputStream(is); ZipEntry entry = zis.getNextEntry(); while (entry != null) { if (entry.getName().endsWith(".xml")) { // READ THE STREAM } entry = zis.getNextEntry(); } } The problematic part is the "// READ THE STREAM". I have a working solution,

Reading from a ZipInputStream into a ByteArrayOutputStream

我与影子孤独终老i 提交于 2019-12-03 15:18:25
问题 I am trying to read a single file from a java.util.zip.ZipInputStream , and copy it into a java.io.ByteArrayOutputStream (so that I can then create a java.io.ByteArrayInputStream and hand that to a 3rd party library that will end up closing the stream, and I don't want my ZipInputStream getting closed). I'm probably missing something basic here, but I never enter the while loop here: ByteArrayOutputStream streamBuilder = new ByteArrayOutputStream(); int bytesRead; byte[] tempBuffer = new byte

Reading from a ZipInputStream into a ByteArrayOutputStream

女生的网名这么多〃 提交于 2019-12-03 04:57:17
I am trying to read a single file from a java.util.zip.ZipInputStream , and copy it into a java.io.ByteArrayOutputStream (so that I can then create a java.io.ByteArrayInputStream and hand that to a 3rd party library that will end up closing the stream, and I don't want my ZipInputStream getting closed). I'm probably missing something basic here, but I never enter the while loop here: ByteArrayOutputStream streamBuilder = new ByteArrayOutputStream(); int bytesRead; byte[] tempBuffer = new byte[8192*2]; try { while ((bytesRead = zipStream.read(tempBuffer)) != -1) { streamBuilder.write(tempBuffer