How to zip files with a size limit?

Solution 1:

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

Solution 2:

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 -

Solution 3:

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

Tags:

Linux

Bash

Gzip