files:是所有文件的目录destZip:是生成压缩文件所存放的目录 public static void zipFiles(Collection<String> files,String destZip){ FileOutputStream fout=null; ZipOutputStream zout=null; int len=-1; byte[] buf=new byte[1024]; try{ FileInputStream fin=null; fout=new FileOutputStream(destZip); zout=new ZipOutputStream(fout); zout.setEncoding("gbk"); for(String reportPath:files){ File repostFile= new File(reportPath); String reportName= reportFile.getName(); ZipEntry zEntry = new ZipEntry(reportName); zout.putNextEntry(zEntry); fin=new FileInputStream(reportFile); while(len=fin.read(buf))!=-1){ zout.write(buf,0,len); } fin.close(); } }catch(Exception e){ e.printStackTrace(); }finally{ try{ fout.close(); }catch(IOExecption e){ e.printStackTrace(); }finally{ } } } }
时间: 2024-11-07 13:06:39