30

I have a script that zip files from a folder. I want to make sure that the zipped file is not more than 10 MB. If the size is more than 10MB, it should create another ZIP file.

Is there any command (or other method )that can be used for this?

Desperatuss0ccus
  • 252
  • 1
  • 4
  • 9
Vishnu
  • 701
  • 2
  • 8
  • 13
  • 1
    The question has the *gzip* tag but the text refers to *zip*, can you clarify? (The answers are divided between approaches based on the different formats.) – Håkan Lindqvist Feb 29 '16 at 17:58
  • I cannot add zip into the tag thats why I used gzip, If you have a gzip method that I can fit with this problem, I can use that too. – Vishnu Feb 29 '16 at 18:40

3 Answers3

39

You can use the "split archive" functionality of "zip" itself using the "--split-size" option.

From "zip" manpage ("man zip"):

(...)

One use of split archives is storing a large archive on multiple remov‐
able media. For a split archive with 20 split files the files are typ‐
ically named (replace ARCHIVE with the name of your archive) AR‐
CHIVE.z01, ARCHIVE.z02, ..., ARCHIVE.z19, ARCHIVE.zip. Note that the
last file is the .zip file.

(...)

-s splitsize
--split-size splitsize

Split size is a number optionally followed by a multiplier.
Currently the number must be an integer. The multiplier can
currently be one of k (kilobytes), m (megabytes), g (gigabytes),
or t (terabytes). As 64k is the minimum split size, numbers
without multipliers default to megabytes. For example, to cre‐
ate a split archive called foo with the contents of the bar
directory with splits of 670 MB that might be useful for burning
on CDs, the command:

                zip -s 670m -r foo bar

could be used.

So, to create a split zip archive, you could do the following (the "-r" is the "recursive" switch to include subdirectories of the directory):

$ zip -r -s 10m archive.zip directory/

To unzip the file, the "zip" manpage explains that you should use the "-s 0`" switch:

(...)

 zip -s 0 split.zip --out unsplit.zip

will convert a split archive to a single-file archive.

(...)

So, you first "unsplit" the ZIP file using the "-s 0" switch:

$ zip -s 0 archive.zip --out unsplit.zip

... and then you unzip the unsplit file:

$ unzip unsplit.zip

ricmarques
  • 1,112
  • 1
  • 13
  • 23
  • Your argument about using --out to unsplit is actually incorrect: " -O (--output-file or --out) zip inarchive.zip foo.c bar.c --out outarchive.zip reads archive inarchive.zip, even if split, adds the files foo.c and bar.c, and writes the resulting archive to outarchive.zip. If inarchive.zip is split then outarchive.zip defaults to the same split size. Be aware that if outarchive.zip and any split files that are created with it already exist, these are always overwritten as needed without warning. This may be changed in the future." – eco Sep 10 '21 at 16:02
11
tar -czvf - /path/to/files | split -b 10M - archive.tar.gz

Will give you a number of files:

archive.tar.gzaa

archive.tar.gzab

...

Which then can be uncompressed with:

cat archive.tar.* | tar -xzvf -
Desperatuss0ccus
  • 252
  • 1
  • 4
  • 9
5

Here's how I did it for a 5GB file (split into 1GB vs 10MB as OP asked)...

Example: To split a 5GB file into 1GB files to copy to a FAT32 USB (filename "FIVE_GB_FILE.ISO")

Step 1: zip the file (no compression, same directory as source)

zip -0 FIVE_GB_FILE.ZIP FIVE_GB_FILE.ISO

Step 2: split the 5GB zip file into 1GB zip files

zip -s 1000m SPLIT_5GB_FILES FIVE_GB_FILE.ZIP

Voila... you should have the following 1GB files (AND the original, AND the zip from step 1)

SPLIT_5GB_FILES.zip
SPLIT_5GB_FILES.Z01
SPLIT_5GB_FILES.Z02
SPLIT_5GB_FILES.Z03
SPLIT_5GB_FILES.Z04
Jay Marm
  • 161
  • 1
  • 4
  • Thanks for this answer. But, what to do when unzipping? – Maged Saeed Jan 20 '20 at 10:59
  • @maged-saeed pass unzip the first of the multi-zipped files and It automatically processes all of the children until finished. – Jay Marm Jan 20 '20 at 20:56
  • @JayMarm Incorrect: unzip ssm-confluent-linux_6.2.0.z01 Archive: ssm-confluent-linux_6.2.0.z01 End-of-central-directory signature not found. Either this file is not a zipfile, or it constitutes one disk of a multi-part archive. In the latter case the central directory and zipfile comment will be found on the last disk(s) of this archive. unzip: cannot find zipfile directory in one of ssm-confluent-linux_6.2.0.z01 or ssm-confluent-linux_6.2.0.z01.zip, and cannot find ssm-confluent-linux_6.2.0.z01.ZIP, period. And when passing the LAST file: – eco Sep 10 '21 at 15:47
  • @JayMarm: unzip ssm-confluent-linux_6.2.0.zip Archive: ssm-confluent-linux_6.2.0.zip warning [ssm-confluent-linux_6.2.0.zip]: zipfile claims to be last disk of a multi-part archive; attempting to process anyway, assuming all parts have been concatenated together in order. Expect "errors" and warnings...true multi-part support doesn't exist yet (coming soon). file #1: bad zipfile offset (local header sig): 4 – eco Sep 10 '21 at 15:48