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 ...
随机推荐
- [转帖]SQL Server中查询CPU占用高的SQL语句
本文导读:触发器造成死锁.作业多且频繁.中间表的大量使用.游标的大量使用.索引的设计不合理.事务操作频繁.SQL语句设计不合理,都会造成查询效率低下.影响服务器性能的发挥.我们可以使用sql serv ...
- [转帖]ASH、AWR、ADDM区别联系
==================================================================================================== ...
- [转帖]iptables ip_conntrack_max 调整
https://www.diewufeiyang.com/post/583.html 一.概念 ==================== -允许的最大跟踪连接条目:CONNTRACK_MAX(默认值是 ...
- [转帖]ELKStack入门篇(二)之Nginx、Tomcat、Java日志收集以及TCP收集日志使用
https://www.cnblogs.com/linuxk/p/9273160.html 1.收集Nginx的json格式日志 1.1.Nginx安装 1.2.配置logstash [root@ ...
- [转帖]/etc/profile和/etc/environment的区别
时间 2019-11-07 标签 profile environment 区别 繁體版 原文 https://my.oschina.net/u/2885925/blog/2989579 /etc ...
- echarts给每个柱状图配置不同的颜色
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- spring boot设置日志打印为控制台输出和文件输出
日志打印 sources里建logback-spring.xml <?xml version="1.0" encoding="UTF-8"?> &l ...
- 【一】gym环境安装以及安装遇到的错误解决
相关文章: [一]gym环境安装以及安装遇到的错误解决 [二]gym初次入门一学就会-简明教程 [三]gym简单画图 [四]gym搭建自己的环境,全网最详细版本,3分钟你就学会了! [五]gym搭建自 ...
- hadoop-3.0.0-cdh6.3.2源码编译实践
1.编译过程 参考:https://blog.mygallop.cn/2020/10/centos/hadoop-cdh6-compile/ 2.问题记录 CDH6.3.2 Hadoop源码位置发生变 ...
- [Go] string、int、int64相互转换
import "strconv" //先导入strconv包 // string到int int, err := strconv.Atoi(string) // string到in ...