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,不仅可以对文件进行压缩,还 ...
随机推荐
- python之八大排序方法
一.插入排序 #-*- coding:utf-8 -*- ''' 描述 插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的.个数加一的有序数据,算法适用于少量数据的排序,时 ...
- 中小型mysql数据库的备份与恢复
#转载请联系 备份到桌面 cd /home/chichung/Desktop # 切换到桌面 mysqldump -u root -p db_jingdong>jd.sql # 重定向写入 jd ...
- 请求参数中的"+"号为什么会丢失,如何保证参数完整
最近在开发中碰见一个问题,后端代码调用接口,在请求端参数没有任何问题,但是当接口接收到参数时,其中的加号全部变为了空格. 在查阅资料后发现是URLDecoder方法的问题,以下是URLDecoder的 ...
- flask学习:如何从config里载入配置
代码如下: 1.main.py from flask import Flask from config import DevConfig app=Flask(__name__) app.config. ...
- maven 通过 profile 设置多环境打包
maven 在设计之初就考虑到了业务代码和测试代码的分开存放.将业务代码默认存放在 src/main 下,将测试代码放在 src/test 下,然后在各自目录下再细分 java 与 res ...
- selenium grid 环境搭建
一.selenium grid简介 selenium grid可以同时在不同机器上测试不同浏览器,包含一个hub和多个node.node会发送配置信息到hub,hub记录并跟踪每一个node的配置信息 ...
- Codeforces 608 B. Hamming Distance Sum-前缀和
B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes input ...
- RabbitMQ生产部署指南
像RabbitMQ这样的数据服务通常有许多可调参数.一些配置对开发有很大的意义,但并不适合生产,本指南旨在为此提供帮助 虚拟主机 例如,在单租户环境中,当您的RabbitMQ集群专门为生产中的单个系统 ...
- RPD Volume 168 Issue 4 March 2016 评论4
Non-vascular interventional procedures: effective dose to patient and equivalent dose to abdominal o ...
- Codeforces 914 C Travelling Salesman and Special Numbers
Discription The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pas ...