思路:

  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. Python获得文件时间戳 异常访问监控 邮件定时提醒

    Python获得文件时间戳  异常访问监控 邮件定时提醒

  2. 9 Range 实用操作

    9.1 剪切.复制和粘贴来移动数据 sourceRange.Cut [Destination] 如果指定Destination,相当于Ctrl^X(sourceRange) & Ctrl^V( ...

  3. pycharm打开多个项目并存

    问题: 有时我们需要打开多个项目,而现在的做法是:  原有的a项目不动,新打开一个pycharm来打开b项目, 或者 在原有的a项目中打开b项目并覆盖a项目,即a项目与b项目不能共存 需求: 有时我们 ...

  4. RDA 多屏参流程

    一.RDA MAKEFILE的本地变量 在介绍多屏参之前,先看一下./code/env.conf的包含过程,通过./code/Makefile.project加载,env.conf中所有的变量,都变为 ...

  5. C#面向过程之编译原理、变量、运算符

    .net基础:.net与C# .net是一个平台 c#是一门语言 .net的用途a.桌面应用程序 b.网站应用程序 c.专业游戏开发(XBOX360) d.嵌入式设备软件开发 e.智能手机APP开发 ...

  6. jQuery中contains和has的区别

    jQuery中contains和has的区别 根据不同的内容和属性可以准确定位到需要找的属性 如何根据内容筛选标签?:contains        匹配包含给定的文本元素$("div:co ...

  7. mldonkey设置!看图(转载)

    转自:http://www.nenew.net/ubuntu-mldonkey-application.html 这里不是争论区,amule和mldonkey各有各好,看个人爱好,没有高下,都是程序员 ...

  8. ffmpeg 有用命令 (转载)

    转自:http://blog.csdn.net/simongyley/article/details/9984167 1.将h264文件解码为yuv文件 ffmpeg -i file.h264 fil ...

  9. va_start和va_end使用详解(转载)

    转自:http://www.cnblogs.com/hanyonglu/archive/2011/05/07/2039916.html 本文主要介绍va_start和va_end的使用及原理. 在以前 ...

  10. 关于Java泛型的?和 T 的区别

    java中的?号指未知的类型:而T指具体类型 泛型问号(?)未知的类型就是可以指定当前问号(?)所代表的类,可以指定上限(extends)和下限(super) 泛型T指已经具体知道了类型,就是不能指定 ...