美文网首页
新大陆,Java-压缩竟然还能这么优化!

新大陆,Java-压缩竟然还能这么优化!

作者: 阿博的java技术栈 | 来源:发表于2021-04-12 16:32 被阅读0次

前言

最近在做数据导出的功能,由于要支持批量导出且导出的文件都巨大3GB起,所以决定在导出最终结果时进行压缩

第一天

java压缩,emmm...首先想到的就是java.util.zip下面的各种api,直接上代码:

/**
*  批量压缩文件 v1.0
*  
* @param fileNames 需要压缩的文件名称列表(包含相对路径) 
* @param zipOutName 压缩后的文件名称
**/
public static void batchZipFiles(List<String> fileNames, String zipOutName) {
        //设置读取数据缓存大小
        byte[] buffer = new byte[4096];
        ZipOutputStream zipOut = null;
        try {
            zipOut = new ZipOutputStream(new FileOutputStream(zipOutName));
            for (String fileName : fileNames) {
                File inputFile = new File(fileName);
                if (inputFile.exists()) {
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inputFile));
                    //将文件写入zip内,即将文件进行打包
                    zipOut.putNextEntry(new ZipEntry(inputFile.getName()));
                    //写入文件的方法,同上
                    int size = 0;
                    //设置读取数据缓存大小
                    while ((size = bis.read(buffer)) >= 0) {
                        zipOut.write(buffer, 0, size);
                    }
                    //关闭输入输出流
                    zipOut.closeEntry();
                    bis.close();
                }
            }
        } catch (Exception e) {
            log.error("batchZipFiles error:sourceFileNames:" + JSONObject.toJSONString(fileNames), e);
        } finally {
            if (null != zipOut) {
                try {
                    zipOut.close();
                } catch (Exception e) {
                    log.error("batchZipFiles error:sourceFileNames:" + JSONObject.toJSONString(fileNames), e);
                }
            }
        }
    }

首先利用BufferedInputStream读取文件内容,ZipOutputStream的putNextEntry方法对每一个文件进行压缩写入。

最后将所有压缩后的文件写入到最终的zipOutName文件中。由于用了BufferedInputStream缓冲输入流,文件的读取和写入都是从缓存区(内存)中也就是代码里面对应的byte数组获取,相比较普通的FileInputStream提升了较大的效率。但是不够!耗时如下:


压缩三个大小为3.5GB的文件

第二天

想到了NIO,传统的IO叫BIO(上面代码)是同步阻塞的,读写都在一个线程中。NIO则是同步非阻塞的,核心是channel(通道),buffer(缓冲区),Selector(选择器)。

其实说的通俗易懂点就是NIO在密集型计算下效率之所以比BIO高的原因是NIO是多路复用,用更少的线程最更多的事情,相对于BIO大大减少了线程切换,竞争带来的资源损耗。不多BB了,上代码:


/**
     *  批量压缩文件 v2.0
     *
     * @param fileNames 需要压缩的文件名称列表(包含相对路径) 
     * @param zipOutName 压缩后的文件名称
     **/
    public static void batchZipFiles(List<String> fileNames, String zipOutName) throws Exception {
        ZipOutputStream zipOutputStream = null;
        WritableByteChannel writableByteChannel = null;
        ByteBuffer buffer = ByteBuffer.allocate(2048);
        try {
            zipOutputStream = new ZipOutputStream(new FileOutputStream(zipOutName));
            writableByteChannel = Channels.newChannel(zipOutputStream);
            for (String sourceFile : fileNames) {
                File source = new File(sourceFile);
                zipOutputStream.putNextEntry(new ZipEntry(source.getName()));
                FileChannel fileChannel = new FileInputStream(sourceFile).getChannel();
                while (fileChannel.read(buffer) != -1) {
                    //更新缓存区位置
                    buffer.flip();
                    while (buffer.hasRemaining()) {
                        writableByteChannel.write(buffer);
                    }
                    buffer.rewind();
                }
                fileChannel.close();
            }

        } catch (Exception e) {
            log.error("batchZipFiles error  fileNames:" + JSONObject.toJSONString(fileNames), e);
        } finally {
            zipOutputStream.close();
            writableByteChannel.close();
            buffer.clear();
        }
    }

还是利用java.nio包下面的api,首先用Channels.newChannel()方法将zipOutputStream输出流创建一个写的通道通道,在读取文件内容的时候直接用FileInputStream.getChannel()

获取当前文件读的通道,然后从读的通道中通过ByteBuffer(缓冲区)读取文件内容写入writableByteChannel写通道中,一定记得反转缓冲区buffer.flip(),否则读取的内容就是文件最后的内容byte=0时的。这种方法相较于上面的速度如下图所示:



压缩三个大小为3.5GB的文件

第三天

继续优化,听说用上内存映射文件的方式更快!那还等什么,让我来try一try!撸代码:

/**
     *  批量压缩文件 v3.0
     *
     * @param fileNames 需要压缩的文件名称列表(包含相对路径) 
     * @param zipOutName 压缩后的文件名称
     **/
public static void batchZipFiles(List<String> fileNames, String zipOutName) {
        ZipOutputStream zipOutputStream = null;
        WritableByteChannel writableByteChannel = null;
        MappedByteBuffer mappedByteBuffer = null;
        try {
            zipOutputStream = new ZipOutputStream(new FileOutputStream(zipOutName));
            writableByteChannel = Channels.newChannel(zipOutputStream);
            for (String sourceFile : fileNames) {
                File source = new File(sourceFile);
                long fileSize = source.length();
                zipOutputStream.putNextEntry(new ZipEntry(source.getName()));
                int count = (int) Math.ceil((double) fileSize / Integer.MAX_VALUE);
                long pre = 0;
                long read = Integer.MAX_VALUE;
                //由于一次映射的文件大小不能超过2GB,所以分次映射          
                for (int i = 0; i < count; i++) {
                    if (fileSize - pre < Integer.MAX_VALUE) {
                        read = fileSize - pre;
                    }
                    mappedByteBuffer = new RandomAccessFile(source, "r").getChannel()
                            .map(FileChannel.MapMode.READ_ONLY, pre, read);
                    writableByteChannel.write(mappedByteBuffer);
                    pre += read;
                }
                //释放资源
                Method m = FileChannelImpl.class.getDeclaredMethod("unmap", MappedByteBuffer.class);
                m.setAccessible(true);
                m.invoke(FileChannelImpl.class, mappedByteBuffer);
                mappedByteBuffer.clear();
            }
        } catch (Exception e) {
            log.error("zipMoreFile error  fileNames:" + JSONObject.toJSONString(fileNames), e);
        } finally {
            try {
                if (null != zipOutputStream) {
                    zipOutputStream.close();
                }
                if (null != writableByteChannel) {
                    writableByteChannel.close();
                }
                if (null != mappedByteBuffer) {
                    mappedByteBuffer.clear();
                }
            } catch (Exception e) {
                log.error("zipMoreFile error  fileNames:" + JSONObject.toJSONString(fileNames), e);
            }
        }
    }

这里有两个坑的地方是:

1.利用MappedByteBuffer.map文件时如果文件太大超过了Integer.MAX时(大约是2GB)就会报错:

所以这里需要分次将要写入的文件映射为内存文件。

2.这里有个bug,就是将文件映射到内存后,在写完就算clear了mappedByteBuffer,也不会释放内存,这时候就需要手动去释放,详细见上代码。

看速度!


压缩三个大小为3.5GB的文件

肯定是我的打开方式有问题,为什么反而是最慢的。。难道是文件太大了吗?我的机器内存太小了?还是我用的有问题,让我思考一下。。希望留言区讨论一下。

第四天

我在想批量压缩文件这么慢是不是因为是串行的,如果改成多线程并行那不是会快了?说干就干,本来想自己写的,后来在google上查资料发现apache-commons有现成的,那果断不重复造轮子,上代码:

/**
     *  批量压缩文件 v4.0
     *
     * @param fileNames 需要压缩的文件名称列表(包含相对路径) 
     * @param zipOutName 压缩后的文件名称
     **/
public static void compressFileList(String zipOutName, List<String> fileNameList) throws IOException, ExecutionException, InterruptedException {
        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 (String fileName : fileNameList) {
            File inFile = new File(fileName);
            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();
        log.info("ParallelCompressUtil->ParallelCompressUtil-> info:{}", JSONObject.toJSONString(parallelScatterZipCreator.getStatisticsMessage()));
    }

先看结果:


压缩三个大小为3.5GB的文件

果然还是并行的快!


相关文章

  • 新大陆,Java-压缩竟然还能这么优化!

    前言 最近在做数据导出的功能,由于要支持批量导出且导出的文件都巨大3GB起,所以决定在导出最终结果时进行压缩 第一...

  • web开发中的压缩、range范围请求

    压缩 Web服务器处理HTTP压缩之gzip、deflate压缩 【Web优化】Yslow优化法则(四)启用Gzi...

  • Android优化

    压缩APK文件 优化UI 内存优化 优化代码查看代码逻辑,提取通用代码设计模式 压缩APK文件 Android s...

  • 这么严肃的人,竟然还能这么幽默

    自从听李昌钰的演讲,再也不相信,幽默与工作有关,再也不相信哪些说,自己从事的工作太严肃,所以不会幽默。 李昌钰博士...

  • 前端资源优化解决方案

    前言 常见的资源优化方案有:1.资源压缩与合并2.图片格式优化3.图片加载优化 资源压缩与合并 为什么要压缩与合并...

  • 性能优化04-图片优化

    性能优化04-图片优化 一、图片压缩 图片在APP中通常占用很大的内存,所以经常需要进行图片压缩。 常用的图片压缩...

  • Hello Java

    目录 Java-基础(1/6) Java-对象(2/6) Java-核心库类 上(3/6) Java-核心库类下(...

  • Android

    Android常用图片压缩方式 质量压缩 尺寸压缩 1. 质量压缩 质量压缩通过相应算法进行优化Bitmap的位深...

  • 收集_Android源码文章

    一、Bitmap: Android bitmap压缩优化方案Android性能优化系列之Bitmap图片优化 二、...

  • 压缩优化

    Resource:https://www.jianshu.com/p/5bb2e8c630d2

网友评论

      本文标题:新大陆,Java-压缩竟然还能这么优化!

      本文链接:https://www.haomeiwen.com/subject/dftxlltx.html