Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Compression to fixed size In Java

I want to perform file compression using zip package of java.util library. The aim is to limit the compressed file to a fixed size. If the compressed file size is greater than this limit, it should be split into multiple files.

try {
            fos = new FileOutputStream(p_request.getOutputFilePath() + zipFileName);
            ZipOutputStream zos = new ZipOutputStream(fos);
            zipEntry1 = new ZipEntry(f.getName());
            fis = new FileInputStream(f.getAbsolutePath());
            int count;
            while ((count = fis.read(fileRAW, 0, BUFFER)) != -1) {
              zipEntry1 = new ZipEntry(f.getName());
              if (currentSize >= (p_request.getMaxSizePerFileInMB() * 1024 * 1024)) {
                zipSplitCount++;
                zos.close();
                zos = new ZipOutputStream(new FileOutputStream(
                    p_request.getOutputFilePath() + zipFileName
                        + "_" + zipSplitCount + ".zip"));
                currentSize = 0;
              }
              zos.putNextEntry(zipEntry1);
//              zos.closeEntry();
              currentSize += zipEntry1.getCompressedSize();
              zos.write(fileRAW, 0, count);
            }

I always get compressed size as -1. Can someone advise a clean approach for this?

EDIT:

So I compressed the file into chunks of fixed size to get multi parts compressed zip of the same file as f.1.zip, f.2.zip. Now when I decompress it, is there some way to restore the original file? Currently, it says the file must be broken.

byte[] buffer = new byte[BUFFER];
        ZipInputStream zis = null;
        try {
          zis = new ZipInputStream(new FileInputStream(f.getAbsolutePath()));
          ZipEntry zipEntry = zis.getNextEntry();

          while(zipEntry!=null){

            String fileName = zipEntry.getName();
            File newFile = new File(p_request.getOutputFilePath() + fileName);

            System.out.println("file unzip : "+ newFile.getAbsoluteFile());

            new File(newFile.getParent()).mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0) {
              fos.write(buffer, 0, len);
            }

            fos.close();
            zipEntry = zis.getNextEntry();
          }

          zis.closeEntry();
          zis.close();
like image 845
Harshit Gupta Avatar asked Nov 19 '25 11:11

Harshit Gupta


1 Answers

You are getting -1 because the size is not known until the Zip file has been written to disk. Compression takes place when you save the entire zip file, not when you add a new entry.

This means that you have to either:

  • write the zip to disk after adding each file and then measuring the zip to determine whether to keep adding to it or creating a new file
  • or guesstimate the size based on an average compression rate and the size of the file before compression on disk.
like image 182
David Brossard Avatar answered Nov 22 '25 00:11

David Brossard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!