8 Replies Latest reply: Aug 21, 2008 3:29 PM by 807589 RSS

    How to create an empty ZIP file?

    807589
      Hi,

      when I try to create a ZIP file using java.util.zip.ZipOutputStream,

      never call putNextEntry(ZipEntry),

      but do close the stream,

      the result is a 0-byte file instead of a valid empty ZIP file.

      What am I doing wrong?

      Thanks.
        • 1. Re: How to create an empty ZIP file?
          800606
          Maybe you could try to take any zip file and remove all its contents...
          • 2. Re: How to create an empty ZIP file?
            807589
            alexxzius wrote:
            Maybe you could try to take any zip file and remove all its contents...
            helpful as ever, alexxzius.

            kevang: Your best bet here may be to condense your question/problem into a single small class that is compilable by any and all of us, and demonstrates your problem. In other words, a [Short, Self Contained, Correct (Compilable), Example or SSCCE|http://homepage1.nifty.com/algafield/sscce.html] .

            Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. You may want to click on the Preview tab to make sure that your code is formatted correctly. Another way is to place the tag [code] at the top of your block of code and the tag [/code] at the bottom, like so:
            [code]
              // your code block goes here.
              // note the differences between the tag at the top vs the bottom.
            [/code]
            or
            {code}
              // your code block goes here.
              // note here that the tags are the same.
            {code}
            good luck
            • 3. Re: How to create an empty ZIP file?
              807589
              kevang wrote:
              Hi,

              when I try to create a ZIP file using java.util.zip.ZipOutputStream,

              never call putNextEntry(ZipEntry),

              but do close the stream,

              the result is a 0-byte file instead of a valid empty ZIP file.
              Okay.

              >
              What am I doing wrong?
              Why have you decided that something is wrong? What do you feel an empty ZIP file should have except no content?

              Anyway. What's your real goal here? What problem are you trying to solve exactly?
              • 4. Re: How to create an empty ZIP file?
                807589
                cotton.m wrote:
                What do you feel an empty ZIP file should have except no content?
                http://www.pkware.com/documents/casestudies/APPNOTE.TXT - section V, item A.
                • 5. Re: How to create an empty ZIP file?
                  807589
                  What is the purpose of this empty file?
                  • 6. Re: How to create an empty ZIP file?
                    807589
                    Encephalopathic wrote:
                    kevang: Your best bet here may be to condense your question/problem into a single small class that is compilable by any and all of us, and demonstrates your problem. In other words, a [Short, Self Contained, Correct (Compilable), Example or SSCCE|http://homepage1.nifty.com/algafield/sscce.html] .
                    Thanks, I should have done so in the first place - turns out an exception message answers my question:
                    ZIP file must have at least one entry
                    See also: http://forums.sun.com/thread.jspa?messageID=1960487
                    • 7. Re: How to create an empty ZIP file?
                      807589
                      cotton.m wrote:
                      What is the purpose of this empty file?
                      My program is supposed to follow a standardized procedure in a standardized directory/file hierarchy where certain files are automatically archived. When in a particular instance of the data there are 0 such files, this should still lead to the creation of a valid ZIP file.

                      Well then, it seems the Java ZIP API can't do this and I need to devise a workaround for such cases. Like always creating a directory (see link above) or writing a suitable byte sequence with a regular output stream, or or or... :)

                      Thanks everybody!
                      • 8. Re: How to create an empty ZIP file?
                        807589
                        kevang wrote:
                        cotton.m wrote:
                        What is the purpose of this empty file?
                        My program is supposed to follow a standardized procedure in a standardized directory/file hierarchy where certain files are automatically archived. When in a particular instance of the data there are 0 such files, this should still lead to the creation of a valid ZIP file.

                        Well then, it seems the Java ZIP API can't do this and I need to devise a workaround for such cases. Like always creating a directory (see link above)
                        That was going to be one of my suggestions if suitable.

                        Why is not suitable to simply not create the zip if there are no files? If you have some logging in your application then you can log that there were no files to zip. (assuming that the reason is that you want to know that it's working)