基于Java向zip压缩包追加文件
这篇文章主要介绍了基于Java向zip压缩包追加文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
有个需求,从某个接口下载的一个zip压缩包,往里面添加一个说明文件。搜索了一下,没有找到往zip直接添加文件的方法,最终解决方法是先解压、再压缩。
具体过程如下:
1、一个zip文件的压缩和解压工具类
pom.xml加入依赖包,如下:
org.apache.ant ant 1.10.7
工具类代码:
packagecom.example.demo;
importjava.io.*;
importjava.util.ArrayList;
importjava.util.Enumeration;
importjava.util.List;
importjava.util.zip.ZipException;
importorg.apache.tools.zip.*;
publicclassZipUtil{
privatestaticintBUFFERSIZE=1024;
/**
*压缩
*
*@parampaths
*@paramfileName
*/
publicstaticvoidzip(Listpaths,StringfileName){
ZipOutputStreamzos=null;
try{
zos=newZipOutputStream(newFileOutputStream(fileName));
for(StringfilePath:paths){
//递归压缩文件
Filefile=newFile(filePath);
StringrelativePath=file.getName();
if(file.isDirectory()){
relativePath+=File.separator;
}
zipFile(file,relativePath,zos);
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(zos!=null){
zos.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
publicstaticvoidzipFile(Filefile,StringrelativePath,ZipOutputStreamzos){
InputStreamis=null;
try{
if(!file.isDirectory()){
ZipEntryzp=newZipEntry(relativePath);
zos.putNextEntry(zp);
is=newFileInputStream(file);
byte[]buffer=newbyte[BUFFERSIZE];
intlength=0;
while((length=is.read(buffer))>=0){
zos.write(buffer,0,length);
}
zos.setEncoding("gbk");//解决文件名中文乱码
zos.flush();
zos.closeEntry();
}else{
StringtempPath=null;
for(Filef:file.listFiles()){
tempPath=relativePath+f.getName();
if(f.isDirectory()){
tempPath+=File.separator;
}
zipFile(f,tempPath,zos);
}
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(is!=null){
is.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
/**
*解压缩
*
*@paramfileName
*@parampath
*/
publicstaticListunzip(StringfileName,Stringpath){
FileOutputStreamfos=null;
InputStreamis=null;
ListfilePaths=newArrayList();
try{
ZipFilezf=newZipFile(newFile(fileName));
Enumerationen=zf.getEntries();
while(en.hasMoreElements()){
ZipEntryzn=(ZipEntry)en.nextElement();
if(!zn.isDirectory()){
is=zf.getInputStream(zn);
Filef=newFile(path+zn.getName());
Filefile=f.getParentFile();
file.mkdirs();
fos=newFileOutputStream(path+zn.getName());
intlen=0;
bytebufer[]=newbyte[BUFFERSIZE];
while(-1!=(len=is.read(bufer))){
fos.write(bufer,0,len);
}
fos.close();
filePaths.add(path+zn.getName());
}
}
}catch(ZipExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(null!=is){
is.close();
}
if(null!=fos){
fos.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
returnfilePaths;
}
}
2、测试
有如下目录结构:
D:\测试\文档.zip
D:\测试\说明.pdf
把“说明.pdf”添加到“文档.zip”里面,生成一个新压缩包“文档(新).zip”。
packagecom.example.demo;
importjava.io.File;
importjava.util.List;
publicclassZipUtilTest{
publicstaticvoidmain(String[]args){
//解压
Listfiles=ZipUtil.unzip("D:/测试/文档.zip","D:/测试/");
//集合添加文件
files.add("D:/测试/说明.pdf");
//压缩
ZipUtil.zip(files,"D:/测试/文档(新).zip");
//保留说明.pdf
files.remove(files.size()-1);
//删除上面解压出来的文件
for(Stringf:files){
Filefile=newFile(f);
if(file.exists()){
file.delete();
}
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。