Search code examples
language-agnosticziparchive

Create zip with a precise fixed size


I'd like to create a zip file with a specific size, e.g. 10.000.000 byte.
Let's say that I have to zip a number of files, and they sum up to 8.123.456 byte (random number) once zipped.
I'd like to add "rubbish" to reach exactly the size I chose (10Mb in this case).

Of course I'm assuming the files I have to zip do not exceed my limit.

I don't have any specific language requirement to do that.


Solution

  • Just concatenate a "rubbish" file and a valid Zip file that match together your 10Mb. The Zip format was made for such situations. Typically, you can concatenate an executable and a Zip file for making an installer or a self-extracting archive.

    Here an example (I concatenate a randomly chosen GIF and a Zip). The Info-Zip unzip tool says:

    unzip -v appended.zip
    Archive:  appended.zip
    warning [appended.zip]:  1772887 extra bytes at beginning or within zipfile
      (attempting to process anyway)
     Length   Method    Size  Ratio   Date   Time   CRC-32    Name
    --------  ------  ------- -----   ----   ----   ------    ----
         186  Defl:N      140  25%  15.02.12 12:03  56202537  zip-ada/debg_za.cmd
         349  Defl:N      202  42%  10.02.12 22:19  7718ccec  zip-ada/debug.pra
        4357  Defl:N     1381  68%  24.09.16 06:43  30f2fef0  zip-ada/demo/demo_csv_into_zip.adb
        1015  Defl:N      513  50%  02.10.18 15:49  f0edcf97  zip-ada/demo/demo_unzip.adb
         603  Defl:N      310  49%  20.03.16 08:26  b3906614  zip-ada/demo/demo_zip.adb
      161483  Defl:N    39845  75%  27.08.16 16:35  9f24d1fe  zip-ada/doc/appnote.txt
    ...
    

    The decompression test is successful (as expected):

    unzip -t appended.zip
    Archive:  appended.zip
    warning [appended.zip]:  1772887 extra bytes at beginning or within zipfile
      (attempting to process anyway)
        testing: zip-ada/debg_za.cmd      OK
        testing: zip-ada/debug.pra        OK
        testing: zip-ada/demo/demo_csv_into_zip.adb   OK
        testing: zip-ada/demo/demo_unzip.adb   OK
        testing: zip-ada/demo/demo_zip.adb   OK
        testing: zip-ada/doc/appnote.txt   OK
        testing: zip-ada/doc/lzma-specification.txt   OK
    ...
    No errors detected in compressed data of appended.zip.