为了账号安全,请及时绑定邮箱和手机立即绑定

使用zip压缩指定文件夹下所有文件

标签:
Java

依赖:

<dependency>

            <groupId>commons-io</groupId>

            <artifactId>commons-io</artifactId>

            <version>2.5</version>

</dependency>


工具类:

FileZipUtil {

    (ZipOutputStream zipFile fileString dir) Exception {
        (file.isDirectory()) {
            File[] fileArray = file.listFiles()(fileArray == ) {
                }
            zip.putNextEntry(ZipEntry(dir + ))dir = dir.length() == ? : dir + (File f : fileArray) {
                (zipfdir + f.getName())}
        } {
            BufferedInputStream bis = BufferedInputStream(FileInputStream(file))String pngName = file.getName()ZipEntry entry = ZipEntry(pngName)zip.putNextEntry(entry)zip.write(FileUtils.(file))IOUtils.(bis)zip.flush()zip.closeEntry()}
    }

    [] (String sourceFilePath) Exception{
        ByteArrayOutputStream outputStream = ByteArrayOutputStream()ZipOutputStream zip = ZipOutputStream(outputStream)File file = File(sourceFilePath)(zipfile)IOUtils.(zip)outputStream.toByteArray()}


    (HttpServletResponse responseString sourceFilePath) {
        SimpleDateFormat sdf = SimpleDateFormat()String filePrefix = sdf.format(Date())String downloadName = filePrefix + {
            OutputStream out = response.getOutputStream()[] data = (sourceFilePath)response.reset()response.addHeader()response.setHeader()response.setHeader(+ downloadName)response.addHeader(+ data.)response.setContentType()IOUtils.(dataout)out.flush()out.close()} (Exception e) {
            e.printStackTrace()}
    }
}

使用:
String path = System.getProperty("user.dir");
String filePath = path+"/file";
String pngPath = filePath+"/png";
FileZipUtil.exportZip(response, pngPath);


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消