Linux determine ZIP size without compression












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)










share|improve this question





























    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)










    share|improve this question



























      0












      0








      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)










      share|improve this question















      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)







      linux zip compression gzip archive






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 12 at 11:02

























      asked Oct 21 at 22:02









      Jakub Pastuszuk

      1063




      1063






















          3 Answers
          3






          active

          oldest

          votes


















          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.






          share|improve this answer





























            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.






            share|improve this answer





















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










            • zip is able to compress to stdout.
              – Isaac
              Oct 22 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.






            share|improve this answer





















              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "106"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476916%2flinux-determine-zip-size-without-compression%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              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.






              share|improve this answer


























                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.






                share|improve this answer
























                  3












                  3








                  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.






                  share|improve this answer












                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 22 at 2:31









                  Isaac

                  11k11648




                  11k11648

























                      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.






                      share|improve this answer





















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










                      • zip is able to compress to stdout.
                        – Isaac
                        Oct 22 at 2:36
















                      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.






                      share|improve this answer





















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










                      • zip is able to compress to stdout.
                        – Isaac
                        Oct 22 at 2:36














                      2












                      2








                      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.






                      share|improve this answer












                      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.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Oct 21 at 22:31









                      xenoid

                      2,6681724




                      2,6681724












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










                      • zip is able to compress to stdout.
                        – Isaac
                        Oct 22 at 2:36


















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










                      • zip is able to compress to stdout.
                        – Isaac
                        Oct 22 at 2:36
















                      D’oh! Writing to stdout (if you can) is simpler than messing with a pipe / process substitution.
                      – G-Man
                      Oct 21 at 22:36




                      D’oh! Writing to stdout (if you can) is simpler than messing with a pipe / process substitution.
                      – G-Man
                      Oct 21 at 22:36












                      zip is able to compress to stdout.
                      – Isaac
                      Oct 22 at 2:36




                      zip is able to compress to stdout.
                      – Isaac
                      Oct 22 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.






                      share|improve this answer


























                        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.






                        share|improve this answer
























                          0












                          0








                          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.






                          share|improve this answer












                          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.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Oct 21 at 22:28









                          G-Man

                          12.9k93264




                          12.9k93264






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476916%2flinux-determine-zip-size-without-compression%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Morgemoulin

                              Scott Moir

                              Souastre