Defry

博客园 首页 新随笔 联系 订阅 管理

String zipFile = /D:/+ ".zip";
   StringOperator.zip(filePath, zipFile);
   InputStream is = null;
   OutputStream os = null;
   BufferedInputStream bis = null;
   BufferedOutputStream bos = null;
   try {
    response.setCharacterEncoding("utf-8");
    response.addHeader("Content-disposition", "attachment;filename=" + folderFileName + ".zip");
    is = new FileInputStream(zipFile);
    bis = new BufferedInputStream(is);
    os = response.getOutputStream();
    bos = new BufferedOutputStream(os);
    
    int read = 0;
    byte[] bytes = new byte[8072];
    while((read = bis.read(bytes, 0, bytes.length)) != -1){
     bos.write(bytes, 0, read);
    }
    bos.flush();
   } catch (Exception e) {
    e.printStackTrace();
   }finally{
    bos.close();
    os.close();
    bis.close();
    is.close();
   }

 

/** 
     * 压缩 
     *  
     * @param sourceFile 
     *            压缩的源文件 如: c:/upload 
     * @param targetZip 
     *            生成的目标文件 如:c:/upload.zip 
     */ 
    public static void zip(String sourceFile,String targetZip){  
          
          Project prj = new Project();  
            
          Zip zip = new Zip();  
            
          zip.setProject(prj);  
            
          zip.setDestFile(new File(targetZip));//设置生成的目标zip文件File对象  
            
          FileSet fileSet = new FileSet();  
            
          fileSet.setProject(prj);  
            
          fileSet.setDir(new File(sourceFile));//设置将要进行压缩的源文件File对象 
            
          zip.addFileset(fileSet);  
 
          zip.execute();  
 
    }  

posted on 2015-06-15 13:36  Defry  阅读(241)  评论(0编辑  收藏  举报