Eugène Adell
Eugène Adell

Reputation: 3174

How to mix files of compressed and stored types in the same zip file

I am looking for a shell command (preferably a one-liner) that will create a zip file with both compressed, and stored content (by stored, I mean uncompressed as stated in the official documentation, link 1).

The .ZIP File Format Specification gives freedom of mixing different compression types, including just storing files :

4.1.8 Each data file placed into a ZIP file MAY be compressed, stored, encrypted or digitally signed independent of how other data files in the same ZIP file are archived.

If this was necessary, this technical possibility is confirmed by the Media Type registered in the IANA registry under application/zip :

A. Local file header:

local file header signature 4 bytes (0x04034b50) .. compression method 2 bytes

Till now I have tried unsuccessfully several zip parameters (-f -u -U,..)

Ideally the command would compress text files, and store binary content, differentiated by their file extension (for example : html, css, js would be considered as text, and jpg, ico, jar as binary).

Upvotes: 3

Views: 418

Answers (2)

Aurel Bílý
Aurel Bílý

Reputation: 7963

Adding to @cody's answer, you can also do this on a per-file (group) basis with -g and -0. Something like:

zip archive.zip compressme.txt
zip -g archive.zip -0 dontcompressme.jpg
-#
(-0, -1, -2, -3, -4, -5, -6, -7, -8, -9)
       Regulate the speed of compression using the specified digit #, where -0
       indicates no compression (store all files), -1 indicates the fastest 
       compression speed (less compression) and -9 indicates the slowest
       compression speed (optimal compression, ignores the suffix list).
       The default compression level is -6.

-g
--grow
       Grow  (append  to) the specified zip archive, instead of creating a new one.
       If this operation fails, zip attempts to restore the archive to its original
       state. If the restoration fails, the archive might become corrupted.
       This option is ignored when there's no existing archive or when at least
       one archive member must be updated or deleted.

Upvotes: 2

cody
cody

Reputation: 11157

Are you looking for the -n flag?

-n suffixes
--suffixes suffixes

 Do not attempt to compress files named with the given suffixes. Such files are simply
 stored (0% compression) in the output zip file, so that zip doesn't waste its time trying
 to compress them. The suffixes are separated by either colons or semicolons. For example:

       zip -rn .Z:.zip:.tiff:.gif:.snd  foo foo

 will copy everything from foo into foo.zip, but will store any files that end in .Z,
 .zip, .tiff, .gif, or .snd without trying to compress them.

Upvotes: 6

Related Questions