现在的位置: 首页 > 综合 > 正文

本地读取excel并打包成zip

2012年02月24日 ⁄ 综合 ⁄ 共 1578字 ⁄ 字号 评论关闭

ant.jar下载地址

/**
  * @author yzh 多个excel导出后打包成zip文件
  * @throws Exception 
  */
 public static void main(String[] args) throws Exception {
      List<String> list = new ArrayList<String>();
      list.add("靠.xls");
      list.add("b.xls");
      list.add("c.xls");
      String fileName = "excel本地读取打包.zip";
      String path = "E:\\";
      Ziptest cc = new Ziptest ();
      cc.getZip(list, path, fileName);
      System.out.println("生成zip");
      //cc.deleteFile(path+fileName+".zip");
      //System.out.println("删除zip");
 }

/**
  * @author yzh    将多个文件 打包处理(.zip)
  * @param list    要打包的文件名称集合
  * @param path     路径
  * @param fileName    zip文件名
  * @throws Exception
  */
 public void getZip(List<String> list, String path, String fileName)
   throws Exception {
      byte[] buffer = new byte[1024];
      if(!list.isEmpty() && path!=null&& fileName!=null){
       //使用ant.jar中zip.*  
       ZipOutputStream out = new ZipOutputStream(new FileOutputStream(path
             + fileName));
       for (int j = 0; j < list.size(); j++) {
            String name = list.get(j).toString();
            System.out.println(name);
            FileInputStream fis = new FileInputStream(path + name);
            out.putNextEntry(new org.apache.tools.zip.ZipEntry(name));
            int len;
            while ((len = fis.read(buffer)) > 0) {
                 out.write(buffer, 0, len);
            }
            out.setEncoding("gbk");  //乱码问题
            out.setComment("文件的读取");   
            out.closeEntry();
            fis.close();
       }
       out.close();
      }
 }

 

/**
  * @author yzh 删除文件
  * @param targetPath
  * @throws IOException
  */
 public void deleteFile(String path) throws IOException {
     File targetFile = new File(path);
      if (targetFile.isDirectory()) {
           FileUtils.deleteDirectory(targetFile);
      } else if (targetFile.isFile()) {
           targetFile.delete();
      } 

}

 

抱歉!评论已关闭.