# 压缩流

private static void testZipFile() {
    File file = new File(FILENAME);
    File zipFile = new File(ZIP_FILENAME);
    try {
        InputStream is = new FileInputStream(file);
        ZipOutputStream zipOs = new ZipOutputStream(new FileOutputStream(zipFile));
        zipOs.putNextEntry(new ZipEntry(file.getName()));
        zipOs.setComment("Test ZIP");
        int temp;
        while ((temp = is.read()) != -1) {
            zipOs.write(temp);
        }
        is.close();
        zipOs.close();
        System.out.println("原始文件大小:" + file.length());
        System.out.println("压缩文件大小:" + zipFile.length());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  • 测试结果:
原始文件大小:171953
压缩文件大小:55905
1
2