0

I would like to make a ZIP file using zip command. Zip file would contain large number of directories containing large numbers of subdirectories, files etc.

Is there any way to calculate final size of ZIP file, but before doing actual zipping on the disk?

For example: Calculating final ZIP file size to determine if ZIP will fit into specific size container (we cannot perform on disk operation even in tmp, because if it will not fit, then zipping will fail without giving info on archive size)

3

From the zip man page:

Streaming input and output. zip will also accept a single dash ("-") as the zip file name, in which case it will write the zip file to standard output, allowing the output to be piped to another program.

So:

$ zip -r - foo | wc -c

Will tell you the compressed size in bytes of the directory foo.

7z can not write to stdout for zip files.

The other alternative is to create a memory disk and compress to it.

2

You can compress to StdOut and count the bytes, but AFAIK zip doesn't zip to stdout (and neither does 7z, the other utility that can produce a ZIP format)(*).

On Linux you typically produce .tar.gz files and the utilities output to stdout if no output file is specified:

# compute size
outputSize=$(tar -cz the_directories | wc -c)
# create file
tar -czf some.tar.gz the_directories

Note that in any case you do the compression twice, you cannot really predict the output size without performing the conversion.

(*) possibly due to the fact that you cannot decode a ZIP file without seeing the end first.

  • D’oh! Writing to stdout (if you can) is simpler than messing with a pipe / process substitution. – G-Man Oct 21 '18 at 22:36
  • zip is able to compress to stdout. – Isaac Oct 22 '18 at 2:36
0

Do

tar  cf  >(wc -c)  <file_name_arguments>;  sleep 1
Rather than writing to a file, tar will write to a pipe to a wc -c command, which will report the number of bytes written to it.  (Include any tar options you need to get it to do compression.)  This uses bash’s “process substitution” functionality.  Use the sleep 1 because, without it, your shell will issue its next shell prompt when the tar command finishes, and will not wait for the wc command to finish.

If you don’t have bash, you can probably accomplish the same thing with a named pipe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.