惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志
Jina AI
Jina AI
博客园 - Franky
U
Unit 42
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
宝玉的分享
宝玉的分享
B
Blog RSS Feed
雷峰网
雷峰网
D
DataBreaches.Net
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
InfoQ
美团技术团队
云风的 BLOG
云风的 BLOG
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
大猫的无限游戏
大猫的无限游戏
G
Google Developers Blog
T
Tailwind CSS Blog
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
月光博客
月光博客
Engineering at Meta
Engineering at Meta

祈雨的笔记

安全多方计算MPC spark原理解析 kueue执行源码分析 spark on k8s执行源码分析 spark-operator源码解析 系统压测遇到的缓存击穿问题 我的世界PC与安卓联机 蚂蚁金服流量投放平台的AIG改造 G1大对象致Old区占用率高 日志打印导致接口响应率下跌分析 Groovy加载类导致OOM分析 ERROR日志打印导致CPU满载 记OceanBase死锁超时 应用发版期间服务响应超时 Ark Serverless初探 系统优化复盘一二三 The user specified as a definer does not exist Kong网关初探 API网关选型调研 CPU火焰图常用工具 配置中心选型调研 root操作Nginx导致用户组错误 基于Proxifier使用代理 FastJSON字段智能匹配踩坑 Nacos初探 记一次Nginx服务器CPU满荷载故障 基于券系统分库分表的思考 limit不参与SQL成本计算致索引失效 Linux常用性能监控命令 golang低版本http2偶现400
纯内存读取Zip文件
祈雨的笔记 · 2017-12-26 · via 祈雨的笔记

总结

方法1完全内存读取,只需要一个输入流即可;
方法2必须从操作系统本地读取文件才行,不能做到完全的内存读取;

方法1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static void readZipFile0(InputStream in) throws Exception {
ZipInputStream zis = new ZipInputStream(in);
ZipEntry entry = null;
while ((entry = zis.getNextEntry()) != null) {
String entryName = entry.getName();
System.out.println(entryName);
int size = (int) entry.getSize();
byte[] buf = new byte[size];
if (entry.isDirectory()) {

continue;
}
byte[] bs = new byte[1024];
int len = 0;
int off = 0;
while ((len = zis.read(bs)) != -1) {
System.arraycopy(bs, 0, buf, off, len);
off += len;
}
FileOutputStream out = new FileOutputStream("D:/doc/"+entryName.replace("/", ""));
out.write(buf);
out.close();
}
zis.close();
}

方法2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public static void readZipFile1(String file) throws Exception {
ZipFile zf = new ZipFile(file);
InputStream in = new BufferedInputStream(new FileInputStream(file));
ZipInputStream zin = new ZipInputStream(in);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
if (ze.isDirectory()) {

continue;
}
System.out.println(ze.getName());
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
InputStream fileIn = zf.getInputStream(ze);
int val;
while((val=fileIn.read())!=-1) {
byteOut.write(val);
}
fileIn.close();
byteOut.writeTo(new FileOutputStream("D:/doc/"+ze.getName().replace("/", "")));
byteOut.close();
}
zin.closeEntry();
zin.close();
in.close();
zf.close();
}