Java压缩文件

来源:互联网 发布:京东商城与淘宝的区别 编辑:程序博客网 时间:2024/05/17 00:51

 

 

 

public class FileService
{
              
    /**
     * 压缩文件
     * @param strFilePath
     */
 public static void doZipFile(String idKey,String sourceFilePath,String targetFilePath,String zipName)throws IOException,FileNotFoundException{
      //定义压缩后的文件名称
      if(StringUtils.isEmpty(sourceFilePath)|| StringUtils.isEmpty(targetFilePath) || StringUtils.isEmpty(zipName)){
               return;
      }
     ZipOutputStream zos = null;
     try {
           File resourcefile = new File(sourceFilePath+"\\"+idKey);
           File targetfile = null;
           if("type1.zip".equals(zipName)){
                 targetfile  = new File(targetFilePath+"\\type1\\"+idKey);
           }else if("type2.zip".equals(zipName)){
                 targetfile  = new File(targetFilePath+"\\type2\\"+idKey);
           }
      if(!resourcefile.exists()){
            resourcefile.mkdirs();
      }
      if(!targetfile.exists()){
            targetfile.mkdirs();
      }
   FileOutputStream fos =  new FileOutputStream(targetfile+"\\"+zipName);
   zos = new ZipOutputStream(new BufferedOutputStream(fos));
   zos.setEncoding("GBK");
   createZipFile(zos,resourcefile,"");
  } catch (FileNotFoundException e) {
        EMPLog.log("FileService->压缩文件", EMPLog.ERROR, 0, "失败: "+e.getMessage());
  }finally{
   if(zos!=null){
    try {
          zos.close();
    } catch (IOException e) {
          EMPLog.log("FileService->压缩文件", EMPLog.ERROR, 0, "失败: "+e.getMessage());
    }
   }
  }
       EMPLog.log("FileService->压缩文件", EMPLog.INFO, 0, "成功");
    }
   
    public static void  createZipFile(ZipOutputStream out,File file,String dir) throws IOException,
     FileNotFoundException{
     //如果当前的是文件夹,则进行进一步处理
  if(file.isDirectory()){
        //得到文件列表信息
        File[] files = file.listFiles();
   try {
         out.putNextEntry(new ZipEntry(dir+"/"));
         dir = dir.length()==0?"":dir+"/";
         //循环将文件夹中的文件打包
    for(int i=0;i<files.length;i++){
          createZipFile(out,files[i],new String((dir+files[i].getName())));
    }
   } catch (IOException e) {
         EMPLog.log("FileService->压缩文件", EMPLog.ERROR, 0, "失败: "+e.getMessage());
   }
  }else{//当前的是文件,打包处理
   
   //文件输入流
   FileInputStream fis = null;
   try {
     fis = new FileInputStream(file);
    out.putNextEntry(new ZipEntry(dir));
    
    //进行写操作
    int n=0;
    byte[] buffer = new byte[1024];
    while((n=fis.read(buffer))>0){
     out.write(buffer, 0,n);
    }
    
   } catch (FileNotFoundException e) {
    EMPLog.log("FileService->压缩文件", EMPLog.ERROR, 0, "失败: "+e.getMessage());
   }catch (IOException e) {
    EMPLog.log("FileService->压缩文件", EMPLog.ERROR, 0, "失败: "+e.getMessage());
   }finally{
    //关闭输入流
    if(null != fis){
     try {
      fis.close();
     } catch (IOException e) {
      EMPLog.log("FileService->压缩文件", EMPLog.ERROR, 0, "失败: "+e.getMessage());
     }
    }
   } 
  }
    }
   
    public static void main(String args[]){    
     try {
   FileService.doZipFile("140","D:\\upload\\type1", "D:\\upload", "type1.zip");
  } catch (FileNotFoundException e) {
   System.out.println("压缩失败...");
  } catch (IOException e) {
   System.out.println("压缩失败...");
  }
    }
   
}

0 0
原创粉丝点击