1

I have a requirement where I need to compress large files in GZip format using Java's GZIPOutputStream, but the target file should not cross 10MB. If it crosses, need to close the zip and open another one.

public void compress() throws Exception {
        try (FileInputStream inputStream = new FileInputStream("compressme.txt");
        OutputStream outputStream = new GZIPOutputStream(new FileOutputStream("compressed.gzip"))) {
            byte buffer[] = new byte[4096];
            int len = 0;
            while ((len = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, len);
                // TODO: watch if the target file can grow more than 10MB
            }
        }
    }

I have tried java.io.File.length() on the target file but that doesn't seem to be giving the accurate size. Please suggest if there is a way to achieve it.

Thank you, Swathi

Swathi P
  • 11
  • 1

0 Answers0