思路:

  1)、读取zip中的文件并将除了重名文件之外的文件转存到中转zip文件中。

  2)、往中转文件中插入txt文件。

  3)、删除原zip文件。

  4)、将中转zip文件重命名为原zip文件。

前提,txt和zip文件需要存在,本代码中未加判断。

具体代码见下:

package zip;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class UpdateZip {

  private static final byte[] BUFFER = new byte[4096 * 1024];

  public static void main(String[] args) throws Exception {
    //路径
    String path = "f:";
    //待插入zip
    String zipname = "a.zip";
    //待插入文件
    String txtname = "c.txt";
    //构建zip和文件准确路径
    String oldzip = path+"/"+zipname;
    //构建中转zip路径
    String newzip = path+"/b.zip";
    String txt = path+"/"+txtname;
    //获取文件
    ZipFile war = new ZipFile(path+"/"+zipname);
    ZipOutputStream append = new ZipOutputStream(new FileOutputStream(newzip));
    //将待插入zip中文件复制到中转zip中
    copytonew(war,append,txtname);
    //往中转zip中插入待插入文件
    insertnewfile(append,txt);
    close(war,append);
    //删除原来zip
    delete(oldzip);
    //将中转zip重命名为原来zip
    rename(oldzip,newzip);
  }
  public static void close(ZipFile war,ZipOutputStream append){
    try {
      war.close();
      append.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  public static void delete(String oldzip){
    File file = new File(oldzip);
    file.delete();
    System.out.println("delete:"+oldzip);
  }
  public static void rename(String oldzip,String newzip){
    File newfile = new File(oldzip);
    File file = new File(newzip);
    file.renameTo(newfile);
    System.out.println("rename:"+oldzip+"------》"+newzip);
  }
  public static void insertnewfile(ZipOutputStream append,String txt){
    File file = new File(txt);
    FileInputStream fis = null; 
    BufferedInputStream bis = null; 
    try { 
      fis = new FileInputStream(file); 
      bis = new BufferedInputStream(fis);
      ZipEntry entry = new ZipEntry(file.getName());
      append.putNextEntry(entry);
      int length; 
      byte[] buffer = new byte[4096]; 
      while ((length = bis.read(buffer)) != -1) { 
        append.write(buffer, 0, length);
        System.out.println("insert:"+txt);
      } 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } finally { 
      try { 
        bis.close(); 
        fis.close(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
    } 
  }
  public static void copy(InputStream input, OutputStream output) throws IOException {
    int bytesRead;
    while ((bytesRead = input.read(BUFFER))!= -1) {
      output.write(BUFFER, 0, bytesRead);
    }
  }
  public static void copytonew(ZipFile war,ZipOutputStream append,String txtname){
    Enumeration<? extends ZipEntry> entries = war.entries();
    while (entries.hasMoreElements()) {
      ZipEntry e = entries.nextElement();
      if(!e.getName().equals(txtname)){
        System.out.println("copy: " + e.getName());
        try {
          append.putNextEntry(e);
          if (!e.isDirectory()) {
            copy(war.getInputStream(e), append);
          }
          append.closeEntry();
        } catch (IOException e1) {
          e1.printStackTrace();
        } 
      }
    }
  }
}

当然,如果你们有好的方法欢迎指导。

posted on 2015-08-06 17:16  程序员丁先生  阅读(934)  评论(0编辑  收藏  举报