首页 > 其他分享 >生成zip文件,并下载

生成zip文件,并下载

时间:2023-03-07 14:44:37浏览次数:52  
标签:fis zip 文件 生成 new null response 下载

1. zip生成

 /**
     * @param sourceFilePath :待压缩的文件路径 文件的目录,并非文件路径
     * @param zipFilePath    :压缩后存放路径
     * @param fileName       :压缩后文件的名称  不要传.zip名称
     * @return
     */
    public static boolean fileToZip(String sourceFilePath, String zipFilePath, String fileName) {
        boolean flag = false;
        File sourceFile = new File(sourceFilePath);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        ZipOutputStream zos = null;

        if (sourceFile.exists() == false) {
            System.out.println("待压缩的文件目录:" + sourceFilePath + "不存在.");
            sourceFile.mkdir(); // 新建目录
        }
        try {
            File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
            if (zipFile.exists()) {
                System.out.println(zipFilePath + "目录下存在名字为:" + fileName + ".zip" + "打包文件.");
            } else {
                File[] sourceFiles = sourceFile.listFiles();
                if (null == sourceFiles || sourceFiles.length < 1) {
                    System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");
                } else {
                    fos = new FileOutputStream(zipFile);
                    zos = new ZipOutputStream(new BufferedOutputStream(fos));
                    byte[] bufs = new byte[1024 * 10];
                    for (int i = 0; i < sourceFiles.length; i++) {
                        //创建ZIP实体,并添加进压缩包
                        ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
                        zos.putNextEntry(zipEntry);
                        //读取待压缩的文件并写进压缩包里
                        fis = new FileInputStream(sourceFiles[i]);
                        bis = new BufferedInputStream(fis, 1024 * 10);
                        int read = 0;
                        while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
                            zos.write(bufs, 0, read);
                        }
                    }
                    flag = true;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            //关闭流
            try {
                if (null != bis) bis.close();
                if (null != zos) zos.close();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        return flag;
    }  

调用

// 获取文件的 上一级目录
ZipUtil.fileToZip(parentFile.getPath(), parentFile.getPath(), fd_name);  

2. 下载zip文件

DownloadFile.downloadFile(downFile, fd_name, response, true);
    public static void downloadFile(File file,String newNameTo,HttpServletResponse response,boolean isDelete) {
        try {
            // 以流的形式下载文件。
            System.out.println("导出的路径:"+file.getPath());
            BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(newNameTo, "UTF-8"));
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
            if(isDelete){
                file.delete();        //是否将生成的服务器端文件删除
            }
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }

  

 

标签:fis,zip,文件,生成,new,null,response,下载
From: https://www.cnblogs.com/qukaige/p/17188059.html

相关文章

  • java代码自动生成带swagger3注解
    最近在做一个经组的项目他们用的之前同事配的[tk.mybatis.mapper.generator]自动生成的包,但是这玩意不支持swagger3注解配置。而且重写的话里边BUG还挺多。所以,索性就不用......
  • 日报周报月报工作总结生成器「智能文案生成器」
    ​在职场上,尤其是互联网公司里,“写周报”是一件麻烦的事情。理想的工作环境下,写周报能让工作内容透明化,并有助于总结工作经验和办事方法。但现实情况往往没那么理想。......
  • EF7数据库提供者的自定义值生成器
    本文将讲解提供者使用值生成器的原理。因代码太多,本文只摘要重要代码,并且删除了代码中的注释。如果您不了解,如何使用EF的值生成器?见这篇文章:《EF7创建模型值生成篇》?SqlS......
  • EF7创建模型值生成篇
    在EF7中,生成的值是非常重要的,因为它们决定了数据库表中的数据。在本文中,我们将以人员为例,使用FluentAPI展示所有EF7生成值的功能。我们先来看一下人员表的属性:pub......
  • 《安富莱嵌入式周报》第305期:超级震撼数码管瀑布,使用OpenAI生成单片机游戏代码的可玩
    往期周报汇总地址:http://www.armbbs.cn/forum.php?mod=forumdisplay&fid=12&filter=typeid&typeid=104 说明:谢谢大家的关注,继续为大家盘点上周精彩内容。 视频版:h......
  • Edge 多线程下载+立即下载
    开启多线程下载打开新标签页,在地址栏里输入:edge://flags/#enable-parallel-downloading选择Enabled立即下载(关闭文件扫描)打开新标签页,在地址栏里输入:edge://settin......
  • Android SDK Manager无法显示可供下载的未安装SDK解决方案
    AndroidSDKManager无法显示可供下载的未安装SDK解决规划2012-10-14AndroidSDKManager无法显示可供下载的未安装SDK解决规划据说dl-ssl.google.com在大陆被强了,巨大的......
  • 【博学谷学习记录】超强总结,用心分享 | 小程序 canvas生成图片
    【博学谷IT技术支持】一、目标将画好的canvas生成一张图片,然后生成图片,保存在本地或者相册二、需求分析调研首先查看保存图片到本地,需要用户授权信息其次,canvas保存......
  • Java中如何解析SQL语句、格式化SQL语句、生成SQL语句?
    昨天在群里看到有小伙伴问,Java里如何解析SQL语句然后格式化SQL,是否有现成类库可以使用?之前TJ没有做过这类需求,所以去研究了一下,并找到了一个不过的解决方案,今天推荐给大家......
  • python生成whl
    whl是我们安装python三方库时离线下载的一个文件,用pipinstallxxx.whl即可安装可是有些三方库download时只有tar包文件,没有whl文件比如:pymsgbox  如何生成我们想......