附件备份成zip包下载 和 zip包附件恢复

 

//附件备份成zip包下载

 

static final int BUFFER = 2048;//附件备份
     public  void backupFJToFile() {
      String sourcePath="";//源文件所在位置
       String destFilePath="";//备份到的位置
         if (Path.isLinux) {
          sourcePath = ServletActionContext.getServletContext().getRealPath("/upload");
          destFilePath=ServletActionContext.getServletContext().getRealPath("/bak")+"/upload.zip";
    } else {
     sourcePath = ServletActionContext.getServletContext().getRealPath("//upload");
     destFilePath = ServletActionContext.getServletContext().getRealPath("//bak")+"//upload.zip";
    }
        try {
           BufferedInputStream origin = null;
           FileOutputStream dest = new FileOutputStream(destFilePath);
           ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
           byte data[] = new byte[BUFFER];
           File f = new File(sourcePath);
           String[] files = f.list();
           zip(dest,out,f);
           out.close();
        } catch(Exception e) {
           e.printStackTrace();
        }
        this.down(destFilePath);
     }

 


 public void down(String destFilePath){
      HttpServletResponse response=ServletActionContext.getResponse();
      File f = new File(destFilePath);
      BufferedInputStream br=null;
      try {
     br = new BufferedInputStream(new FileInputStream(f));
   } catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
      byte[] buf = new byte[1024];
      int len = 0;
       response.reset();
          response.setContentType("application/x-download");//设置为下载application/x-download
          try {
    destFilePath = URLEncoder.encode(destFilePath,"UTF-8");
   } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
          response.addHeader("Content-Disposition","attachment;filename="+destFilePath);
   try {
    OutputStream out = response.getOutputStream();
    while((len = br.read(buf)) >0)
    out.write(buf,0,len);
    br.close();
    out.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } 
     }

 


     public void zip(FileOutputStream dest,ZipOutputStream out,File f)
     {
       byte data[] = new byte[BUFFER];
       BufferedInputStream origin = null;
       try {
      String[] files = f.list();
         for (int i=0; i<files.length; i++) {
          File subf=null;
          if (Path.isLinux) {
             subf = new File(f.getPath() + "/" + files[i].trim());
     } else {
        subf = new File(f.getPath() + "//" + files[i].trim());
     }
          // File subf = new File(f.getPath() + "//" + files[i].trim());
           if(subf.isDirectory())
           {
             zip(dest,out,subf);
           }
           else
           {
            String fpath="";
            if (Path.isLinux) {
             fpath = f.getPath() + "/" + files[i].trim();
     } else {
       fpath = f.getPath() + "//" + files[i].trim();
     }
             //String fpath = f.getPath() + "//" + files[i].trim();
             FileInputStream fi = new FileInputStream(fpath);
             origin = new BufferedInputStream(fi, BUFFER);
             ZipEntry entry = new ZipEntry(fpath);
             out.putNextEntry(entry);
             int count;
             while((count = origin.read(data, 0, BUFFER)) != -1)
             {
                  out.write(data, 0, count);
             }
             origin.close();
           }
         }
       } catch (Exception e) {
           e.printStackTrace();
       }
     }

 

 

 

 

//zip包附件恢复

public void restoreFJFromFile(File f) {//附件恢复
      try {
         String filePath="";
            ZipFile zipFile =new ZipFile(f) ;
            Enumeration emu = zipFile.entries();
            int i=0;
            while(emu.hasMoreElements()){
                ZipEntry entry = (ZipEntry)emu.nextElement();
                //会把目录作为一个file读出一次,所以只建立目录就可以,之下的文件还会被迭代到。
                if (entry.isDirectory())
                {
                    new File(filePath + entry.getName()).mkdirs();
                    continue;
                }
                BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
                System.out.println( entry.getName()+"path22222");
                File file = new File(filePath + entry.getName());
                //加入这个的原因是zipfile读取文件是随机读取的,这就造成可能先读取一个文件
                //而这个文件所在的目录还没有出现过,所以要建出目录来。
                File parent = file.getParentFile();
                if(parent != null && (!parent.exists())){
                    parent.mkdirs();
                }
                FileOutputStream fos = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(fos,BUFFER);          
                int count;
                byte data[] = new byte[BUFFER];
                while ((count = bis.read(data, 0, BUFFER)) != -1)
                {
                    bos.write(data, 0, count);
                }
                bos.flush();
                bos.close();
                bis.close();
            }
            zipFile.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

     }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值