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文件的更多相关文章

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

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

  2. PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 && Linux下的ZipArchive配置开启压缩 &&搞个鸡巴毛,写少了个‘/’号,浪费了一天

    PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有 ...

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

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

  4. PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载

    文章转载自:https://my.oschina.net/junn/blog/104464 PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PH ...

  5. Java 上传解压zip文件,并且解析文件里面的excel和图片

    需求:上传一个zip文件,zip文件里面包含一个excel和很多图片,需要把excel里面的信息解析出来保存到表中,同时图片也转化成base64保存到数据库表中. PS:为了方便不同水平的开发人员阅读 ...

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

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

  7. java实现解压zip文件,(亲测可用)!!!!!!

    项目结构: Util.java内容: package com.cfets.demo; import java.io.File; import java.io.FileOutputStream; imp ...

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

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

  9. Java中的AES加解密工具类:AESUtils

    本人手写已测试,大家可以参考使用 package com.mirana.frame.utils.encrypt; import com.mirana.frame.constants.SysConsta ...

随机推荐

  1. MacOS常用命令行工具

    转自:https://blog.csdn.net/u014102846/article/details/77964493 https://ohmyz.sh/ https://github.com/ro ...

  2. Solution -「NOI.AC 省选膜你赛」array

    题目 题意简述   维护一个长度为 \(n\) 的序列 \(\{a_n\}\),并给出 \(q\) 个操作: 将下标为 \(x\) 的数修改为 \(y\). 给定 \(l,r,k\),求最大的 \(m ...

  3. Solution -「LOCAL」画画图

    \(\mathcal{Description}\)   OurTeam.   给定一棵 \(n\) 个点的树形随机的带边权树,求所有含奇数条边的路径中位数之和.树形生成方式为随机取不连通两点连边直到全 ...

  4. 【第二十三期】春招实习阶段性总结(阿里云已OC)

    本人算是一个半路出家找工作的菜鸡了,现在手中阿里云云原生offer.百度度小满offer.腾讯PCG二面环节.美团点评等offer环节.希望我的经历分享能对各位或之后准备春招的同学有帮助. 个人背景 ...

  5. 从零开始,开发一个 Web Office 套件(5):Mouse hover over text

    <从零开始, 开发一个 Web Office 套件>系列博客目录 这是一个系列博客, 最终目的是要做一个基于HTML Canvas 的, 类似于微软 Office 的 Web Office ...

  6. 数据分析刚入门?这几个BI软件你一定得知道!

    在当前乃至未来5年的职场中,将取代你的可能不是 AI ,而是比你"更懂"数据分析的同事.毕竟,现在做什么都讲究"用数字说话",很多岗位在招聘JD中均给出了&qu ...

  7. 目前数据可视化工具排名如何?好用的BI可视化软件

    数据可视化用专业术语来就是通过视觉的方式向人类展示数据,这种在文本基础上的图表即简单又实用,而且相关性.趋势分析都非常明确,也非常可靠,通过图表一目了然.用通俗的话说就是画一张图表,将数据以比例的方式 ...

  8. 关于WinForm布局那些事情

    最近项目中,需要用WinForm做一些简单的功能,给第三方作为测试用.本来想着简单的拖几个控件,布局一下就了事了的.但是因为第三方是个大客户,需要展示出我们的技术水平.遂好好的研究了一下WinForm ...

  9. oracle 日期改字符格式_Oracle日期类型转换格式

    转至:https://blog.csdn.net/weixin_39629269/article/details/111537468 将日期型转换成字符串时,可以按新的格式显示. 如格式YYYY-MM ...

  10. 『无为则无心』Python日志 — 66、将日志信息保存到文件中

    目录 1.把日志信息保存到文件中 2.拓展 (1)观察代码 (2)提出问题 (3)问题说明 1.把日志信息保存到文件中 代码如下所示: """ logging模块是Pyt ...