java 创建压缩包_用Java创建ZIP压缩文件
发布日期:2021-06-24 10:43:34 浏览次数:5 分类:技术文章

本文共 951 字,大约阅读时间需要 3 分钟。

1.[代码][Java]代码

/**

* 压缩文件夹

* @param sourceDIR 文件夹名称(包含路径)

* @param targetZipFile 生成zip文件名

* @author liuxiangwei

*/

public static void zipDIR(String sourceDIR, String targetZipFile) {

try {

FileOutputStream target = new FileOutputStream(targetZipFile);

ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(target));

int BUFFER_SIZE = 1024;

byte buff[] = new byte[BUFFER_SIZE];

File dir = new File(sourceDIR);

if (!dir.isDirectory()) {

throw new IllegalArgumentException(sourceDIR+" is not a directory!");

}

File files[] = dir.listFiles();

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

FileInputStream fi = new FileInputStream(files[i]);

BufferedInputStream origin = new BufferedInputStream(fi);

ZipEntry entry = new ZipEntry(files[i].getName());

out.putNextEntry(entry);

int count;

while ((count = origin.read(buff)) != -1) {

out.write(buff, 0, count);

}

origin.close();

}

out.close();

} catch (IOException e) {

throw new MsgException("");

}

}

转载地址:https://blog.csdn.net/weixin_32256355/article/details/114893091 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java typedarray_TintTypedArray.java
下一篇:java单元测试断言_单元测试+断言

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月17日 16时49分12秒