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

  • 相关阅读:
    [leetcode.com]算法题目
    [leetcode.com]算法题目
    [leetcode.com]算法题目
    [实战演练]2014年人人公司应届生校招技术笔试题
    [杂谈]笔试中一些数字逻辑推理(非技术)
    [实战演练]腾讯2013年校招软件开发类笔试题目(选择题部分)
    [实战演练]史上最长最醒目的队名
    [Linux]在linux中,常常用到ctrl和其他按键组合,常用的有哪些及意义呢
    [linux] grep awk sort uniq学习
    [IDEA] 快捷键学习
  • 原文地址:https://www.cnblogs.com/Defry/p/4576914.html
Copyright © 2020-2023  润新知