Opening the file with a ZipOutputStream overwrites it. Is there a way to keep the file and just add new entries?

解决方案

The function renames the existing zip file to a temporary file and then adds all entries in the existing zip along with the new files, excluding the zip entries that have the same name as one of the new files.

public static void addFilesToExistingZip(File zipFile,

File[] files) throws IOException {

// get a temp file

File tempFile = File.createTempFile(zipFile.getName(), null);

// delete it, otherwise you cannot rename your existing zip to it.

tempFile.delete();

boolean renameOk=zipFile.renameTo(tempFile);

if (!renameOk)

{

throw new RuntimeException("could not rename the file "+zipFile.getAbsolutePath()+" to "+tempFile.getAbsolutePath());

}

byte[] buf = new byte[1024];

ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));

ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

ZipEntry entry = zin.getNextEntry();

while (entry != null) {

String name = entry.getName();

boolean notInFiles = true;

for (File f : files) {

if (f.getName().equals(name)) {

notInFiles = false;

break;

}

}

if (notInFiles) {

// Add ZIP entry to output stream.

out.putNextEntry(new ZipEntry(name));

// Transfer bytes from the ZIP file to the output file

int len;

while ((len = zin.read(buf)) > 0) {

out.write(buf, 0, len);

}

}

entry = zin.getNextEntry();

}

// Close the streams

zin.close();

// Compress the files

for (int i = 0; i < files.length; i++) {

InputStream in = new FileInputStream(files[i]);

// Add ZIP entry to output stream.

out.putNextEntry(new ZipEntry(files[i].getName()));

// Transfer bytes from the file to the ZIP file

int len;

while ((len = in.read(buf)) > 0) {

out.write(buf, 0, len);

}

// Complete the entry

out.closeEntry();

in.close();

}

// Complete the ZIP file

out.close();

tempFile.delete();

}

java 压缩包添加文件,如何在Java中向现有zip文件添加条目?相关推荐

  1. android创建一个新文件,如何在android中创建一个新文件夹DCIM

    张贴在评论,我想你的代码,并它为我工作. MainActivity.java public class MainActivity extends Activity { private final st ...

  2. linux强制移除pdf密码,分享|如何在 Linux 中从一个 PDF 文件中移除密码

    今天,我碰巧分享一个受密码保护的 PDF 文件给我的一个朋友.我知道这个 PDF 文件的密码,但是我不想透露密码.作为代替,我只想移除密码并发送文件给他.我开始在因特网上查找一些简单的方法来从 PDF ...

  3. 如何在linux上压缩文件夹,如何在Linux中使用命令压缩文件和文件夹

    通常情况下,我们都是安装了zip,但验证一下也无妨.你可以运行下面的命令来安装zip sudo apt install zip unzip 现在你知道你的系统支持zip了,你可以继续阅读学习如何在Li ...

  4. Acrobat Pro DC 教程:如何在 PDF 中插入其他 PDF 文件?

    欢迎观看 Acrobat Pro DC 教程,小编带大家学习 Acrobat Pro DC 的基本工具和使用技巧,了解如何在 PDF 中插入其他 PDF文件. 在 Acrobat Pro DC 中选择 ...

  5. mysql data ibdata1_database - 如何在MySQL中收缩/清除ibdata1文件

    database - 如何在MySQL中收缩/清除ibdata1文件 我在localhost中使用MySQL作为在R中执行统计的"查询工具",也就是说,每次运行R脚本时,我创建一个 ...

  6. Microsoft Word教程:如何在 Word 中创建文档、添加和编辑文本?

    欢迎观看 Microsoft Word 教程,小编带大家学习 Microsoft Word 的使用技巧,了解如何在 Word 中创建文档.添加和编辑文本. 创建文档,打开 Word,选择「空白文档」. ...

  7. linux中如何压缩目录文件,如何在Linux中压缩文件和目录

    Zip是使用最广泛的存档文件格式,支持无损数据压缩. 压缩文件是一种数据容器,其中包含一个或多个压缩文件或目录.与未压缩的文件相比,压缩文件占用的磁盘空间更少,可以更快地从一台计算机传输到另一台计算机 ...

  8. android 大文件加密,如何在android中加密大视频文件

    我有一个应用程序,我正在使用该代码来解密已加密的文件.文件位置是"/mnt/sdcard/myfolder/test.mp4". test.mp4文件大小约为20MB.如何在and ...

  9. 如何在MyEclipse中反编译class文件

    反编译class文件,以前都是用的jd-gui.exe这个绿色软件.最近学到了如何在Myeclipse中直接查看class文件,这里记录一下. 1.下载net.sf.jadclipse_3.3.0.j ...

最新文章

  1. [转]gitHub客户端Desktop的安装使用总结 ---基础篇
  2. oss导出数据为空时怎么处理_sql数据库导出空库的搜索结果-阿里云开发者社区...
  3. BZOJ 1613: [Usaco2007 Jan]Running贝茜的晨练计划
  4. 【转载】JS获取浏览器版本信息
  5. Linux下SVN搭建与配置
  6. 探索 OpenStack 之(15):oslo.messaging 和 Cinder 中 MessageQueue 消息的发送和接收
  7. 6.2(求一个整数各位数字之和)
  8. 2017.12.1T19_B2_6
  9. SpringMVC整合Quartz实现定时任务和Spring自带Task定时任务
  10. 【附源码】计算机毕业设计JAVA家庭记账系统
  11. HTML——响应式布局
  12. 高考满分作文《细雨湿衣看不见,闲花落地听无声》 之 金庸新版
  13. 初中计算机卡片的制作教案,其他教案-贺卡的设计与制作
  14. 多态中对象的上下转型
  15. layui复选框,设置部分可选
  16. 计算机辅助普通话水平证书要考吗,目前普通话证书考试都是机器测试吗?
  17. 从生态繁荣到市场猛增:财报背后,李彦宏如何建立DuerOS矩阵
  18. 多版本node安装相关知识
  19. dede后台-系统基本参数无法保存中文/显示空白
  20. 父子组件通信,父类调用子类方法

热门文章

  1. Win10彻底关闭恢复功能、省流量终极设置
  2. 工业相机选型:相机接口
  3. mybatis中#{}和${}
  4. Beta 冲刺 (6/7)
  5. 《社交网站界面设计(原书第2版)》——1.12 传感器在交互中的作用
  6. 9.9递归和动态规划(十二)——小鸡吃米
  7. 使用篇-基于Laravel开发博客应用系列 —— 联系我们 发送邮件 队列使用(基于数据库)...
  8. .NET自动服务程序—C#
  9. 华为机试HJ100:等差数列
  10. Github-Git for windowsTortoiseGit下载与安装