java 常用zip压缩方式

1 篇文章 0 订阅

常用压缩方式

测试结果仅为小文本测试,压缩时间以及占用内存差距不大

普通压缩

public static void toZip1(List<File> srcFiles, OutputStream out) {
        long start = System.currentTimeMillis();
        try (ZipOutputStream zos = new ZipOutputStream(out);) {
            for (File srcFile : srcFiles) {
                zos.putNextEntry(new ZipEntry(srcFile.getName()));
               FileInputStream in = new FileInputStream(srcFile);
                IOUtils.copy(in, zos);
                in.close();
                zos.closeEntry();
            }
            long end = System.currentTimeMillis();
            log.info("1压缩完成,耗时:{} ms", (end - start));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

内存映射

 public static void toZip2(List<File> srcFiles, OutputStream out) {
        long start = System.currentTimeMillis();
        try (ZipOutputStream zipOut = new ZipOutputStream(out);
            WritableByteChannel writableByteChannel = Channels.newChannel(zipOut)) {
            for (File srcFile : srcFiles) {
                
                zipOut.putNextEntry(new ZipEntry(srcFile.getName()));
                FileInputStream fileInputStream = new FileInputStream(srcFile);
                FileChannel channel = fileInputStream.getChannel();
                //内存中的映射文件
                //FileChannel r = new RandomAccessFile(path, "r").getChannel();
                MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
                
                writableByteChannel.write(mappedByteBuffer);
            }
            long end = System.currentTimeMillis();
            log.info("2压缩完成,耗时:{} ms", (end - start));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

管道(NIO Pipe)

public static void toZip3(List<File> srcFiles, OutputStream out) {
        long start = System.currentTimeMillis();
        try (WritableByteChannel writeChannel = Channels.newChannel(out)) {
            Pipe pipe = Pipe.open();
            //异步任务
            CompletableFuture.runAsync(() -> runTask(pipe, srcFiles));
            
            //获取读通道
            ReadableByteChannel readableByteChannel = pipe.source();
            ByteBuffer buffer = ByteBuffer.allocate((1024));
            while (readableByteChannel.read(buffer) >= 0) {
                buffer.flip();
                writeChannel.write(buffer);
                buffer.clear();
            }
            long end = System.currentTimeMillis();
            log.info("3压缩完成,耗时:{} ms", (end - start));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    //异步任务
    public static void runTask(Pipe pipe, List<File> srcFiles) {
        try (ZipOutputStream zos = new ZipOutputStream(Channels.newOutputStream(pipe.sink()));
            WritableByteChannel writeChannel = Channels.newChannel(zos)) {
            for (File srcFile : srcFiles) {
                zos.putNextEntry(new ZipEntry(srcFile.getName()));
                FileChannel fileChannel = new FileInputStream(srcFile).getChannel();
                fileChannel.transferTo(0, fileChannel.size(), writeChannel);
                fileChannel.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

通道(NIO Channel)

public static void toZip14(List<File> srcFiles, OutputStream out) {
        //开始时间
        long start = System.currentTimeMillis();
        try (ZipOutputStream zipOut = new ZipOutputStream(out);
            WritableByteChannel writableByteChannel = Channels.newChannel(zipOut)) {
            for (File srcFile : srcFiles) {
                try (FileChannel fileChannel = new FileInputStream(srcFile).getChannel()) {
                    zipOut.putNextEntry(new ZipEntry(srcFile.getName()));
                    fileChannel.transferTo(0, fileChannel.size(), writableByteChannel);
                }
            }
            long end = System.currentTimeMillis();
            log.info("4压缩完成,耗时:{} ms", (end - start));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

测试示例

 public static void main(String[] args) throws FileNotFoundException {
        List<File> srcFiles = new ArrayList<>();
        srcFiles.add(new File("文本1.txt"));
        srcFiles.add(new File("文本2.txt"));
        srcFiles.add(new File("文本3.txt"));
        srcFiles.add(new File("文本4.txt"));

        FileOutputStream fileOutputStream1 = new FileOutputStream("zip1.zip");
        FileOutputStream fileOutputStream2 = new FileOutputStream("zip2.zip");
        FileOutputStream fileOutputStream3 = new FileOutputStream("zip3.zip");
        FileOutputStream fileOutputStream4 = new FileOutputStream("zip4.zip");
        toZip1(srcFiles, fileOutputStream1);
        toZip1(srcFiles, fileOutputStream2);
        toZip1(srcFiles, fileOutputStream3);
        toZip1(srcFiles, fileOutputStream4);
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值