0

I'm trying to take some files/file content from a GCS storage bucket, add them to a ZIP archive, and save that archive file in a GCS bucket using a Java Cloud Function.

I'm able to read files and write normal (non-zip) files just fine. However, I'm unclear on how I could use the available Java API (bucket.create(String fileName, byte[] content) to post a ZIP archive. I have the bytes of the individual files I want to archive but how do I get the bytes of the ZIP file itself? Or is there another API I should be looking at?

I can't find any examples of doing this in Java. I found this source about how to do this in Python and this Stack Overflow article on how to do it within Google App Engine, but neither of these apply to my use case.

erronius
  • 49
  • 2
  • 7
  • 1
    You upload the zip file like any other file. On the other hand your question is not clear, what do you mean by `how do I get the bytes of the ZIP file itself`. Why the App Engine approach will not work for your use case? – Puteri Feb 24 '22 at 23:25

2 Answers2

2

A zip file is a file. Therefore, you have to download all the file that you have to add in your zip file, create the zip file and send that zip file to GCS.

You can create ZIP file in JAVA like that.

keep in mind that the /tmp directory is an in memory and the writable directory on Cloud Functions. Because it's in memory, the size of the file PLUS the size of the zip mustn't break that limit.

guillaume blaquiere
  • 66,369
  • 2
  • 47
  • 76
0

The key I was missing was the ability to get the bytes of the ZIP file that's being generated. You can't do this directly from the ZipOutputStream, but you can get the bytes from the "inner" ByteArrayOutputStream (or presumably another type of OutputStream) like so:

byte[] result = null;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ZipOutputStream zipOut = new ZipOutputStream(outputStream)) {
            //Do some writing from wherever you got your content
            zipOut.close();
            outputStream.close();
            result = outputStream.toByteArray();

This byte[] can then be written to GCS.

erronius
  • 49
  • 2
  • 7