随笔-2  评论-6  文章-5  trackbacks-0

忙了一天就为了把几个文件变成zip包:
发现:1,java的zip中文有问题用ant的
             2,只能生成zip,不能往原有的zip里面添加东西

/**
  *
  * @param list 准备压缩文件bean列表,bean里面是压缩文件的路进和名字
  * @param outDir 输出路径
  * @param outName  输出文件名
  */
 
 public static void zipFilesAntJar(List list,String outDir,String outName){
  FileOutputStream fout;
  org.apache.tools.zip.ZipOutputStream zipOut;
  try {
   fout = new FileOutputStream(outDir+outName);
   
   zipOut = new org.apache.tools.zip.ZipOutputStream(fout);
   for (Iterator iter = list.iterator(); iter.hasNext();) {
    FileBean fb = (FileBean) iter.next();
    
     FileInputStream fin = new FileInputStream(fb.getPathName());
     
     zipOut.setEncoding("GBK");
     zipOut.putNextEntry(new org.apache.tools.zip.ZipEntry(fb.getName()));
     
     int number = 0;
     byte[] b = new byte[1024];
     while((number=fin.read(b))!=-1){
      zipOut.write(b, 0, number);
     }
     zipOut.flush();
     fin.close();
    
   }
   zipOut.close();
   fout.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
 }

public class FileBean {
 private String path;
 private String name;
 
 public FileBean(String path,String name){
  this.path = path;
  this.name = name;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getPath() {
  return path;
 }
 public void setPath(String path) {
  this.path = path;
 }
 public String getPathName(){
  return path+name;
 }
}

 

posted on 2007-03-27 17:19 tornado 阅读(299) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: