java操作zip文件
思路:
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文件的更多相关文章
- Java使用基本JDK操作ZIP文件以及zip文件的加密、解密等功能
Java使用基本JDK操作ZIP文件 http://blog.csdn.net/zhyh1986/article/details/7723649 Java解压和压缩带密码的zip文件 http://b ...
- Java处理ZIP文件的解决方案——Zip4J(不解压直接通过InputStream形式读取其中的文件,解决中文乱码)
一.JDK内置操作Zip文件其实,在JDK中已经存在操作ZIP的工具类:ZipInputStream. 基本使用: public static Map<String, String> re ...
- Java操作属性文件,支持新增或更新多个属性
Java操作属性文件.支持新增或更新多个属性 一.更新或新增单个属性的方法 /** * 写入properties信息 * @param filePath 绝对路径(包含文件名称和后缀名) * @par ...
- Python操作Zip文件
Python操作Zip文件 需要使用到zipfile模块 读取Zip文件 随便一个zip文件,我这里用了bb.zip,就是一个文件夹bb,里面有个文件aa.txt. import zipfile # ...
- java 生成zip文件并导出
总结一下,关于Java下载zip文件并导出的方法,浏览器导出. String downloadName = "下载文件名称.zip"; downloadName = Browser ...
- Java操作zip压缩和解压缩文件工具类
需要用到ant.jar(这里使用的是ant-1.6.5.jar) import java.io.File; import java.io.FileInputStream; import java.io ...
- java 操作excel 文件
JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该API非Windows操作系统也可以通过 ...
- 使用commons-compress操作zip文件(压缩和解压缩)
http://www.cnblogs.com/luxh/archive/2012/06/28/2568758.html Apache Commons Compress是一个压缩.解压缩文件的类库. 可 ...
- Java操作xml文件
Bbsxml.java public class Bbsxml { private String imgsrc; private String title; private String url; p ...
随机推荐
- 基于cocos2d-x-3.2学习Box2D(一)
cocos版本号:cocos2d-x-3.2 环境:Win7+VS2013 因为一些太底层的实现我如今的能力学习不到,仅仅能做一些简单的笔记,供以后翻阅.假设别人可以得到帮助,莫大的荣幸. 一.创建世 ...
- 实战c++中的vector系列--emplace_back造成的引用失效
上篇将了对于struct或是class为何emplace_back要优越于push_back,可是另一些细节没有提及.今天就谈一谈emplace_back造成的引用失效. 直接撸代码了: #inclu ...
- java入门之——对象转型
对象的类型转换是我们在编程的时候常常会遇到的,java平台也是如此.比方一些基本类型的数据转型和复合数据的转换. 举例 java语言中主要分为向上转型和向下转型,怎样来了解和掌握这两者转型的关系呢?首 ...
- 我怎么在AD里面找到已经改名的Administrator账户?
近期有博友问我一个问题,他是一个企业里面的IT管理员,他非常苦恼.他是一个新手,之前管理员交接的时候,没有交接更改的管理员username和password.他如今不知道哪个才是系统之前内置的admi ...
- 【剑指Offer】俯视50题之31 - 40题
面试题31连续子数组的最大和 面试题32从1到n整数中1出现的次数 面试题33把数组排成最小的数 面试题34丑数 面试题35第一个仅仅出现一次的字符 面试题36数组中的逆序对 面试题37两个链表的第一 ...
- 2014阿里巴巴WEB前端实习生在线笔试题
2014年3月31日晚,我怀着稍微忐忑的心情(第一次在线笔试^_^!!)进行了笔试.阿里巴巴的笔试题共同拥有10道,差点儿包括了Web前端开发的各个方面,有程序题.有叙述题.时间很紧张,仅仅完毕了大概 ...
- SEO 搜索引擎优化培训01
百度搜索风云榜:http://top.baidu.com/boards 页面上的因素:对搜索引擎而言
- ZOJ Design the city LCA转RMQ
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- go5--数组
package main /* 数组Array 定义数组的格式:var <varName> [n]<type>,n>=0 数组长度也是类型的一部分,因此具有不同长度的数组 ...
- idea mac 控制台中文乱码
参考:https://blog.csdn.net/lheangus/article/details/48915357 修改内容 -Dfile.encoding=UTF-8