Read, encrypt, zip and send a file without rewriting it

102 Views Asked by At

I have a to read a file, zip it, encrypt it with AES algorithm and send it to an S3 bucket.

The workflow of my actual code is :

  1. Read file and write it as a zip file (Files.copy(filePath, zipOutputStream) from java.nio.file
  2. Read the zip file, encrypt and rewrite it

"Pseudocode" for 1. and 2.

    fileList.foreach(localFile => {
      zipOutputStream.putNextEntry(new ZipEntry(localFile.toPath.getFileName.toString))
      file.Files.copy(localFile.toPath, zipOutputStream)
      encryptAndReplace(localFile, someEncryptionConfig)
    })
  1. Read the encrypted zip and send it to s3 (FileInputStream to S3OutputStreamWrapper)

I want to do all those steps without rewriting the file 2 times and avoid IO to save time.

How can i process to optimize my workflow ?

If i return an InputStream in my step 1. instead of rewriting it. And then re-return an InputStream in my step 2. and then send it to S3, does is means that my 2 methods will return the whole file ? And that my whole file will be stored in memory ? What should i take care of if I process in this way ?

1

There are 1 best solutions below

0
Rob Spoor On

You don't need to read and write the file for encryption. Using CipherOutputStream you can do everything in one go.

Cipher cipher = ...;
try (OutputStream fileOut = Files.newOutputStream(...);
        CipherOutputStream cipherOut = new CipherOutputStream(fileOut, cipher);
        ZipOutputStream zipOut = new ZipOutputStream(cipherOut)) {

    // add entries to zipOut
}

I think you can even replace fileOut with the S3OutputStreamWrapper and do everything in one go.