java 压缩和解压zip包
网上有关压缩和解压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包的更多相关文章
- 使用PowerShell压缩和解压ZIP包
更新记录 本文迁移自Panda666原博客,原发布时间:2021年7月13日. 解压ZIP包 使用PowerShell的Expand-Archive命令.PowerShell官方文档地址. 命令格式: ...
- Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)
本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...
- 【转】Java压缩和解压文件工具类ZipUtil
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- JAVA调用外部安装7-Zip压缩和解压zip文件
1.首先在本地安装7-Zip(下载链接:https://www.7-zip.org/)2.调用7-Zip压缩.zip文件: /** * 生成.zip压缩文件 * @param fi ...
- java压缩和解压字符串,Byte数组,String
在网上找到的压缩解压的工具类,可以压缩String字符串 /*** * 压缩GZip * * @param data * @return */ public static byte[] gZip(by ...
- Qt压缩和解压 zip
zlib编译详见 https://blog.csdn.net/zhangxuechao_/article/details/85049711 下载quazip https://github.com/st ...
- JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包
package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...
- 原生java 压缩解压zip文件
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- java 文件压缩和解压(ZipInputStream, ZipOutputStream)
最近在看java se 的IO 部分 , 看到 java 的文件的压缩和解压比较有意思,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还 ...
随机推荐
- Javascript基于对象三大特征 -- 冒充对象
Javascript基于对象三大特征 基本概述 JavaScript基于对象的三大特征和C++,Java面向对象的三大特征一样,都是封装(encapsulation).继承(inheritance ) ...
- happens-before规则
happens-before原则: happens-before它是判断数据是否存在竞争.线程是否安全的主要依据.为了保证线程安全我们可以让2个操作具有happens-before关系.(JDK5 开 ...
- PHP文件操作函数
1 获得文件名: basename(); 给出一个包含有指向一个文件的全路径的字符串,本函数返回基本的文件名.如果文件名是以 suffix 结束的,那这一部分也会被去掉. eg: 复制代码 代码如下: ...
- JS—正则表达式
正则表达式的元字符是包含特殊含义的字符,它们有一些特殊的功能,可以控制匹配模式的方式,反斜杠后的元字符失去其特殊含义. 单个字符和数字 .表示匹配除换行符外的单个字符,两个.就表示匹配两个字符 var ...
- Windows7 SP1旗舰版精简版最终版本
2016年6月9号,老毛子@lopatkin大神针对Win7 SP1旗舰版精简版进行了最终更新,此次主要是之前小问题的修复和调整.该版为Windows 7 SP1 简体中文旗舰版最新版,包含2016年 ...
- HDU 6318.Swaps and Inversions-求逆序对-线段树 or 归并排序 or 离散化+树状数组 (2018 Multi-University Training Contest 2 1010)
6318.Swaps and Inversions 这个题就是找逆序对,然后逆序对数*min(x,y)就可以了. 官方题解:注意到逆序对=交换相邻需要交换的次数,那么输出 逆序对个数 即可. 求逆序对 ...
- 基于opencv的摄像头的标定
四个坐标系分别为:世界坐标系(Ow),摄像机坐标系(Oc),图像物理坐标系(O1,单位mm),图像像素坐标系(O,位于视野平面的左上角,单位pix). 空间某点P到其像点p的坐标转换过程主要是通过这四 ...
- 洛谷——P1358 扑克牌
题目描述 组合数学是数学的重要组成部分,是一门研究离散对象的科学,它主要研究满足一定条件的组态(也称组合模型)的存在.计数以及构造等方面的问题.组合数学的主要内容有组合计数.组合设计.组合矩阵.组合优 ...
- SPOJ UOFTCG - Office Mates (树的最小路径覆盖)
UOFTCG - Office Mates no tags Dr. Baws has an interesting problem. His N graduate students, while f ...
- 后门工具dbd
后门工具dbd dbd功能类似于Netcat,但提供强大的加密功能,支持AES-CBC-128和HMAC-SHA1加密.该工具可以运行在类Unix和Windows系统中.渗透测试人员首先使用该工具 ...