searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

java如何将数据库中的文本打包成压缩文件并提供下载接口

2023-05-30 02:52:41
8
0
    1. 场景
      一个环境下有多个集群,每个集群下有多个配置,这些配置保存在数据库中,需要提供一个接口用来下载整个环境下的配置(压缩包),压缩包的目录层次为
config.tar.gz/
├─ config/
│  ├─ yarn/
│  │  ├─ yarn-site.xml
│  ├─ hdfs/
│  │  ├─ core-site.xml
│  │  ├─ hdfs-site.xml
 
    2. 代码
 
      2.1 引入依赖
      我们需要使用commons-compress来完成tar这种格式的压缩
 
      2.2 实现
 


    @Data
    public static class ClusterClientConfig {


        private String clusterName;


        private List<ClientConfig> configs;


    }


    @Data
    public static class ClientConfig {


        private String filename;


        private String content;


    }
  @SneakyThrows
    public void download(List<ClusterClientConfig> clusters, HttpServletResponse response) {


        String filenamePrefix = "config";
        String filenameSuffix = ".tar.gz";


        Path path = tarGzip(clusters, filenamePrefix, filenameSuffix);
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition",
                "attachment; filename=" + URLEncoder.encode(filenamePrefix + filenameSuffix, "UTF-8"));


        try (InputStream from = Files.newInputStream(path); ServletOutputStream to = response.getOutputStream()) {
            long length = ByteStreams.copy(from, to);
            response.setContentLengthLong(length);
        }
    }
    @SneakyThrows
    private static Path tarGzip(List<ClusterClientConfig> clusters, String filenamePrefix, String filenameSuffix) {
        Path outputTarGzip = Files.createTempFile(filenamePrefix, filenameSuffix);


        try (OutputStream fOut = Files.newOutputStream(outputTarGzip);
             BufferedOutputStream buffOut = new BufferedOutputStream(fOut);
             GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(buffOut);
             TarArchiveOutputStream tOut = new TarArchiveOutputStream(gzOut)) {


            clusters.forEach(clusterClientConfig -> {
                clusterClientConfig.configs.forEach(clientConfig -> {
                    String path= filenamePrefix + "/" + clusterClientConfig.clusterName + "/" + clientConfig.getFilename();
                    createTarArchiveEntry(path, clientConfig.getContent(), tOut);
                });


            });


            tOut.finish();
        }
        return outputTarGzip;
    }


    @SneakyThrows
    private static void createTarArchiveEntry(String fileName,
                                              String data,
                                              TarArchiveOutputStream tOut) {
        byte[] bytes = data.getBytes(StandardCharsets.UTF_8);


        TarArchiveEntry tarEntry = new TarArchiveEntry(fileName);
        tarEntry.setSize(bytes.length);
        tOut.putArchiveEntry(tarEntry);


        ByteStreams.copy(new ByteArrayInputStream(bytes), tOut);


        tOut.closeArchiveEntry();


    }​
0条评论
作者已关闭评论
l****n
6文章数
0粉丝数
l****n
6 文章 | 0 粉丝
原创

java如何将数据库中的文本打包成压缩文件并提供下载接口

2023-05-30 02:52:41
8
0
    1. 场景
      一个环境下有多个集群,每个集群下有多个配置,这些配置保存在数据库中,需要提供一个接口用来下载整个环境下的配置(压缩包),压缩包的目录层次为
config.tar.gz/
├─ config/
│  ├─ yarn/
│  │  ├─ yarn-site.xml
│  ├─ hdfs/
│  │  ├─ core-site.xml
│  │  ├─ hdfs-site.xml
 
    2. 代码
 
      2.1 引入依赖
      我们需要使用commons-compress来完成tar这种格式的压缩
 
      2.2 实现
 


    @Data
    public static class ClusterClientConfig {


        private String clusterName;


        private List<ClientConfig> configs;


    }


    @Data
    public static class ClientConfig {


        private String filename;


        private String content;


    }
  @SneakyThrows
    public void download(List<ClusterClientConfig> clusters, HttpServletResponse response) {


        String filenamePrefix = "config";
        String filenameSuffix = ".tar.gz";


        Path path = tarGzip(clusters, filenamePrefix, filenameSuffix);
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition",
                "attachment; filename=" + URLEncoder.encode(filenamePrefix + filenameSuffix, "UTF-8"));


        try (InputStream from = Files.newInputStream(path); ServletOutputStream to = response.getOutputStream()) {
            long length = ByteStreams.copy(from, to);
            response.setContentLengthLong(length);
        }
    }
    @SneakyThrows
    private static Path tarGzip(List<ClusterClientConfig> clusters, String filenamePrefix, String filenameSuffix) {
        Path outputTarGzip = Files.createTempFile(filenamePrefix, filenameSuffix);


        try (OutputStream fOut = Files.newOutputStream(outputTarGzip);
             BufferedOutputStream buffOut = new BufferedOutputStream(fOut);
             GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(buffOut);
             TarArchiveOutputStream tOut = new TarArchiveOutputStream(gzOut)) {


            clusters.forEach(clusterClientConfig -> {
                clusterClientConfig.configs.forEach(clientConfig -> {
                    String path= filenamePrefix + "/" + clusterClientConfig.clusterName + "/" + clientConfig.getFilename();
                    createTarArchiveEntry(path, clientConfig.getContent(), tOut);
                });


            });


            tOut.finish();
        }
        return outputTarGzip;
    }


    @SneakyThrows
    private static void createTarArchiveEntry(String fileName,
                                              String data,
                                              TarArchiveOutputStream tOut) {
        byte[] bytes = data.getBytes(StandardCharsets.UTF_8);


        TarArchiveEntry tarEntry = new TarArchiveEntry(fileName);
        tarEntry.setSize(bytes.length);
        tOut.putArchiveEntry(tarEntry);


        ByteStreams.copy(new ByteArrayInputStream(bytes), tOut);


        tOut.closeArchiveEntry();


    }​
文章来自个人专栏
文章 | 订阅
0条评论
作者已关闭评论
作者已关闭评论
0
0