环境准备

其实也没什么准备,准备好springboot就行,还有几张图片:

将文件打包成zip存放

代码

controller代码:

@requestmapping("/zip")
@restcontroller
public class zipcontroller {

    /**
     * 将文件打包成zip并存放在特定位置
     */
    @postmapping("package")
    public void packagefiletozip() throws ioexception {
        // 为了方便我直接将文件地址写好了,各位可以根据自己的情况修改
        string[] filepath = new string[]{"e:\\ykds\68128498917799516.jpg", "e:\\ykds\68128498917917980.jpg", "e:\\ykds\68128498917807874.jpg"};
        // 将需要打包的文件都放在一个集合中
        list<file> filelist = new arraylist<>();
        for (string s : filepath) {
            file file = new file(s);
            filelist.add(file);
        }
        // 先在d盘创建一个压缩包
        file zipfile = new file("d:\\package.zip");
        if(!zipfile.exists())
            zipfile.createnewfile();
        // 将package.zip的file对象传到tozip对象中
        ziputils.tozip(filelist, zipfile);
    }
}

ziputils工具类代码

public class ziputils {

    /**
     * 把文件集合打成zip压缩包
     * @param srcfiles 压缩文件集合
     * @param zipfile  zip文件名
     * @throws runtimeexception 异常
     */
    public static void tozip(list<file> srcfiles, file zipfile) throws ioexception {
        if(zipfile == null){
            return;
        }
        if(!zipfile.getname().endswith(".zip")){
            return;
        }
        zipoutputstream zos = null;
        fileoutputstream out = new fileoutputstream(zipfile);
        try {
            zos = new zipoutputstream(out);
            for (file srcfile : srcfiles) {
                byte[] buf = new byte[buffer_size];
                zos.putnextentry(new zipentry(srcfile.getname()));
                int len;
                // 读取文件并写入到zip中
                fileinputstream in = new fileinputstream(srcfile);
                while ((len = in.read(buf)) != -1) {
                    zos.write(buf, 0, len);
                    zos.flush();
                }
                in.close();
            }

        } catch (exception e) {
            e.printstacktrace();
        }finally {
            if (zos != null) {
                zos.close();
            }
        }
    }
}

测试

代码打好了,接下来测试下,打开熟悉的postman:

调用接口后就会在d盘中新建一个package.zip的压缩包:

可以看到,我打包的文件都在这里,再看看能不能正常显示:

very good!

将文件打包成zip并导出

上面的方法只是将压缩包保存在本地,如果需要导出的话代码有点不一样。

代码

controller代码:

/**
     * 将文件打包成zip并下载
     */
    @postmapping("download")
    public void download(httpservletresponse response) throws ioexception {
        // 这里还是和上面一样
        string[] filepath = new string[]{"e:\\ykds\68128498917799516.jpg", "e:\\ykds\68128498917917980.jpg", "e:\\ykds\68128498917807874.jpg"};
        list<file> filelist = new arraylist<>();
        for (string s : filepath) {
            file file = new file(s);
            filelist.add(file);
        }
        response.setheader("content-type", "application/octet-stream");
        response.setcontenttype("application/octet-stream");
        response.setheader(httpheaders.content_disposition, "attachment; filename=download.zip");
        ziputils.downloadzip(response.getoutputstream(), filelist);
    }

ziputils工具类代码

public static void downloadzip(outputstream outputstream, list<file> filelist){
        bufferedinputstream bufferedinputstream = null;
        zipoutputstream zipoutputstream = null;
        try {
            zipoutputstream = new zipoutputstream(outputstream);
            for (file file : filelist) {
                zipentry zipentry = new zipentry(file.getname());
                zipoutputstream.putnextentry(zipentry);
                byte[] buf = new byte[buffer_size];
                int len;
                fileinputstream in = new fileinputstream(file);
                while ((len = in.read(buf)) != -1) {
                    zipoutputstream.write(buf, 0, len);
                    zipoutputstream.flush();
                }
            }
            zipoutputstream.flush();
            zipoutputstream.close();

        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            // 关闭流
            try {
                if (bufferedinputstream != null) {
                    bufferedinputstream.close();
                }
                if (zipoutputstream != null ) {
                    zipoutputstream.close();
                }
                if (outputstream != null) {
                    outputstream.close();
                }
            } catch (ioexception e) {
                e.printstacktrace();
            }
        }

    }

测试

还是用postman:

下载完成后打开看看

到此这篇关于手把手教你用springboot将文件打包成zip存放或导出的文章就介绍到这了,更多相关springboot将文件打包成zip内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!