在S3上解压缩base64 gzip并保存到本地文件系统

时间:2018-03-23 04:43:24

标签: java base64 gzip

我是Java骨头/新手所以请保持温柔。我有两个功能,我发现它们有些不兼容:

  1. saveS3toFilesystem - 从AWS S3获取InputStream并将其作为文件保存到本地文件系统
  2. decompress - 获取字符串并解码base64编码并解压缩gzip压缩。
  3. 我真的希望这两个协同工作以实现保存到文件系统的文件的结果是未压缩的文件,但我意识到我的“解压缩”功能应该更改为接收流而不是字符串但遗憾的是如今,我只是Java世界中的“刀具和贴纸”。

    以下是我现在的两个功能:

    private void saveS3toFilesystem(String filename, String bucketName, String localFilename) {
      S3Object obj = s3.getObject(bucketName, filename);
      InputStream in = obj.getObjectContent();
      try {
        Files.createDirectories(Paths.get(localFilename.replace(this.FILE_EXTENSION, "")));
        Files.copy(in, Paths.get(localFilename));
        this.logger.log("Input file has been placed in local filesystem for ITMS to pick up: " + localFilename + "\n");
      } catch (IOException err) {
        this.logger.log("There was a problem saving the file to " + localFilename);
        err.printStackTrace();
      } finally {
        try {
          in.close();
        } catch (IOException err) {
          err.printStackTrace();
        }
      }
      return;
    }
    

    然后......

    private String decompress(String compressedZip) {
      byte[] decodedBytes = Base64.getDecoder().decode(compressedZip);
      String result = null;
      GZIPInputStream zip = null;
      try {
        zip = new GZIPInputStream(new ByteArrayInputStream(decodedBytes));
        result = IOUtils.toString(zip);
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        IOUtils.closeQuietly(zip);
      }
      return result;
    }
    

    任何人都可以帮助我实现梦想吗?很高兴用流,字符串或任何可行的方法来做。可悲的是,我无法负担得起我的Java技能,足以让我自己解决问题。

    非常感谢提前。

1 个答案:

答案 0 :(得分:1)

基于以下API: Base64.DecoderGZIPInputStream(查看前者的wrap方法和后者的构造函数),解压缩方法可以按如下方式重载:

private String decompress(InputStream compressedStream) {
  InputStream decodingStream = Base64.getDecoder().wrap(compressedStream);
  String result = null;
  GZIPInputStream zip = null;
  try {
    zip = new GZIPInputStream(decodingStream);
    result = IOUtils.toString(zip);
  } catch (IOException e) {
    e.printStackTrace();
  } finally {
    IOUtils.closeQuietly(zip);
  }
  return result;
}

最后,saveS3toFilesystem的更改如下:

private void saveS3toFilesystem(String filename, String bucketName, String localFilename) {
  S3Object obj = s3.getObject(bucketName, filename);
  InputStream in = obj.getObjectContent();
  // decoding the inputstream via decode into a string, which is then
  // used in order to create an inputstream of decoded data
  InputStream decodedStream = 
     new ByteArrayInputStream(decompress(in).getBytes(StandardCharsets.UTF_8));
  try {
    Files.createDirectories(Paths.get(localFilename.replace(this.FILE_EXTENSION, "")));
    Files.copy(decodedStream, Paths.get(localFilename));
    this.logger.log("Input file has been placed in local filesystem for ITMS to pick up: " + localFilename + "\n");
  } catch (IOException err) {
    this.logger.log("There was a problem saving the file to " + localFilename);
    err.printStackTrace();
  } finally {
    try {
      in.close();
    } catch (IOException err) {
      err.printStackTrace();
    }
  }
  return;
}