0

I have a MultipartFile and I need to compress inputStream as gzip and sent it, but I need to find a way to compress it and know the compressed size of it


param: MultipartFile file

        try(var inputStream = file.getInputStream()) {

            var outputStream = new GZIPOutputStream(OutputStream.nullOutputStream());

            IOUtils.copyLarge(inputStream, outputStream);

           var compressedInputStream = someConvertMerthod(outputStream);


            sendCompressed(compressedInputStream, compressedSize)
        }

Maybe I can do something like this Java: How do I convert InputStream to GZIPInputStream? but I am not gonna be a able to get the compressedSize

I am not finding an easy way to do it :(

2 Answers2

0

CopyLarge() returns the number of bytes copied. I would assume this is true even if the output is discarded, so all you need is to capture the return value of IOUtils.copyLarge(in,out) and you should be good to go, but this does assume the return value is bytes WRITTEN and not bytes READ, which is not really documented. So it might work!

In general though, you are assuming you can turn the output stream back into an input stream, so nullOutputStream() is not going to be an option. Instead you will be creating a temp file, writing your compressed data to it, and then closing it. At that point you can simply ask the file system API how big it is, that should be iron clad.

Tod Harter
  • 516
  • 5
  • 8
  • I just tried that, it will return the byte size that was transferred, not there compressed – Edgar Silva Feb 01 '23 at 19:59
  • Yeah, I was just adding a bit to my answer. Kinda figures. You will just have to use a temp file, which you'd probably need anyway. I think IOUtils has routines to make all that easy, or you can use the newer style Path interface. – Tod Harter Feb 01 '23 at 20:01
0

hey I think I found the solution :)

    param: MultipartFile file

            try (InputStream inputStream = file.getInputStream()) {

                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);

                inputStream.transferTo(gzipOutputStream);
                InputStream compressedInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());

                byteArrayOutputStream.size() // is the compressed size      


            }

Thanks guys!