介绍

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

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

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

  2. [Java 基础] 使用java.util.zip包压缩和解压缩文件

    reference :  http://www.open-open.com/lib/view/open1381641653833.html Java API中的import java.util.zip ...

  3. Java用ZIP格式压缩和解压缩文件

    转载:java jdk实例宝典 感觉讲的非常好就转载在这保存! java.util.zip包实现了Zip格式相关的类库,使用格式zip格式压缩和解压缩文件的时候,须要导入该包. 使用zipoutput ...

  4. Java ZIP压缩和解压缩文件(解决中文文件名乱码问题)

    Java ZIP压缩和解压缩文件(解决中文文件名乱码问题) 学习了:http://www.tuicool.com/articles/V7BBvy 引用原文: JDK中自带的ZipOutputStrea ...

  5. Java ZIP压缩和解压缩文件并兼容linux

    JDK中自带的ZipOutputStream在压缩文件时,如果文件名中有中文,则压缩后的 zip文件打开时发现中文文件名变成乱码. 解决的方法是使用apache-ant-zip.jar包(见附件)中的 ...

  6. Java 的zip压缩和解压缩

    Java 的zip压缩和解压缩 好久没有来这写东西了,今天中秋节,有个东西想拿出来分享,一来是工作中遇到的问题,一来是和csdn问候一下,下面就分享一个Java中的zip压缩技术,代码实现比较简单,代 ...

  7. IO操作之使用zip包压缩和解压缩文件

    转自:http://www.cdtarena.com/java.html​​Java API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作. 我们可以使 ...

  8. 使用commons-compress操作zip文件(压缩和解压缩)

    http://www.cnblogs.com/luxh/archive/2012/06/28/2568758.html Apache Commons Compress是一个压缩.解压缩文件的类库. 可 ...

  9. Java对zip格式压缩和解压缩

    Java对zip格式压缩和解压缩 通过使用java的相关类可以实现对文件或文件夹的压缩,以及对压缩文件的解压. 1.1 ZIP和GZIP的区别 gzip是一种文件压缩工具(或该压缩工具产生的压缩文件格 ...

  10. java 压缩和解压zip包

    网上有关压缩和解压zip包的博文一大堆,我随便找了一个.看了看,依照自己的须要改动了一下,与各位分享一下,希望各位大神指正: package com.wangpeng.utill; import ja ...

随机推荐

  1. Mongo-文档主键-ObjectId

    文档主键 文档主键时 _id,如果插入文档时,没有传入则自动生产ObjectId 作为文档主键 文档主键要求在集合中唯一 文档主键可以时另一个文档,被当作字符串对象处理 ObjectId对象 获取文档 ...

  2. [转帖]fullgc问题解决:Full GC (Metadata GC Threshold)

    #问题描述 在工作过程中,遇到一个问题:Tomcat在重启或者发布的时候,会有多次的full GC. 笔者使用的版本说明: Tomcat7.0.25 JDK8 首先排查JVM的问题,就要把GC日志打开 ...

  3. [转帖]global cache cr request等待事件分析及优化

    在RAC环境中,和全局调整缓存相关的最常见的等待事件无非就是:global cache cr request,global cache busy和equeue 在XX电信做了一次数据库巡检中发现,sp ...

  4. [转帖]TiKV 缩容不掉如何解决?

    TiKV节点缩容不掉,通常遇到的情况: 1.经常遇到的情况是:3个节点的tikv集群缩容肯定会一直卡着,因为没有新节点接受要下线kv的region peer. 2.另外就是除缩容tikv外,剩下的KV ...

  5. [转帖]Tomcat maxKeepAliveRequests

    https://www.cnblogs.com/turn2i/p/10480088.html 在写这个问题前,其实我是为了分析项目碰到的一个tcp close wait问题.这个问题就不在这里讲了. ...

  6. [转帖]Unixbench的使用(综合性能测试、2D测试)和问题解决(跑不出多线程分数,调不出窗口,报错等)

    一.Unixbench简介 Unixbench一个基于系统的基准测试工具,不单纯是CPU 内存 或者磁盘测试工具.测试结果不仅仅取决于硬件,也取决于系统.开发库.甚至是编译器.Unixbench是一个 ...

  7. [转帖]03-rsync传输模式(本地传输、远程方式传输、守护进程模式传输)

    https://developer.aliyun.com/article/885801?spm=a2c6h.24874632.expert-profile.282.7c46cfe9h5DxWK 简介: ...

  8. [转帖] Linux命令拾遗-理解系统负载

    https://www.cnblogs.com/codelogs/p/16060498.html 简介# 这是Linux命令拾遗系列的第七篇,本篇主要介绍Linux中负载的概念与问题诊断方法. 本系列 ...

  9. Mysql 安装文件下载

    今天上了mysql的官方网站想下载mysql数据库 https://www.mysql.com 注册之后发现 出口许可证的问题 这里fxxk 一下川建国的老婆和女儿 感觉比较抑郁 然后就去百度了下 发 ...

  10. elementui出现展开后子菜单宽度多出1px问题

    添加 就可以解决了 .el-menu { border-right-width: 0; } <template> <div class="compen-left-men&q ...