Java-ZipUtil
Zip 压缩工具类,不支持压缩空文件夹。
简单版
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.Instant;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; public class ZipUtil {
public static void main(String[] args) {
zipCompression("D:\\123.zip", "D:\\123", "D:\\456", "D:\\er4.zip");
} static void zipCompression(String zipPath, String... paths) {
Path[] ps = new Path[paths.length];
for (int i = 0; i < paths.length; i++) {
ps[i] = Paths.get(paths[i]);
}
zipCompression(Paths.get(zipPath), ps);
} static void zipCompression(Path zipPath, Path... paths) {
long beginTime = Instant.now().toEpochMilli();
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipPath.toFile()))) {
for (Path path : paths) {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override // 访问一个文件
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
zos.putNextEntry(new ZipEntry(file.toString().replace(path.getParent().toString(), "")));
Files.copy(file, zos);
zos.closeEntry();
return FileVisitResult.CONTINUE;
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("耗时:" + (Instant.now().toEpochMilli() - beginTime));
}
}
内存映射+管道+异步线程版,效率似乎没有什改变。。。。。。
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.Pipe;
import java.nio.channels.WritableByteChannel;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.Instant;
import java.util.concurrent.CompletableFuture;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; public class ZipUtil {
public static void main(String[] args) {
zipCompression("D:\\123.zip", "D:\\123", "D:\\456", "D:\\er4.zip");
} static void zipCompression(String zipPath, String... paths) {
Path[] ps = new Path[paths.length];
for (int i = 0; i < paths.length; i++) {
ps[i] = Paths.get(paths[i]);
}
zipCompression(Paths.get(zipPath), ps);
} static void zipCompression(Path zipPath, Path... paths) {
long beginTime = Instant.now().toEpochMilli();
try (FileOutputStream fileOutputStream = new FileOutputStream(zipPath.toFile());
WritableByteChannel out = Channels.newChannel(fileOutputStream)) {
Pipe pipe = Pipe.open();
// 异步任务往通道中塞入数据
CompletableFuture.runAsync(() -> runCompressionTask(pipe, paths));
// 读取通道中数据
Pipe.SourceChannel source = pipe.source(); ByteBuffer buffer = ByteBuffer.allocate(2048);
// ByteBuffer buffer = ByteBuffer.allocateDirect(2048);
while (source.read(buffer) >= 0) {
buffer.flip();
out.write(buffer);
buffer.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("耗时:" + (Instant.now().toEpochMilli() - beginTime));
} // 异步任务
public static void runCompressionTask(Pipe pipe, Path... paths) {
try (Pipe.SinkChannel sink = pipe.sink();
OutputStream os = Channels.newOutputStream(sink);
ZipOutputStream zos = new ZipOutputStream(os);
WritableByteChannel out = Channels.newChannel(zos)) { for (Path path : paths) {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override // 访问一个目录
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
if (dir.toFile().list().length == 0) {
// 无法打包空文件夹
// zos.putNextEntry(new ZipEntry(dir.toString().replace(path.getParent().toString(), "") + File.separator));
// System.out.println(dir.toString().replace(path.getParent().toString(), "") + File.separator);
// zos.closeEntry();
}
return FileVisitResult.CONTINUE;
} @Override // 访问一个文件
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
zos.putNextEntry(new ZipEntry(file.toString().replace(path.getParent().toString(), ""))); MappedByteBuffer mappedByteBuffer = new RandomAccessFile(file.toFile(), "r").getChannel().map(FileChannel.MapMode.READ_ONLY, 0, attrs.size());
out.write(mappedByteBuffer); // FileChannel fileChannel = new FileInputStream(file.toFile()).getChannel();
// fileChannel.transferTo(0, fileChannel.size(), out);
// fileChannel.close(); zos.closeEntry();
return FileVisitResult.CONTINUE;
}
});
}
zos.finish();
} catch (IOException e) {
e.printStackTrace();
}
}
}
用到了 NIO 相关特性
https://juejin.im/post/5d5626cdf265da03a65312be
https://www.cnblogs.com/jhxxb/p/11272727.html
https://www.cnblogs.com/jhxxb/p/11303947.html
Java-ZipUtil的更多相关文章
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- java 解压缩Zip文件 ziputil
package com.lanyuan.assembly.util; import java.io.BufferedOutputStream;import java.io.File;import ja ...
- 【转】Java压缩和解压文件工具类ZipUtil
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- Java实现压缩与解压缩
import java.io.*; import java.util.*; import java.util.zip.ZipOutputStream; import java.util.zip.Zip ...
- Java导出excel
一.介绍 常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际的开发中,很多时候需要实现导入.导出Excel的应用. ...
- Java 基础【12】 压缩与解压缩
Java.util.zip 提供用于读写标准 ZIP 和 GZIP 文件格式的类. 还包括使用 DEFLATE 压缩算法(用于 ZIP 和 GZIP 文件格式)对数据进行压缩和解压缩的类. 依赖 Jd ...
- 原生java 压缩解压zip文件
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- Java 压缩字符串
1.引言 最近在做项目中,平台提供一个http服务给其他系统调用,然后我接收到其他系统的json格式的报文后去解析,然后用拿到的数据去调用corba服务,我再把corba的返回值封装完成json字符串 ...
- java中常用的工具类(二)
下面继续分享java中常用的一些工具类,希望给大家带来帮助! 1.FtpUtil Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- Java操作zip压缩和解压缩文件工具类
需要用到ant.jar(这里使用的是ant-1.6.5.jar) import java.io.File; import java.io.FileInputStream; import java.io ...
随机推荐
- vim文件时自动添加作者、时间、版权等信息
在工作中,搞运维的工程师往往会编写或完善自动化脚本时,都会手动添加表头注释,例如版权声明.作用.时间等信息提示,如果每次都手动编辑添加会大大消耗时间,所有我们可以利用快捷方法来节省时间,一种是手动在家 ...
- 【Zookerper】 安装开启
一.Windows环境 1.1 下载和安装: 1.2 开启 1.3 关闭 1.4 用客户端连接 二.Linux 环境 一.Windows环境 1.1 下载和安装: 环境要求:必须要有jdk环境 1.安 ...
- 【Hibernate】持久化对象状态及以及缓存
一.持久化类状态 1.1 三种持久化对象的状态 1.2 区分三种状态 1.3 三种状态对象转换 1.瞬时态 2.持久态 3.脱管态 4.持久态对象有自动更新数据库的能力 一.持久化类状态 1.1 三种 ...
- 编译和执行 C# 应用程序
- cmd生成大文件
用cmd生成一个大小一定的文件 输入fsutil file createnew 文件位置 文件大小(以字节为单位1024b=1kb) 列如:fsutil file createnew d:\my ...
- mysql修改表字段顺序
修改字段排列位置 ALTER TABLE 表名 MODIFY 字段名1 数据类型 FIRST|AFTER 字段名2 参数说明 FIRST,可选参数 将字段1,修改为表的第一个字段. AFTER 字段名 ...
- DNS域名解析系统介绍
域名系统(D N S)是一种用于T C P / I P应用程序的分布式数据库,它提供主机名字和 I P地址之间的转换及有关电子邮件的选路信息.这里提到的分布式是指在 I n t e r n e t上的 ...
- python面向对象基础(四)内置方法 __xx__之new与init
__init__和__new__方法 __new__() 是在新式类中新出现的方法,它作用在构造方法建造实例之前,可以这么理解,在 Python 中存在于类里面的构造方法 __init__() 负责将 ...
- XML 命名规范
XML 元素必须遵循以下命名规则: 名称可以含字母.数字以及其他的字符 名称不能以数字或者标点符号开始 名称不能以字符 "xml"(或者 XML.Xml)开始 名称不能包含空格 可 ...
- redis与spring整合实例
1)首先是redis的配置. 使用的是maven工程,引入redis与spring整合的相关jar包 <!-- redis服务 start--> <dependency> &l ...