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

推荐订阅源

小众软件
小众软件
A
About on SuperTechFans
博客园 - Franky
Engineering at Meta
Engineering at Meta
Recent Announcements
Recent Announcements
云风的 BLOG
云风的 BLOG
B
Blog
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
Martin Fowler
Martin Fowler
Y
Y Combinator Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 叶小钗
Vercel News
Vercel News
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
Last Week in AI
Last Week in AI
腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
V
V2EX
G
Google Developers Blog

祈雨的笔记

安全多方计算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();
}