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 ...
随机推荐
- [转帖]Linux Shell编程 循环语法
https://zhuanlan.zhihu.com/ for循环 for 循环是固定循环,也就是在循环时已经知道需要进行几次循环.有时也把 for 循环称为计数循环.语法: for 变量 in 值1 ...
- [转帖]Linux系统下cpio命令详解
简介 cpio主要是解压或者将文件压缩到指定文件中即copy-in和copy-out模式. 参数说明 参数 参数说明 -i copy-in模式,解压文件 -o copy-out模式,即压缩文件 -d ...
- [转帖]【k8s】5、资源管理命令-声明式
文章目录 一. yaml和json介绍 1.yuml语言介绍 2.k8s支持的文件格式 3.yaml和json的主要区别 二.声明式对象管理 1.命令式对象配置 2.声明式对象配置 3.声明式对象管理 ...
- Sysbench 开启超线程/关闭超线程以及容器运行数据库的性能损耗
Sysbench 开启超线程/关闭超线程性能损耗 摘要 Stress-NG 测试完之后 突然想 使用sysbenchen也进行一次压测 验证一把 超线程对数据的性能影响. 压测命令 ./sysbenc ...
- 数据结构与算法 第一章(48课时课程笔记)Data Structure and Algorithms
数据结构基础知识 数据(Data):是对信息的一种符号表示.在计算机科学中是指所有能输入到计算机中并被计算机程序处理的符号的总称.数据元素(Data Element):是数据的基本单位,在计算机程序中 ...
- WebAssembly入门笔记[2]:利用Memory传递数据
利用灵活的"导入"和"导出"机制,WebAssembly与承载的JavaScript应用之间可以很便利地"互通有无".<与JavaSc ...
- 范德蒙德矩阵行列式 & 循环矩阵行列式的证明
范德蒙德矩阵的行列式 \[\begin{vmatrix} 1 & 1 & 1 & \dots & 1 \\ x_1 & x_2 & x_3 & ...
- (数据科学学习手札157)pandas新增case_when方法
本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 大家好我是费老师,pandas在前不久更新的2. ...
- 第二届数字化经济与管理科学国际学术会议(CDEMS 2024)
[经济&管理|录用率高]第二届数字化经济与管理科学国际学术会议(CDEMS 2024) 2024 2nd International Conference on Digital Economy ...
- 什么是 Java 字节码?采用字节码的好处是什么?
在 Java 中,JVM 可以理解的代码就叫做字节码(即扩展名为 .class 的文件),它不面向任何特定的处理器,只面向虚拟机.Java 语言通过字节码的方式,在一定程度上解决了传统解释型语言执行效 ...