使用java压缩文件成zip——三种方式压缩文件速度对比

文章目录
写在前面
使用Buffered缓冲流压缩
使用nio的FileChannel压缩
使用apache.commons包下的并行压缩方式压缩
写在前面
对于大文件批量压缩的问题,实际上是非常消耗时间的,怎么能提高压缩速度呢?
在这里提供了三种方式压缩文件,咱们对比一下哪一个压缩速度更快。

在这里提供了三个文件,压缩后的大小大概是1.1GB左右,咱们测试一下每一种压缩方式的压缩时间。


使用Buffered缓冲流压缩
    /**
     * zip文件压缩
     * @param inputFile 待压缩文件夹/文件名
     * @param outputFile 生成的压缩包名字
     */

    public static void ZipCompress(String inputFile, String outputFile) throws Exception {
        //创建zip输出流
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFile));
        //创建缓冲输出流
        BufferedOutputStream bos = new BufferedOutputStream(out);
        File input = new File(inputFile);
        compress(out, bos, input,null);
        bos.close();
        out.close();
    }
    /**
     * @param name 压缩文件名,可以写为null保持默认
     */
    //递归压缩
    public static void compress(ZipOutputStream out, BufferedOutputStream bos, File input, String name) throws IOException {
        if (name == null) {
            name = input.getName();
        }
        //如果路径为目录(文件夹)
        if (input.isDirectory()) {
            //取出文件夹中的文件(或子文件夹)
            File[] flist = input.listFiles();

            if (flist.length == 0)//如果文件夹为空,则只需在目的地zip文件中写入一个目录进入
            {
                out.putNextEntry(new ZipEntry(name + "/"));
            } else//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
            {
                for (int i = 0; i < flist.length; i++) {
                    compress(out, bos, flist[i], name + "/" + flist[i].getName());
                }
            }
        } else//如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
        {
            out.putNextEntry(new ZipEntry(name));
            FileInputStream fos = new FileInputStream(input);
            BufferedInputStream bis = new BufferedInputStream(fos);
            int len;
            //将源文件写入到zip文件中
            byte[] buf = new byte[1024];
            while ((len = bis.read(buf)) != -1) {
                bos.write(buf,0,len);
            }
            bis.close();
            fos.close();
        }
    }
    public static void main(String[] args) {
        try {
            //压缩
            System.out.println("开始压缩");
            long date = new Date().getTime();
            ZipCompress("D:\\fileTest", "D:\\TestbyYTT.zip");
            System.out.println("压缩结束用时" + (new Date().getTime() - date));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

执行了三次,结果如下:
开始压缩
压缩结束用时46835

开始压缩
压缩结束用时48016

开始压缩
压缩结束用时47280
1
2
3
4
5
6
7
8
9
10
结论:平均就是47秒左右。

使用nio的FileChannel压缩
    /**
     * NIO方式压缩
     */
    public static File fileToZip(String sourceFilePath, String zipFilePath, String fileName) throws IOException {
        File sourceFile = new File(sourceFilePath);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        ZipOutputStream zos = null;
        WritableByteChannel writableByteChannel = null;
        File zipFile = new File(zipFilePath + "/" + fileName);
        if (!sourceFile.exists()) {
            System.out.println("待压缩的文件目录:" + sourceFilePath + "不存在.");
        } else {
            try {
                if (zipFile.exists()) {
                    Files.delete(zipFile.toPath());
                }
                File[] sourceFiles = sourceFile.listFiles();
                if (null == sourceFiles || sourceFiles.length < 1) {
                    System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");
                } else {
                    fos = new FileOutputStream(zipFile);
                    zos = new ZipOutputStream(new BufferedOutputStream(fos));
                    writableByteChannel = Channels.newChannel(zos) ;

                    for (File file : sourceFiles) {
                        //创建ZIP实体,并添加进压缩包
                        ZipEntry zipEntry = new ZipEntry(file.getName());
                        zos.putNextEntry(zipEntry);
                        //读取待压缩的文件并写进压缩包里
                        fis = new FileInputStream(file);
                        FileChannel fileChannel = fis.getChannel();
                        fileChannel.transferTo(0, file.length() -1,writableByteChannel);
                    }
                }
            } finally {
                //关闭流
                if (null != bis) {
                    bis.close();
                }
                if (fis != null) {
                    fis.close();
                }
                if (zos != null) {
                    zos.close();
                }
                if (fos != null) {
                    fos.close();
                }
                if (null != writableByteChannel) {
                    writableByteChannel.close();
                }
            }
        }
        return zipFile;
    }
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
执行了三次,结果如下:
开始压缩
压缩结束用时36929

开始压缩
压缩结束用时37783

开始压缩
压缩结束用时36246
1
2
3
4
5
6
7
8
9
结论:平均36秒左右。
注意!transferTo方法最大能读取2G的文件!

使用apache.commons包下的并行压缩方式压缩
   /**
     * @param sourceFilePath 需要压缩的目录
     * @param zipOutName 压缩后的文件名称
     **/
    public static void compressFileList(String sourceFilePath, String zipOutName) throws IOException, ExecutionException, InterruptedException {
        File sourceFile = new File(sourceFilePath);
        File[] sourceFiles = sourceFile.listFiles();
        ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("compressFileList-pool-").build();
        ExecutorService executor = new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(20), factory);
        ParallelScatterZipCreator parallelScatterZipCreator = new ParallelScatterZipCreator(executor);
        OutputStream outputStream = new FileOutputStream(zipOutName);
        ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(outputStream);
        zipArchiveOutputStream.setEncoding("UTF-8");
        for (File inFile : sourceFiles) {
            final InputStreamSupplier inputStreamSupplier = () -> {
                try {
                    return new FileInputStream(inFile);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    return new NullInputStream(0);
                }
            };
            ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(inFile.getName());
            zipArchiveEntry.setMethod(ZipArchiveEntry.DEFLATED);
            zipArchiveEntry.setSize(inFile.length());
            zipArchiveEntry.setUnixMode(UnixStat.FILE_FLAG | 436);
            parallelScatterZipCreator.addArchiveEntry(zipArchiveEntry, inputStreamSupplier);
        }
        parallelScatterZipCreator.writeTo(zipArchiveOutputStream);
        zipArchiveOutputStream.close();
        outputStream.close();
        System.out.println("ParallelCompressUtil->ParallelCompressUtil-> info:{}"+ JSONObject.toJSONString(parallelScatterZipCreator.getStatisticsMessage()));
    }
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
27
28
29
30
31
32
33
执行了三次,结果如下:
开始压缩
ParallelCompressUtil->ParallelCompressUtil-> info:{}{"compressionElapsed":22642,"mergingElapsed":3802}
压缩结束用时27261

开始压缩
ParallelCompressUtil->ParallelCompressUtil-> info:{}{"compressionElapsed":21958,"mergingElapsed":3406}
压缩结束用时25524

开始压缩
ParallelCompressUtil->ParallelCompressUtil-> info:{}{"compressionElapsed":22076,"mergingElapsed":3668}
压缩结束用时25977
1
2
3
4
5
6
7
8
9
10
11
12
结论:平均26秒左右。
————————————————
版权声明:本文为CSDN博主「秃了也弱了。」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/A_art_xiang/article/details/125207480

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以使用java.util.zip包下的相关类来实现将文件压缩zip文件的功能。以下是一个简单的示例代码: ```java import java.io.*; import java.util.zip.*; public class ZipUtils { public static void compressFileToZip(String sourceFilePath, String zipFilePath) { try { // 创建压缩文件输出流 FileOutputStream fos = new FileOutputStream(zipFilePath); ZipOutputStream zos = new ZipOutputStream(fos); // 创建待压缩的文件输入流 FileInputStream fis = new FileInputStream(sourceFilePath); BufferedInputStream bis = new BufferedInputStream(fis); // 往压缩包中写入文件 zos.putNextEntry(new ZipEntry(sourceFilePath)); byte[] buffer = new byte[1024]; int len; while ((len = bis.read(buffer)) > 0) { zos.write(buffer, 0, len); } // 关闭输入输出流 bis.close(); fis.close(); zos.closeEntry(); zos.close(); System.out.println("文件压缩功!"); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { String sourceFilePath = "source.txt"; // 待压缩的文件路径 String zipFilePath = "compressed.zip"; // 压缩后的zip文件路径 compressFileToZip(sourceFilePath, zipFilePath); } } ``` 以上代码中,我们首先创建一个压缩文件输出流(`ZipOutputStream`),并指定输出文件路径。然后创建一个待压缩的文件输入流(`FileInputStream`)和一个缓冲输入流(`BufferedInputStream`)。接下来,我们将待压缩的文件写入压缩包(`zip` 文件)中,并使用缓冲区逐块写入。最后,关闭输入输出流,完文件的压缩操作。 注意:上述代码只能压缩一个文件,如果要压缩多个文件,需要在循环中依次压缩每一个文件。另外,压缩完后,可以通过解压缩操作还原被压缩的文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值