读取网络图片并输出zip压缩包(单文件夹/多文件夹)示例

目录

 

需求

结果示例

示例1

示例2

源码

总结


需求

将图片按照不同的分组存放在不同的文件夹下,然后打包输出。

结果示例

展示的是解压后的文件夹

示例1

示例2

 

源码

package com.asyf.zip_demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@SpringBootApplication
@RestController
public class ZipDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZipDemoApplication.class, args);
        System.out.println("http://localhost:8080/test");
        System.out.println("http://localhost:8080/download");
        System.out.println("http://localhost:8080/downloadSplitFloder");
    }

    @RequestMapping(value = "test")
    public String test() {
        return new Date().toString();
    }

    @RequestMapping(value = "download")
    public void download(HttpServletRequest request, HttpServletResponse response) {
        try {
            String downloadFilename = "图片在一个文件夹.zip";//文件的名称
            downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
            response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
            response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);// 设置在下载框默认显示的文件名
            ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
            String[] files = new String[]{"http://okyd.oss-cn-beijing.aliyuncs.com/img/157164690500022537.jpg", "http://okyd.oss-cn-beijing.aliyuncs.com/img/157164694074782832.jpg"};
            for (int i = 0; i < files.length; i++) {
                URL url = new URL(files[i]);
                zos.putNextEntry(new ZipEntry(i + ".jpg"));
                //读取网络文件
                InputStream fis = url.openConnection().getInputStream();
                byte[] buffer = new byte[1024];
                int r = 0;
                while ((r = fis.read(buffer)) != -1) {
                    zos.write(buffer, 0, r);
                }
                fis.close();
            }
            zos.flush();
            zos.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "downloadSplitFloder")
    public void downloadSplitFloder(HttpServletRequest request, HttpServletResponse response) {
        try {
            String downloadFilename = "图片分成2个文件夹.zip";//文件的名称
            downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
            response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
            response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);// 设置在下载框默认显示的文件名
            ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
            String[] files = new String[]{"http://okyd.oss-cn-beijing.aliyuncs.com/img/157164690500022537.jpg", "http://okyd.oss-cn-beijing.aliyuncs.com/img/157164694074782832.jpg"};
            for (int i = 0; i < files.length; i++) {
                URL url = new URL(files[i]);
                zos.putNextEntry(new ZipEntry("文件夹" + i + "/" + i + ".jpg"));
                //读取网络文件
                InputStream fis = url.openConnection().getInputStream();
                byte[] buffer = new byte[1024];
                int r = 0;
                while ((r = fis.read(buffer)) != -1) {
                    zos.write(buffer, 0, r);
                }
                fis.close();
            }
            zos.flush();
            zos.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

 

总结

zos设置文件时加入“/”,区分目录即可。

关键代码:zos.putNextEntry(new ZipEntry("文件夹" + i + "/" + i + ".jpg"));

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java提供了ZipInputStream和ZipOutputStream两个类来进行zip文件的读取和写入操作,可以不需要解压实现替换zip压缩包里面文件夹中的指定文件。以下是实现的步骤: 1. 创建ZipInputStream对象,读取zip文件中的内容。 2. 创建ZipOutputStream对象,向zip文件中写入内容。 3. 遍历ZipInputStream中的每一个ZipEntry,如果是要替换的文件,则跳过,否则将其写入ZipOutputStream中。 4. 将要替换的文件写入ZipOutputStream中。 5. 关闭ZipInputStream和ZipOutputStream对象。 以下是一个代码示例: ```java import java.io.*; import java.util.zip.*; public class ReplaceFileInZip { public static void main(String[] args) throws IOException { String zipFilePath = "test.zip"; String fileNameToReplace = "test.txt"; String replacementFilePath = "replacement.txt"; // 创建ZipInputStream对象,读取zip文件中的内容 ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath)); // 创建ZipOutputStream对象,向zip文件中写入内容 ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("temp.zip")); // 遍历ZipInputStream中的每一个ZipEntry,如果是要替换的文件,则跳过,否则将其写入ZipOutputStream中 ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { if (entry.getName().equals(fileNameToReplace)) { continue; // 不写入要替换的文件 } zos.putNextEntry(new ZipEntry(entry.getName())); byte[] buffer = new byte[1024]; int len; while ((len = zis.read(buffer)) > 0) { zos.write(buffer, 0, len); } zos.closeEntry(); } // 将要替换的文件写入ZipOutputStream中 ZipEntry replacementEntry = new ZipEntry(fileNameToReplace); zos.putNextEntry(replacementEntry); FileInputStream fis = new FileInputStream(replacementFilePath); byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) > 0) { zos.write(buffer, 0, len); } fis.close(); zos.closeEntry(); // 关闭ZipInputStream和ZipOutputStream对象 zis.close(); zos.close(); // 删除原来的zip文件并将新的temp.zip文件重命名为原来的文件名 new File(zipFilePath).delete(); new File("temp.zip").renameTo(new File(zipFilePath)); } } ``` 在上面的示例中,我们将test.zip文件中的test.txt文件替换为replacement.txt文件。注意,这个示例只是演示了如何进行zip文件中指定文件的替换,实际应用中还需要考虑异常处理、路径处理等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值