javainputstreamgzipinputstream

InputStream to GZIPInputStream, and I need to know the size of the GZIPInputStream


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 :(


Solution

  • 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!