How to create zip files using input stream list?

In my case, I need to load images from the resource folder in my web application. I am now using the following code to upload images via a URL.

url = new URL(properties.getOesServerURL() + "//resources//WebFiles//images//" + imgPath); filename = url.getFile(); is = url.openStream(); os = new FileOutputStream(sClientPhysicalPath + "//resources//WebFiles//images//" + imgPath); b = new byte[2048]; while ((length = is.read(b)) != -1) { os.write(b, 0, length); } 

But I want one operation to read all the images at once and create a zip file for this. I donโ€™t know much about using sequence input streams and zip input streams, so if possible, let me know.

+6
source share
2 answers

The only way I can see that you are able to do this is with something like the following:

 try { ZipOutputStream zip = new ZipOutputStream(new FileOutputStream("C:/archive.zip")); //GetImgURLs() is however you get your image URLs for(URL imgURL : GetImgURLs()) { is = imgURL.openStream(); zip.putNextEntry(new ZipEntry(imgURL.getFile())); int length; byte[] b = new byte[2048]; while((length = is.read(b)) > 0) { zip.write(b, 0, length); } zip.closeEntry(); is.close(); } zip.close(); } 

Link: ZipOutputStream example

+6
source

The url should return a zip file. In addition, you need to take one by one and create a zip using your program

+2
source

Source: https://habr.com/ru/post/952416/


All Articles