zoukankan      html  css  js  c++  java
  • 读写ZIP文件

    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();  
     
        }  

  • 相关阅读:
    How to solve the problem "A project with an Output Type of Class Library cannot be started directly "
    Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)
    JavaScript实现startWith、endWith效果函数
    不同格式证书导入keystore方法
    用KeyTool生成安全证书
    怎样让操作系统的虚拟机退出全屏?
    对https的理解
    JDK中keytool常用命令
    百折不回结局凄惨的金庸反角
    主题数据库的特征
  • 原文地址:https://www.cnblogs.com/Defry/p/4576914.html
Copyright © 2011-2022 走看看