用途

不解压的情况下,直接从zip文件中读取出被压缩文件的大小.

代码

public class Test {
public static void main(String[] args) {
String zipFilePath = "/users/jerry/tmp/app-debug.zip";
read(zipFilePath);
}

private static void read(String zipFilePath) {
try {
ZipFile zipFile = new ZipFile(zipFilePath);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
String name = entry.getName();
long compressedSize = entry.getCompressedSize();
long normalSize = entry.getSize();
String type = entry.isDirectory() ? "文件夹" : "文件";

System.out.print(name);
System.out.format("\t 类型:%s;压缩后大小:%d;原始大小:%d\n", type, compressedSize, normalSize);
}

zipFile.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

输出结果样例

AndroidManifest.xml  类型:文件;压缩后大小:2993;原始大小:13556
assets/ 类型:文件夹;压缩后大小:0;原始大小:0
assets/provided_dependencies_v2.txt 类型:文件;压缩后大小:402;原始大小:1110
classes.dex 类型:文件;压缩后大小:56519;原始大小:141472
classes2.dex 类型:文件;压缩后大小:2147323;原始大小:5379740