网上有关压缩和解压zip包的博文一大堆,我随便找了一个。看了看,依照自己的须要改动了一下,与各位分享一下,希望各位大神指正:

package com.wangpeng.utill;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream; /**
* @author wangpeng
*/
public class ToolOfZip { /**
* 解压zip包
* @param inputPath 被解压的zip包的路径
* @param targetPath 被解压到的文件夹
* @param isClear 是否清楚已有文件夹
*/
public static void unZip(String inputPath,String targetPath, boolean isClear) {
try {
if (null == targetPath || "".equals(targetPath)) {
targetPath = inputPath.substring(0,inputPath.lastIndexOf("."));
} ZipFile zipFile = new ZipFile(new File(inputPath));
Enumeration<? extends ZipEntry> entrys = zipFile.entries(); while (entrys.hasMoreElements()) {
ZipEntry entry = entrys.nextElement();
String name = entry.getName();// aaa\testdir\xxxxx\eee.txt
String strTargetPath=targetPath+"/"+name;
String strTargetDir=strTargetPath.substring(0, strTargetPath.lastIndexOf(File.separator));
if (entry.isDirectory()) {
File dir = new File(strTargetPath);
if (!dir.exists()) {
dir.mkdirs();
}
} else {
File dir = new File(strTargetDir);
if (!dir.exists()) {
dir.mkdirs();
} File file = new File(strTargetPath);
if (file.exists() && isClear) {
file.delete();
} if (!file.exists()) {
InputStream in = zipFile.getInputStream(entry);
ToolOfFile.copyFile(in, file.getAbsolutePath());
}
}
}
} catch (ZipException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 创建ZIP文件
*
* @param sourcePath
* 文件或文件夹路径
* @param zipPath
* 生成的zip文件存在路径(包含文件名称)
*/
public static void createZip(String sourcePath, String zipPath) {
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
fos = new FileOutputStream(zipPath);
zos = new ZipOutputStream(fos);
writeZip(new File(sourcePath), "", zos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (zos != null) {
zos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} private static void writeZip(File inFile, String parentPath,
ZipOutputStream zipOutStream) {
if (inFile.exists()) {
if (inFile.isDirectory()) {
parentPath += inFile.getName() + File.separator;
File[] files = inFile.listFiles();
for (File file : files) {
writeZip(file, parentPath, zipOutStream);
}
} else {
FileInputStream fileInStream = null;
try {
fileInStream = new FileInputStream(inFile);
ZipEntry entry = new ZipEntry(parentPath + inFile.getName());
zipOutStream.putNextEntry(entry); byte[] buff = new byte[1024];
int len;
while ((len = fileInStream.read(buff)) != -1) {
zipOutStream.write(buff, 0, len);
zipOutStream.flush();
} } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileInStream != null) {
fileInStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
} public static void main(String[] args) {
String oldPath = "F:/test/ddd/";
String newPath = "F:/test/ccc.zip";
// ToolOfZip.createZip(oldPath, newPath);
//ToolOfZip.unZip(newPath, null, false);
System.out.println();
System.out.println("---------ok----------");
}
}

java 压缩和解压zip包的更多相关文章

  1. 使用PowerShell压缩和解压ZIP包

    更新记录 本文迁移自Panda666原博客,原发布时间:2021年7月13日. 解压ZIP包 使用PowerShell的Expand-Archive命令.PowerShell官方文档地址. 命令格式: ...

  2. Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)

    本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...

  3. 【转】Java压缩和解压文件工具类ZipUtil

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  4. JAVA调用外部安装7-Zip压缩和解压zip文件

    1.首先在本地安装7-Zip(下载链接:https://www.7-zip.org/)2.调用7-Zip压缩.zip文件: /**      * 生成.zip压缩文件      * @param fi ...

  5. java压缩和解压字符串,Byte数组,String

    在网上找到的压缩解压的工具类,可以压缩String字符串 /*** * 压缩GZip * * @param data * @return */ public static byte[] gZip(by ...

  6. Qt压缩和解压 zip

    zlib编译详见 https://blog.csdn.net/zhangxuechao_/article/details/85049711 下载quazip https://github.com/st ...

  7. JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包

    package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...

  8. 原生java 压缩解压zip文件

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  9. java 文件压缩和解压(ZipInputStream, ZipOutputStream)

    最近在看java se 的IO 部分 , 看到 java 的文件的压缩和解压比较有意思,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还 ...

随机推荐

  1. 【Android开发日记】之入门篇(十四)——Button控件+自定义Button控件

        好久不见,又是一个新的学期开始了,为什么我感觉好惆怅啊!这一周也发生了不少事情,节假日放了三天的假(好久没有这么悠闲过了),实习公司那边被组长半强制性的要求去解决一个后台登陆的问题,结果就是把 ...

  2. Django-ContentType

    背景:学位课.专题课.价格策略(每一种课程(学位课和专题课下可分为不同的种类的课程)在不同学习时间内的价格不同) 例如:如何将课程表与价格策略表关联起来: 用外键是可以将课程表和价格策略表关联起来的, ...

  3. 通过Cookie来记住用户名出现乱码问题(URL编码)

    在登录时,提交一个中文名的用户名到服务器并返回到客户端的Cookie中时, 这个过程会后台会报 java.lang.IllegalArgumentException (非法数据异常) -->在给 ...

  4. POJ1251 Jungle Roads(Kruskal)(并查集)

    Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23882   Accepted: 11193 De ...

  5. hihocoder1067 最近公共祖先·二(tarjin算法)(并查集)

    #1067 : 最近公共祖先·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上上回说到,小Hi和小Ho用非常拙劣——或者说粗糙的手段山寨出了一个神奇的网站,这个网站 ...

  6. Express下使用formidable实现POST表单上传文件并保存

    Express下使用formidable实现POST表单上传文件并保存 在上一篇文章中使用formidable实现了上传文件,但没将它保存下来. 一开始,我也以为是只得到了文件的相关信息,需要用fs. ...

  7. centos7 启用iptables

    在centos 7下启用iptables systemctl stop firewalld.service systemctl disable firewalld.service yum instal ...

  8. JDBC操作数据库的基本流程

    所有的JDBC应用程序都具有下面的基本流程: 1.加载数据库驱动并建立到数据库的连接. 2.执行SQL语句. 3.处理结果. 4.从数据库断开连接释放资源. 下面我们就来仔细看一看每一个步骤: 其实按 ...

  9. Codeforces 788C The Great Mixing(背包问题建模+bitset优化或BFS)

    [题目链接] http://codeforces.com/problemset/problem/788/C [题目大意] 给出一些浓度的饮料,要求调出n/1000浓度的饮料,问最少需要多少升饮料 [题 ...

  10. 【权值分块】bzoj1861 [Zjoi2006]Book 书架

    权值分块……rank3……没什么好说的. #include<cstdio> #include<cmath> #include<algorithm> using na ...