Java压缩和解压缩zip文件
介绍
Java提供的java.util.zip包只支持zip和gzip。至于更多格式的压缩可以选择apache的Commons Compress。


参考:https://o7planning.org/en/10195/java-compression-and-decompression-tutorial
读取zip文件列表

package com.dylan.javacore.compress;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* @Description: 列出压缩包中的文件结构
* @Author laoxu
* @Date 2019/8/5 10:01
**/
public class ListZipEntriesDemo {
public static void main(String[] args) {
String FILE_PATH="D:/test/data.zip";
ZipInputStream zipls = null;
try {
zipls = new ZipInputStream(new FileInputStream(FILE_PATH), Charset.forName("GBK"));
ZipEntry entry = null;
while ((entry=zipls.getNextEntry())!=null){
if(entry.isDirectory()){
System.out.print("Directory:");
}else{
System.out.print("File:");
}
System.out.println(entry.getName());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(zipls!=null){
try {
zipls.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
输出:

解压zip到指定目录
package com.dylan.javacore.compress;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* @Description: 解压zip
* @Author laoxu
* @Date 2019/8/5 13:50
**/
public class UnZipDemo {
public static void main(String[] args) {
final String OUTPUT_FOLDER="d:/test/output";
String FILE_PATH="d:/test/data.zip";
// 判断文件夹是否存在
File folder = new File(OUTPUT_FOLDER);
if(!folder.exists()){
folder.mkdir();
}
// 创建buffer
byte[] buffer = new byte[1024];
ZipInputStream zipls = null;
try {
zipls = new ZipInputStream(new FileInputStream(FILE_PATH), Charset.forName("GBK"));
ZipEntry entry = null;
while ((entry=zipls.getNextEntry())!=null){
String entryName = entry.getName();
String outFileName = OUTPUT_FOLDER + File.separator + entryName;
System.out.println("Unzip: " + outFileName);
if(entry.isDirectory()){
new File(outFileName).mkdirs();
}else{
FileOutputStream fos = new FileOutputStream(outFileName);
int len;
while ((len = zipls.read(buffer))>0){
fos.write(buffer,0,len);
}
fos.close();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
zipls.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

压缩文件夹
package com.dylan.javacore.compress;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @Description: 压缩文件夹
* @Author laoxu
* @Date 2019/8/5 15:25
**/
public class ZipDirectory {
public ZipDirectory() {
}
// A method to Compress a directory.
public void zipDirectory(File inputDir, File outputZipFile) {
// Create parent directory for the output file.
outputZipFile.getParentFile().mkdirs();
String inputDirPath = inputDir.getAbsolutePath();
byte[] buffer = new byte[1024];
FileOutputStream fileOs = null;
ZipOutputStream zipOs = null;
try {
List<File> allFiles = this.listChildFiles(inputDir);
// Create ZipOutputStream object to write to the zip file
fileOs = new FileOutputStream(outputZipFile);
//
zipOs = new ZipOutputStream(fileOs, Charset.forName("GBK"));
for (File file : allFiles) {
String filePath = file.getAbsolutePath();
System.out.println("Zipping " + filePath);
// entryName: là một đường dẫn tương đối.
String entryName = filePath.substring(inputDirPath.length() + 1);
ZipEntry ze = new ZipEntry(entryName);
// Put new entry into zip file.
zipOs.putNextEntry(ze);
// Read the file and write to ZipOutputStream.
FileInputStream fileIs = new FileInputStream(filePath);
int len;
while ((len = fileIs.read(buffer)) > 0) {
zipOs.write(buffer, 0, len);
}
fileIs.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
closeQuite(zipOs);
closeQuite(fileOs);
}
}
private void closeQuite(OutputStream out) {
try {
out.close();
} catch (Exception e) {
}
}
// This method returns the list of files,
// including the children, grandchildren files of the input folder.
private List<File> listChildFiles(File dir) throws IOException {
List<File> allFiles = new ArrayList<File>();
File[] childFiles = dir.listFiles();
for (File file : childFiles) {
if (file.isFile()) {
allFiles.add(file);
} else {
List<File> files = this.listChildFiles(file);
allFiles.addAll(files);
}
}
return allFiles;
}
public static void main(String[] args) {
ZipDirectory zipDir = new ZipDirectory();
File inputDir = new File("D:/test/output");
File outputZipFile = new File("D:/test/output/datas.zip");
zipDir.zipDirectory(inputDir, outputZipFile);
}
}


下一篇讲Common Compress
Java压缩和解压缩zip文件的更多相关文章
- JAVA调用外部安装7-Zip压缩和解压zip文件
1.首先在本地安装7-Zip(下载链接:https://www.7-zip.org/)2.调用7-Zip压缩.zip文件: /** * 生成.zip压缩文件 * @param fi ...
- [Java 基础] 使用java.util.zip包压缩和解压缩文件
reference : http://www.open-open.com/lib/view/open1381641653833.html Java API中的import java.util.zip ...
- Java用ZIP格式压缩和解压缩文件
转载:java jdk实例宝典 感觉讲的非常好就转载在这保存! java.util.zip包实现了Zip格式相关的类库,使用格式zip格式压缩和解压缩文件的时候,须要导入该包. 使用zipoutput ...
- Java ZIP压缩和解压缩文件(解决中文文件名乱码问题)
Java ZIP压缩和解压缩文件(解决中文文件名乱码问题) 学习了:http://www.tuicool.com/articles/V7BBvy 引用原文: JDK中自带的ZipOutputStrea ...
- Java ZIP压缩和解压缩文件并兼容linux
JDK中自带的ZipOutputStream在压缩文件时,如果文件名中有中文,则压缩后的 zip文件打开时发现中文文件名变成乱码. 解决的方法是使用apache-ant-zip.jar包(见附件)中的 ...
- Java 的zip压缩和解压缩
Java 的zip压缩和解压缩 好久没有来这写东西了,今天中秋节,有个东西想拿出来分享,一来是工作中遇到的问题,一来是和csdn问候一下,下面就分享一个Java中的zip压缩技术,代码实现比较简单,代 ...
- IO操作之使用zip包压缩和解压缩文件
转自:http://www.cdtarena.com/java.htmlJava API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作. 我们可以使 ...
- 使用commons-compress操作zip文件(压缩和解压缩)
http://www.cnblogs.com/luxh/archive/2012/06/28/2568758.html Apache Commons Compress是一个压缩.解压缩文件的类库. 可 ...
- Java对zip格式压缩和解压缩
Java对zip格式压缩和解压缩 通过使用java的相关类可以实现对文件或文件夹的压缩,以及对压缩文件的解压. 1.1 ZIP和GZIP的区别 gzip是一种文件压缩工具(或该压缩工具产生的压缩文件格 ...
- java 压缩和解压zip包
网上有关压缩和解压zip包的博文一大堆,我随便找了一个.看了看,依照自己的须要改动了一下,与各位分享一下,希望各位大神指正: package com.wangpeng.utill; import ja ...
随机推荐
- [转帖]Redis重大版本整理(Redis2.6-Redis7.0)
Redis借鉴了Linux操做系统对于版本号的命名规则:node 版本号第二位若是是奇数,则为非稳定版本(例如2.7.2.9.3.1),若是是偶数,则为稳定版本(例如2.6.2.8.3.0.3.2). ...
- [转帖]Kafka生产者——重要参数配置
https://www.cnblogs.com/luckyhui28/p/12001798.html 目录 acks max.request.size retries和retry.backoff.ms ...
- [转帖]Python基础之判断和循环(三)
https://www.jianshu.com/p/5a7552821c63 一.判断 关于判断,跟字面意思一样,就是判断某一个时刻应不应该做某件事: 语法: if 判断条件: 执行语句-- else ...
- [转帖]PyCharm无法安装第三方模块,一直提示 updating list:time out 解决办法
Pycharm无法安装第三方模块解决办法: 1.打开pycharm的项目的venv文件夹 2.打开文件夹目录中的pyvenv文件 3.将文件中的include-system-site-packages ...
- [转帖]人脸特征计算速度优化-SIMD技术Neon介绍
人脸特征计算速度优化-SIMD技术Neon介绍 JasonZhu 游走于秃头和研究的边缘 关注 15 人赞同了该文章 目录 收起 1. baseline计算 2. simd和数据重排加速 数 ...
- HTML直接插入js、css
简单的小页面可以使用 代码量大的话还是建议引用代码 直接包裹起来 <style>这里添加css代码</style> 加入css标识 <style type="t ...
- TienChin 渠道管理-渠道类型
在上一篇文章当中,表里面有一个渠道类型,我们这节主要是将这个渠道类型创建好,首先我们来看看字典表. sys_dict_type 表: 字段名 数据类型 注释 dict_id bigint 字典主键 d ...
- 语言模型的预训练[6]:思维链(Chain-of-thought,CoT)定义原理详解、Zero-shot CoT、Few-shot CoT 以及在LLM上应用
大语言模型的预训练[6]:思维链(Chain-of-thought,CoT)定义原理详解.Zero-shot CoT.Few-shot CoT 以及在LLM上应用 1.思维链定义 背景 在 2017- ...
- 5.7 Windows驱动开发:取进程模块函数地址
在笔者上一篇文章<内核取应用层模块基地址>中简单为大家介绍了如何通过遍历PLIST_ENTRY32链表的方式获取到32位应用程序中特定模块的基地址,由于是入门系列所以并没有封装实现太过于通 ...
- 9.2 Windows驱动开发:内核解析PE结构导出表
在笔者的上一篇文章<内核特征码扫描PE代码段>中LyShark带大家通过封装好的LySharkToolsUtilKernelBase函数实现了动态获取内核模块基址,并通过ntimage.h ...