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 ...
随机推荐
- appium填坑
首次使用appium web driver,不说搭建环境的麻烦,初次写完一个操作计算器的程序,但是运行一直报错:selenium.common.exceptions.WebDriverExceptio ...
- spring security中当已登录用户再次访问登录界面时,应跳转到home
@RequestMapping("/login") public String login(){ Authentication auth = SecurityContextHold ...
- PyCharm编程软件详细安装教程
PyCharm编程软件安装教程&破解 一.官网下载软件 1. 网页搜索进入PyCharm官网下载页面(https://www.jetbrains.com/pycharm/download/ ) ...
- HGAME-week2-web-wp
hgame第二周总结 1.webpack-engine 我不懂,但是真的刚打开就出来了,一脸懵逼(wp说是sourcemap没关 hgame{D0nt_f0r9et_2_ClOs3_S0urce_m@ ...
- Solution -「Gym 102979L」 Lights On The Road
\(\mathcal{Description}\) Link. 给定序列 \(\{w_n\}\),选择 \(i\) 位置的代价为 \(w_i\),要求每个位置要不被选择,要不左右两个位置至少被 ...
- suse 12 利用缓存创建本地源供内网服务使用
文章目录 服务端获取 添加源 刷新源 清除缓存 安装软件 获取rpm包 客户端测试 zypper --help 前言: 其实,咱也不知道为啥写了这篇博客,咱就是想学一学suse,咱也不会,咱也只能学, ...
- 虚拟机搭建web服务器
下载CentOS镜像 下载网址:阿里云镜像 选择版本(这里我使用的7) 选择isos/ 选择Minimal.iso,这个版本是最小镜像安装:没有图像界面 只有命令行 将CentOS安装到VM16中 注 ...
- nginx域名转发
场景1:因服务器限制,所以只对外开放了一个端口,但是需要请求不同的外网环境,所以在中转服务器上用nginx做了一次转发 实现: server { listen 8051; server_name lo ...
- for循环例子
代码 点击查看[ForTest.java]代码 //package com.d; import java.util.Scanner; /** * For循环例子 * @date: 2022.2.24 ...
- 递归Recursion
从开始自学写代码开始,就感觉递归是个特别美丽的算法. "如果使用循环,程序的性能可能更高:如果使用递归,程序可能更容易理解.如何选择要看什么对你来说更重要." 编写递归函数时,必须 ...