如何使用输入流列表创建ZIP文件?

ami*_*ngh 9 java inputstream spring-mvc zipinputstream

在我的情况下,我必须从我的网络应用程序资源文件夹中下载图像.现在我使用以下代码通过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);
}
Run Code Online (Sandbox Code Playgroud)

但是我想要一次操作来一次读取所有图像并为此创建一个zip文件.我不太了解序列输入流和zip输入流的使用,所以如果可以通过这些,请告诉我.

Sha*_*Haw 8

我能看到你能做到这一点的唯一方法是:

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();
}
Run Code Online (Sandbox Code Playgroud)

参考:ZipOutputStream示例