java zip 添加文件类型_Java中往zip压缩包追加文件

有个需求,从某个接口下载的一个zip压缩包,往里面添加一个说明文件。搜索了一下,没有找到往zip直接添加文件的方法,最终解决方法是先解压、再压缩。

具体过程如下:

1、一个zip文件的压缩和解压工具类

压缩和解压工具类来自https://www.iteye.com/blog/songfeng-123-2243016,但是原文代码因为用的是java自带的java.util.zip,有中文乱码的bug,所以需要修改部分代码,并且修改为引用org.apache.tools.zip.*,pom.xml加入依赖包,如下:

org.apache.ant

ant

1.10.7

工具类代码:

package com.example.demo;

import java.io.*;

import java.util.arraylist;

import java.util.enumeration;

import java.util.list;

import java.util.zip.zipexception;

import org.apache.tools.zip.*;

public class ziputil {

private static int buffersize = 1024;

/**

* 压缩

*

* @param paths

* @param filename

*/

public static void zip(list paths, string filename) {

zipoutputstream zos = null;

try {

zos = new zipoutputstream(new fileoutputstream(filename));

for (string filepath : paths) {

// 递归压缩文件

file file = new file(filepath);

string relativepath = file.getname();

if (file.isdirectory()) {

relativepath += file.separator;

}

zipfile(file, relativepath, zos);

}

} catch (ioexception e) {

e.printstacktrace();

} finally {

try {

if (zos != null) {

zos.close();

}

} catch (ioexception e) {

e.printstacktrace();

}

}

}

public static void zipfile(file file, string relativepath, zipoutputstream zos) {

inputstream is = null;

try {

if (!file.isdirectory()) {

zipentry zp = new zipentry(relativepath);

zos.putnextentry(zp);

is = new fileinputstream(file);

byte[] buffer = new byte[buffersize];

int length = 0;

while ((length = is.read(buffer)) >= 0) {

zos.write(buffer, 0, length);

}

zos.setencoding("gbk");//解决文件名中文乱码

zos.flush();

zos.closeentry();

} else {

string temppath = null;

for (file f : file.listfiles()) {

temppath = relativepath + f.getname();

if (f.isdirectory()) {

temppath += file.separator;

}

zipfile(f, temppath, zos);

}

}

} catch (ioexception e) {

e.printstacktrace();

} finally {

try {

if (is != null) {

is.close();

}

} catch (ioexception e) {

e.printstacktrace();

}

}

}

/**

* 解压缩

*

* @param filename

* @param path

*/

public static list unzip(string filename, string path) {

fileoutputstream fos = null;

inputstream is = null;

list filepaths = new arraylist();

try {

zipfile zf = new zipfile(new file(filename));

enumeration en = zf.getentries();

while (en.hasmoreelements()) {

zipentry zn = (zipentry) en.nextelement();

if (!zn.isdirectory()) {

is = zf.getinputstream(zn);

file f = new file(path + zn.getname());

file file = f.getparentfile();

file.mkdirs();

fos = new fileoutputstream(path + zn.getname());

int len = 0;

byte bufer[] = new byte[buffersize];

while (-1 != (len = is.read(bufer))) {

fos.write(bufer, 0, len);

}

fos.close();

filepaths.add(path + zn.getname());

}

}

} catch (zipexception e) {

e.printstacktrace();

} catch (ioexception e) {

e.printstacktrace();

} finally {

try {

if (null != is) {

is.close();

}

if (null != fos) {

fos.close();

}

} catch (ioexception e) {

e.printstacktrace();

}

}

return filepaths;

}

}

2、测试

有如下目录结构:

d:\测试\文档.zip

d:\测试\说明.pdf

把“说明.pdf”添加到“文档.zip”里面,生成一个新压缩包“文档(新).zip”。

package com.example.demo;

import java.io.file;

import java.util.list;

public class ziputiltest {

public static void main(string[] args) {

//解压

list files = ziputil.unzip("d:/测试/文档.zip", "d:/测试/");

//集合添加文件

files.add("d:/测试/说明.pdf");

//压缩

ziputil.zip(files,"d:/测试/文档(新).zip");

//保留说明.pdf

files.remove(files.size()-1);

//删除上面解压出来的文件

for(string f : files){

file file = new file(f);

if(file.exists()){

file.delete();

}

}

}

}

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值