在test.zip文件中读取test.xml文件内容。
代码如下所示。
File processFile = new File(RESOURCES_BASE_FILE, PROCESS);
File file = new File(processFile, "test.zip");
ZipFile zipFile = new ZipFile(file);
InputStream zipInputStream = zipFile.getInputStream(zipFile.getEntry("test.xml"));
String xml = FileUtil.readFileContentAsString(zipInputStream, "UTF-8");
assertNotNull(xml);
zipInputStream.close();
压缩文件IO性能分析
编号 | 文件类型 | 大小 | 读写次数 | 消耗时间 | 备注 |
1 | 普通文本文件(ZIP) | 1K | 100 | 0.020秒 | 1,2,3,4所读取的同一个文件,区别在于2,3,4是在压缩包中的。 |
2 | 单个压缩文件 | 0.5K | 100 | 0.025秒 | 编号1的压缩文本 |
3 | 流程压缩文件(ZIP) | 100K | 100 | 0.025秒 | 消息流程压缩文件 |
4 | 大压缩文件(ZIP) | 100M | 100 | 0.025秒 | 树状层次的压缩文件 |
5 | 大文本文件 | 50M | 10 | 1.2秒 | |
6 | 单个大压缩文件(ZIP) | 35M | 10 | 1.3秒 | 编号5的压缩文本 |
总体上讲,从ZIP中IO文件的性能比直接IO文本的性能慢20%