We were packing our builds into ZIP archives for distribution right in TeamCity using its built-in artifact packaging. But then we decided to try to do that explicitly with 7-Zip as a dedicated build step, and that turned out to be a faster option, providing a better compression ratio too.

7-Zip packing

The only question remaining was finding the right balance between compression level and compression time.

So, the goal is to create a ZIP archive with a good balance between compression ratio and packing time using 7-Zip (on Linux). Our build artifacts are mostly binary .dll’s and plain-text files (.h, .cmake, etc).

Why ZIP and not 7z? Well, unfortunatelly, quite a lot of users simply won’t know what to do with a file named some-build.7z, so we have to stick with good old ZIP.

7-Zip command line parameters

How to create a ZIP archive

$ 7z a -tzip some-build.zip some-build-artifacts

here:

  • -tzip - type of archive: ZIP

Maximum compression level

$ 7z a -tzip -mx9 some-build.zip some-build-artifacts

here:

  • -mx9 - compression level: 9 (maximum compression)

Specific number of threads

If you won’t set the number of threads explicitly, 7-Zip seems to default to all the available threads, which I think is a good default behaviour. You can set a specific number of threads too:

$ 7z a -tzip -mx9 -mmt12 some-build.zip some-build-artifacts

here:

  • -mmt12 - number of threads to use: 12

If the machine has only 12 threads available, then providing --mmt12 seems to be futile, as it will use all 12 by default anyway.

Finding optimal parameters

Now let’s see what results do we get with various combinations of compression level and numbers of threads.

Testing machine:

  • Intel Core i7-5820K
  • 12 threads
  • 3.30GHz

Results:

Compression Threads Time Original size Archived size
1 default default 00:02:33.359 3399145366 1045577064
2 mx1 default 00:00:25.220 3399145366 1107968010
3 mx3 default 00:00:25.610 3399145366 1107968010
4 mx5 default 00:02:32.504 3399145366 1045577064
5 mx7 default 00:06:15.133 3399145366 1035188596
6 mx9 default 00:15:32.641 3399145366 1032895709
7 mx9 mmt12 00:15:38.725 3399145366 1032895709

So with default parameters the packing on this particular machine seems to be done with mx5 compression level and all the 12 available threads.

Graphical representation of the results:

Comparing 7-Zip compression levels

Given quite a little difference in resulting archived size and a quite huge difference in time, the option #3 is the most optimal in terms of time/compression for our case.

So, the resulting set of parameters:

$ 7z a -tzip -mx3 some-build.zip some-build-artifacts