10.文件打包工具类(jdk版)

/**
 * 文件打包工具类
 *
 * @author zlx
 * @date 2021/09/09 17:09
 **/
public class ZipUtils {

    // InputStream 耗时20s左右
    public static void zipFileNoBuffer(String zipFilePath, String... filePaths) {
        File zipFile = new File(zipFilePath);
        try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile))) {
            for (String filePath : filePaths) {
                try (InputStream input = new FileInputStream(filePath)) {
                    zipOut.putNextEntry(new ZipEntry(filePath.substring(filePath.lastIndexOf(File.separator) + 1)));
                    int temp;
                    while ((temp = input.read()) != -1) {
                        zipOut.write(temp);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // BufferedInputStream 耗时2s左右
    public static void zipFileBuffer(String zipFilePath, String... filePaths) {
        File zipFile = new File(zipFilePath);
        try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
             BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(zipOut)) {
            for (String filePath : filePaths) {
                try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(filePath))) {
                    zipOut.putNextEntry(new ZipEntry(filePath.substring(filePath.lastIndexOf(File.separator) + 1)));
                    int temp;
                    while ((temp = bufferedInputStream.read()) != -1) {
                        bufferedOutputStream.write(temp);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // FileChannel 耗时1s左右
    public static void zipFileChannel(String zipFilePath, String... filePaths) {
        long beginTime = System.currentTimeMillis();
        File zipFile = new File(zipFilePath);
        try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
             WritableByteChannel writableByteChannel = Channels.newChannel(zipOut)) {
            for (String filePath : filePaths) {
                try (FileChannel fileChannel = new FileInputStream(filePath).getChannel()) {
                    zipOut.putNextEntry(new ZipEntry(filePath.substring(filePath.lastIndexOf(File.separator) + 1)));
                    fileChannel.transferTo(0, fileChannel.size(), writableByteChannel);
                }
            }
            System.out.println(System.currentTimeMillis() - beginTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //使用Map内存映射文件 耗时1s左右
    public static void zipFileMap(String zipFilePath, String... filePaths) {
        File zipFile = new File(zipFilePath);
        try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
             WritableByteChannel writableByteChannel = Channels.newChannel(zipOut)) {
            for (String filePath : filePaths) {
                zipOut.putNextEntry(new ZipEntry(filePath.substring(filePath.lastIndexOf(File.separator) + 1)));
                //内存中的映射文件
                MappedByteBuffer mappedByteBuffer = new RandomAccessFile(filePath, "r").getChannel()
                        .map(FileChannel.MapMode.READ_ONLY, 0, new File(filePath).length());

                writableByteChannel.write(mappedByteBuffer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //使用Pipe 异步执行
    public static void zipFilePip(String zipFilePath, String... filePaths) {
        try (WritableByteChannel out = Channels.newChannel(new FileOutputStream(zipFilePath))) {
            Pipe pipe = Pipe.open();
            //异步任务
            CompletableFuture.runAsync(() -> runTask(pipe, filePaths));
            //获取读通道
            ReadableByteChannel readableByteChannel = pipe.source();
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            while (readableByteChannel.read(buffer) >= 0) {
                buffer.flip();
                out.write(buffer);
                buffer.clear();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //异步任务
    public static void runTask(Pipe pipe, String... filePaths) {
        try (ZipOutputStream zos = new ZipOutputStream(Channels.newOutputStream(pipe.sink()));
             WritableByteChannel out = Channels.newChannel(zos)) {
            for (String filePath : filePaths) {
                zos.putNextEntry(new ZipEntry(filePath.substring(filePath.lastIndexOf(File.separator) + 1)));
                File file = new File(filePath);
                FileChannel jpgChannel = new FileInputStream(file).getChannel();
                jpgChannel.transferTo(0, file.length(), out);
                jpgChannel.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值