思路:

  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. Cookie对象的特点

    1.存储少量不重要的数据2.存储在客户端的文本文件中(必须设置有效期,否则不被存储)3.安全性差4.存储的数据类型--字符串5.浏览器窗口无关,但与访问的站点相关6.具体特定的过期时间和日期7.在客户 ...

  2. Scala-LIST/Tuple/Map

    环境: CentOS 6.3 LIST(列表) 代码: $ cat list.scala var mylist = List(1,2,3) println(mylist) var mylist1 = ...

  3. HDOJ 5381 The sum of gcd 莫队算法

    大神题解: http://blog.csdn.net/u014800748/article/details/47680899 The sum of gcd Time Limit: 2000/1000 ...

  4. LVS 负载均衡 (VS/DR模式 与 VS/TUN 模式)

    一.VS/DR模式 ①.客户端将请求发往前端的负载均衡器,请求报文源地址是CIP,目标地址为VIP. ②.负载均衡器收到报文后,发现请求的是在规则里面存在的地址,那么它将目标MAC改为了RIP的MAC ...

  5. Java集合类汇总记录--JDK篇

    接口类图 Java Collection由两套并行的接口组成,一套是Collection接口,一套是Map接口.例如以下图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkb ...

  6. MySQL-数据库创建与删除

    创建数据库 在MySQL中,数据库是用于存储和操作诸如表,数据库视图,触发器,存储过程等数据的对象的集合. 要在MySQL中创建数据库,使用CREATE DATABASE语句,如下: CREATE D ...

  7. Flask中的ThreadLocal本地线程,上下文管理

    先说一下和flask没有关系的: 我们都知道线程是由进程创建出来的,CPU实际执行的也是线程,那么线程其实是没有自己独有的内存空间的,所有的线程共享进程的资源和空间,共享就会有冲突,对于多线程对同一块 ...

  8. IDEA kafka producer数据输出缓慢 和 kafka consumer 报错的处理

    问题1. IDEA 中Kafa_Producer程序数据输出缓慢 但不报错 问题2. Kafa_Consumer程序报错: 17/11/10 11:31:11 ERROR ReceiverTracke ...

  9. Runtime ----- 带你上道

    在IOS开发和学习过程中,我们经常会接触到一个词: Runtime  .很多开发者对之既熟悉又陌生,基本都是浅尝辄止,达不到灵活使用的水平(话说开发中也确实不经常用..)本文和大家一起研究一下,Run ...

  10. FPGA基础入门篇(四) 边沿检测电路

    FPGA基础入门篇(四)--边沿检测电路 一.边沿检测 边沿检测,就是检测输入信号,或者FPGA内部逻辑信号的跳变,即上升沿或者下降沿的检测.在检测到所需要的边沿后产生一个高电平的脉冲.这在FPGA电 ...