思路:

  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();
        }
      }
    }
  }
}

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

java操作zip文件的更多相关文章

  1. Java使用基本JDK操作ZIP文件以及zip文件的加密、解密等功能

    Java使用基本JDK操作ZIP文件 http://blog.csdn.net/zhyh1986/article/details/7723649 Java解压和压缩带密码的zip文件 http://b ...

  2. Java处理ZIP文件的解决方案——Zip4J(不解压直接通过InputStream形式读取其中的文件,解决中文乱码)

    一.JDK内置操作Zip文件其实,在JDK中已经存在操作ZIP的工具类:ZipInputStream. 基本使用: public static Map<String, String> re ...

  3. Java操作属性文件,支持新增或更新多个属性

    Java操作属性文件.支持新增或更新多个属性 一.更新或新增单个属性的方法 /** * 写入properties信息 * @param filePath 绝对路径(包含文件名称和后缀名) * @par ...

  4. Python操作Zip文件

    Python操作Zip文件 需要使用到zipfile模块 读取Zip文件 随便一个zip文件,我这里用了bb.zip,就是一个文件夹bb,里面有个文件aa.txt. import zipfile # ...

  5. java 生成zip文件并导出

    总结一下,关于Java下载zip文件并导出的方法,浏览器导出. String downloadName = "下载文件名称.zip"; downloadName = Browser ...

  6. Java操作zip压缩和解压缩文件工具类

    需要用到ant.jar(这里使用的是ant-1.6.5.jar) import java.io.File; import java.io.FileInputStream; import java.io ...

  7. java 操作excel 文件

    JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该API非Windows操作系统也可以通过 ...

  8. 使用commons-compress操作zip文件(压缩和解压缩)

    http://www.cnblogs.com/luxh/archive/2012/06/28/2568758.html Apache Commons Compress是一个压缩.解压缩文件的类库. 可 ...

  9. Java操作xml文件

    Bbsxml.java public class Bbsxml { private String imgsrc; private String title; private String url; p ...

随机推荐

  1. TextView设置成仅仅读

    TextView设置成仅仅读 方法一:代理 - (BOOL)textViewShouldBeginEditing:(UITextView *)textView { return NO; } 方法二:设 ...

  2. ios学习8_KVC和字典转模型

    Key Value Coding是cocoa的一个标准组成部分,它能让我们能够通过name(key)的方式訪问属性,某些情况下极大地简化了代码.可称之为cocoa的大招. 例如以下的样例: 使用KVC ...

  3. 【Unix编程】进程间通信(IPC)

    进程间通信(IPC,InterProcess Communication)是指在不同进程之间传播或交换信息.IPC的方式通常有管道(包括无名管道和命名管道).消息队列.信号量.共享存储.Socket. ...

  4. hdu 1258 Sum It Up (dfs+路径记录)

    pid=1258">Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  5. JButton的setRollover出现的奇怪问题

    设置了setRollover,可以正常出现状态但是却不会回到默认状态. 研究了一下才发现,repaint的时候不会清除背板而是覆盖上去的, 所以如果原图是透明图就会出现状态不变的情况

  6. CPU上电时序详细分析

    首先是RTC电源,这部分电力是永远不关闭的,除非电池(纽扣电池)没电并且没接任何外部电源(比如电池和电源适配器). RTC用以保持机器内部时钟的运转和保证CMOS配置信息在断电的情况下不丢失:其次,在 ...

  7. Java游戏之碰撞检测

    在进行Java游戏开发时,我们经常会遇到碰撞检测的问题.如坦克大战中,炮弹与坦克相遇发生爆炸:守卫者游戏中,守卫者发射的箭与怪物相遇使怪物失血:打飞机游戏中,飞机发送的子弹与敌机相遇干掉敌机.这些都需 ...

  8. bzoj1202: [HNOI2005]狡猾的商人(并查集 差分约束)

    1202: [HNOI2005]狡猾的商人 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4127  Solved: 1981[Submit][Sta ...

  9. 双栈排序 2008年NOIP全国联赛提高组(二分图染色)

    双栈排序 2008年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master     题目描述 Description Tom最近在研究一个有 ...

  10. java Class.getResource和ClassLoader.getResource

    http://www.cnblogs.com/wang-meng/p/5574071.html http://blog.csdn.net/earbao/article/details/50009241 ...