2.Java压缩指定目录或文件成zip

描述:指定一个目录/文件,如果是文件直接下载,如果是目录则可以把该目录以及子目录和目录下的文件全部压缩打包下载

  /**
     * 下载文件:单个文件和zip压缩包
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public void downloadFile(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        OutputStream out = null;
        FileInputStream fr = null;
        try {
            String path = request.getParameter("path");
            String fileName = null;
            String os = System.getProperty("os.name");  
            if(os.toLowerCase().startsWith("win")){
                fileName = path.substring(path.lastIndexOf("\\")+1, path.length());
            }else{
                fileName = path.substring(path.lastIndexOf("/")+1, path.length());                
            }
            File file = new File(path);
            long fileSize = file.length();
            //文件直接下载,目录需要打包zip再下载
            if(file.isDirectory()){
                this.downloadZipFile(response, path, fileName);
            }else{
                response.setCharacterEncoding("UTF-8");
                response.setContentLength((int) fileSize);
                response.setContentType("application/x-download");
                response.setHeader("Pragma","public");
                response.setHeader("Content-Disposition", "attachment;filename=\""
                        + new String(fileName.getBytes("GBK"),"ISO8859-1") + "\"");
                
                out = response.getOutputStream();
                fr = new FileInputStream(file);
                
                int bufSize = 1024 * 1024;
                byte[] buffer = new byte[bufSize];
                int len;
                while ((len = fr.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                }
                out.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(out != null){
                out.close();                
            }
            if(fr != null){
                fr.close();                
            }
        }
    }
public void downloadZipFile(HttpServletResponse response, String path,String zipName){
        zipName = zipName+".zip";
        File zipFile = new File(zipName);
      FileOutputStream fos = null;
      ZipOutputStream zos = null;
        //写入Zip
        try {
                File files = new File(path);
            fos = new FileOutputStream(zipName);
            zos = new ZipOutputStream(fos);
            zos.setEncoding("gbk");
            writeZip(files, "", zos);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(zos != null){
                try {
                    zos.close();
                    zos.closeEntry();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //下载Zip
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            response.reset();
            response.setHeader("Pragma","public");
            response.setHeader("Content-Disposition", "attachment;filename="
                    + new String(zipName.getBytes("GBK"), "ISO-8859-1"));
            response.setContentType("application/octet-stream; charset=utf-8");
            response.setCharacterEncoding("UTF-8");
            out.write(FileUtils.readFileToByteArray(zipFile));
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(out != null){
                try {
                    out.close();
                    //下载完成之后,把打包的zip文件删掉
                    //commons-io-1.4.jar
                    FileUtils.deleteQuietly(zipFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 //打包
    public static void writeZip(File file,String parentPath, ZipOutputStream zos){
        if(file.exists()){
            if(file.isDirectory()){
                parentPath+=file.getName()+File.separator;
                File[] files = file.listFiles();
                if(files.length != 0){
                    for (File f : files) {
                        writeZip(f,parentPath,zos);
                    }
                }else{
                    try {
                        zos.putNextEntry(new ZipEntry(parentPath));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }else{
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(file);
                    ZipEntry ze = new ZipEntry(parentPath+file.getName());
                    zos.putNextEntry(ze);
                    byte[] content = new byte[1024];
                    int len;
                    while((len = fis.read(content)) != -1){
                        zos.write(content);
                        zos.flush();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }finally{
                    if(fis != null){
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值