java中自己常用到的工具类-压缩解压zip文件
package com.ricoh.rapp.ezcx.admintoolweb.util; 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.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream; public class ZipUtil { private static final int BUFFER_SIZE = 2 * 1024; /**
* @param srcDir
* 需要压缩的文件夹
* @param zipPath
* 压缩文件目录
* @param zipFileName
* 压缩文件的名称
* @throws RuntimeException
* @throws FileNotFoundException
*/
public static void toZip(String srcDir, String zipPath, String zipFileName)
throws RuntimeException, FileNotFoundException {
long start = System.currentTimeMillis();
File zipDir = new File(zipPath);
if (!zipDir.exists() || !zipDir.isDirectory()) {
zipDir.mkdirs();
} File zipFile = new File(zipPath, zipFileName);
FileOutputStream out = new FileOutputStream(zipFile);
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(out);
File sourceFile = new File(srcDir);
compress(sourceFile, zos, sourceFile.getName(), false);
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) + " ms");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils", e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} /**
* @param sourceFile
* 需要压缩的文件目录
* @param zos
* zip输出流
* @param name
* 压缩后的zip名称
* @param KeepDirStructure
* 是否保留原来的目录结构(false:不保留;true:保留)
* @throws Exception
*/
private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure)
throws Exception {
byte[] buf = new byte[BUFFER_SIZE];
if (sourceFile.isFile()) {
zos.putNextEntry(new ZipEntry(name));
int len;
FileInputStream in = new FileInputStream(sourceFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
} else {
File[] listFiles = sourceFile.listFiles();
if (listFiles == null || listFiles.length == 0) {
zos.putNextEntry(new ZipEntry(name + "/"));
zos.closeEntry();
} else {
for (File file : listFiles) {
if (KeepDirStructure) {
compress(file, zos, name + "/" + file.getName(), true);
} else {
compress(file, zos, file.getName(), true);
}
}
}
}
} /**
* 压缩成ZIP 方法2
*
* @param srcFiles
* 需要压缩的文件列表
* @param out
* 压缩文件输出流
* @throws RuntimeException
*
*/
public static void toZip(File[] files, String zipPath, String zipFileName) throws RuntimeException { long start = System.currentTimeMillis();
ZipOutputStream zos = null;
FileOutputStream out = null;
try {
File zipDir = new File(zipPath);
if (!zipDir.exists() || !zipDir.isDirectory()) {
zipDir.mkdirs();
} File zipFile = new File(zipPath, zipFileName);
out = new FileOutputStream(zipFile);
zos = new ZipOutputStream(out);
for (File srcFile : files) {
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int len;
FileInputStream in = new FileInputStream(srcFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) + " ms");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils", e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} /**
* 解压文件到指定目录
*/
@SuppressWarnings("rawtypes")
public static void unZipFiles(String zipPath, String descDir) {
File zipFile = new File(zipPath);
File pathFile = new File(descDir);
if (!pathFile.exists()) {
pathFile.mkdirs();
}
// 解决zip文件中有中文目录或者中文文件
ZipFile zip = null;
try {
zip = new ZipFile(zipFile, Charset.forName("GBK")); for (Enumeration entries = zip.entries(); entries.hasMoreElements();) {
// 输出文件路径信息
FileOutputStream out = null;
InputStream in = null;
try {
ZipEntry entry = (ZipEntry) entries.nextElement();
String zipEntryName = entry.getName();
in = zip.getInputStream(entry);
String outPath = descDir + File.separator + zipEntryName;
// 判断路径是否存在,不存在则创建文件路径
// File file = new File(outPath.substring(0, outPath.lastIndexOf("/")));
File file = new File(outPath.substring(0, outPath.lastIndexOf(File.separator)));
if (!file.exists()) {
file.mkdirs();
}
// 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if (new File(outPath).isDirectory()) {
continue;
} out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while ((len = in.read(buf1)) > 0) {
out.write(buf1, 0, len);
} } catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (zip != null) {
try {
zip.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
} /**
* 测试
*/ public static void main(String[] args) throws FileNotFoundException { /*
* String sourFile = "D:/testzip"; String zipPath = "D:/wl"; String zipFileName
* = "tow.zip"; ZipUtil.toZip(sourFile, zipPath, zipFileName);
*/ /*
* File sfile = new File(sourFile); List<File> fileList = new ArrayList<>();
* for(File f : sfile.listFiles()) { fileList.add(f); } toZip(sfile.listFiles(),
* zipPath, zipFileName);
*/ String zipPath1 = "d:/testunzip/user.zip";
String zipPath2 = "D:/testunzip/photos.zip";
String descDir1 = "d:/testunzip/";
String descDir2 = "d:/testunzip/ps";
unZipFiles(zipPath2, descDir2); } }
java中自己常用到的工具类-压缩解压zip文件的更多相关文章
- JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包
package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...
- PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 && Linux下的ZipArchive配置开启压缩 &&搞个鸡巴毛,写少了个‘/’号,浪费了一天
PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有 ...
- 原生java 压缩解压zip文件
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载
文章转载自:https://my.oschina.net/junn/blog/104464 PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PH ...
- Java 上传解压zip文件,并且解析文件里面的excel和图片
需求:上传一个zip文件,zip文件里面包含一个excel和很多图片,需要把excel里面的信息解析出来保存到表中,同时图片也转化成base64保存到数据库表中. PS:为了方便不同水平的开发人员阅读 ...
- JAVA调用外部安装7-Zip压缩和解压zip文件
1.首先在本地安装7-Zip(下载链接:https://www.7-zip.org/)2.调用7-Zip压缩.zip文件: /** * 生成.zip压缩文件 * @param fi ...
- java实现解压zip文件,(亲测可用)!!!!!!
项目结构: Util.java内容: package com.cfets.demo; import java.io.File; import java.io.FileOutputStream; imp ...
- Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)
本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...
- Java中的AES加解密工具类:AESUtils
本人手写已测试,大家可以参考使用 package com.mirana.frame.utils.encrypt; import com.mirana.frame.constants.SysConsta ...
随机推荐
- 5、Linux基础--etc(文件系统)、启动模式、单用户模式修改密码、安装目录、日志目录、状态目录
笔记 1.晨考 1.存放系统配置文件的目录 /etc 2.存储系统实时运行状态的目录 /proc 3.存储系统硬件接口的目录 /dev 4.查看系统挂载情况的命令 df -h 5.系统网卡文件路径 / ...
- Solution -「ARC 101D」「AT4353」Robots and Exits
\(\mathcal{Description}\) Link. 有 \(n\) 个小球,坐标为 \(x_{1..n}\):还有 \(m\) 个洞,坐标为 \(y_{1..m}\),保证上述坐标 ...
- JDK、JRE 和 JVM 有什么用,它们是怎样运行的
JDK如何运作? JDK 功能 以下是JDK的重要组件: JDK 和 JRE:程序员通过使用JDK 创建由 JRE 运行的 Java 程序,其中包括 JVM 和类库. 类库:是一组可动态加载的库,Ja ...
- 神奇!这款 Vue 后台框架居然不用手动配置路由
前言 做 Vue 开发脱离不了路由,尤其是中大型项目,页面多且杂,在配置路由的时候总是会变得逐渐暴躁,因为费时,并且又没有什么太多技术含量,总觉得是在浪费时间. 另外如果接手了别人的项目,当业务有变更 ...
- [GAMEDEV] 个人开发如何找到合适的图片素材?
1. 起因 起因是想找一些UI设计方面的素材(具体地说,类似于Web或者App上一些按钮/页签/进度条等元素),用到游戏GUI中来,毕竟扁平化的网页设计还是很清爽的. 本以为这方面的素材会比游戏中的图 ...
- 华为HCIP实验--OSPF单区域
场景:你是公司的网络管理员.现在公司的网络中有三台ARG3路由器,通过以太网实现相互的连通.在以太网这样的广播式多路访问网络上,可能存在安全隐患,所有你选择采用OSPF区域认证的方法来避免恶意的路由攻 ...
- C#值类型回收
函数调用在执行时,首先要在栈中为形参和局部变量分配存储空间,然后还要将实参的值复制给形参,接下来还要将函数的返回地址(该地址指明了函数执行结束后,程序应该回到哪里继续执行)放入栈中,最后才跳转到函数内 ...
- 换行符号(\r\n)的历史
文章来源:https://cloud.tencent.com/developer/article/1730918 \r\n与\n是有区别的. 如果要通用的则是\r\n,因为有些编辑器它不认\n &qu ...
- oj教程--队列
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表.进行插入操作的端称为队尾,进行删除操作的 ...
- WIN10:全选一个文件夹中的所有文件