Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - Add an empty dir to a Zip file programmatically

 
Thread Tools Search this Thread
Old 04-02-2004, 02:42 AM   #1
Default Add an empty dir to a Zip file programmatically


Hi,
How do I add an empty dir to a Zip file programmatically

import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
....
try {
ZipEntry dirEntry = new ZipEntry(dirFile.getPath());
zipOutputStream.putNextEntry(dirEntry);
}
catch (IOException e) {}

The dir is added as an empty file

Please help



Covington Bradshaw
  Reply With Quote
Old 04-02-2004, 03:11 AM   #2
Tony Morris
 
Posts: n/a
Default Re: Add an empty dir to a Zip file programmatically


"Covington Bradshaw" <> wrote in message
news...
> Hi,
> How do I add an empty dir to a Zip file programmatically
>
> import java.util.zip.ZipEntry;
> import java.util.zip.ZipOutputStream;
> ...
> try {
> ZipEntry dirEntry = new ZipEntry(dirFile.getPath());
> zipOutputStream.putNextEntry(dirEntry);
> }
> catch (IOException e) {}
>
> The dir is added as an empty file
>
> Please help
>


I have a reasonable suspicion that a zip archive cannot contain an empty
directory entry.
You might be able to back this up with a more formal specification of the
zip archive format.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform


  Reply With Quote
Old 04-02-2004, 05:37 AM   #3
nos
 
Posts: n/a
Default Re: Add an empty dir to a Zip file programmatically

or you can just try it, i did and it was
easy to create a zip file that contains only an empty directory
with zip


"Tony Morris" <> wrote in message
news:c4ii4q$9g6$...
>
> "Covington Bradshaw" <> wrote in message
> news...
> > Hi,
> > How do I add an empty dir to a Zip file programmatically
> >
> > import java.util.zip.ZipEntry;
> > import java.util.zip.ZipOutputStream;
> > ...
> > try {
> > ZipEntry dirEntry = new ZipEntry(dirFile.getPath());
> > zipOutputStream.putNextEntry(dirEntry);
> > }
> > catch (IOException e) {}
> >
> > The dir is added as an empty file
> >
> > Please help
> >

>
> I have a reasonable suspicion that a zip archive cannot contain an empty
> directory entry.
> You might be able to back this up with a more formal specification of the
> zip archive format.
>
> --
> Tony Morris
> (BInfTech, Cert 3 I.T.)
> Software Engineer
> (2003 VTR1000F)
> Sun Certified Programmer for the Java 2 Platform (1.4)
> Sun Certified Developer for the Java 2 Platform
>
>



  Reply With Quote
Old 04-02-2004, 05:48 AM   #4
Tony Morris
 
Posts: n/a
Default Re: Add an empty dir to a Zip file programmatically


"nos" <> wrote in message
news:M56bc.58157$K91.141189@attbi_s02...
> or you can just try it, i did and it was
> easy to create a zip file that contains only an empty directory
> with zip


I did try it with both Winzip and jar, hence the suspicion (and not the
conclusion).
What did you use to create teh archive ?


--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform


  Reply With Quote
Old 04-02-2004, 06:26 AM   #5
Andrew Thompson
 
Posts: n/a
Default Re: Add an empty dir to a Zip file programmatically

On Thu, 01 Apr 2004 20:42:21 -0500, Covington Bradshaw wrote:

> How do I add an empty dir to a Zip file programmatically


I do not know if that is possible.
Why do you need it, there may be
better ways to achieve what you want.

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
  Reply With Quote
Old 04-02-2004, 06:33 AM   #6
nos
 
Posts: n/a
Default Re: Add an empty dir to a Zip file programmatically


"Tony Morris" <> wrote in message
news:c4ira3$jge$...
>
> "nos" <> wrote in message
> news:M56bc.58157$K91.141189@attbi_s02...
> > or you can just try it, i did and it was
> > easy to create a zip file that contains only an empty directory
> > with zip

>
> I did try it with both Winzip and jar, hence the suspicion (and not the
> conclusion).
> What did you use to create teh archive ?
>


Here is my screen
-----------------------------------
C:\>mkdir sss

C:\>zip jones.zip sss
adding: sss/ (stored 0%)

C:\>unzip -t jones
Archive: jones.zip
testing: sss/ OK
No errors detected in compressed data of jones.zip.

-----------------------------------
> --
> Tony Morris
> (BInfTech, Cert 3 I.T.)
> Software Engineer
> (2003 VTR1000F)
> Sun Certified Programmer for the Java 2 Platform (1.4)
> Sun Certified Developer for the Java 2 Platform
>
>



  Reply With Quote
Old 04-02-2004, 07:21 AM   #7
Andrew Thompson
 
Posts: n/a
Default Re: Add an empty dir to a Zip file programmatically

On Fri, 2 Apr 2004 12:11:36 +1000, Tony Morris wrote:

> "Covington Bradshaw" <> wrote in message

....
>> How do I add an empty dir to a Zip file programmatically

...
> I have a reasonable suspicion that a zip archive cannot contain an empty
> directory entry.


My response was based on a (vague) memory
of trying to do just that, and failing.

(AFAIR, I was iterating through a directory
structure copying every file/directory to
a zip - only the 'non-empty' directories
made it)

> You might be able to back this up with a more formal specification of the
> zip archive format.


That is the best idea so far.

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
  Reply With Quote
Old 04-02-2004, 08:37 AM   #8
Dario
 
Posts: n/a
Default Re: Add an empty dir to a Zip file programmatically

Tony Morris wrote:

> "nos" <> wrote:
>
>> or you can just try it, i did and it was
>> easy to create a zip file that contains only an empty directory
>> with zip

>
> I did try it with both Winzip and jar, hence the suspicion (and not the
> conclusion).
> What did you use to create teh archive ?


The following is a uuencoded ZIP file containg
-1- an empty file
-2- an empty folder

You can uudecode it to retrieve EmptyTest.zip
and then you can use WinZip extract-all command
to retrieve
-1- EmptyTextDocument.txt an empty file
-2- EmptyTestFoleer an empty folder

- Dario



  Reply With Quote
Old 04-02-2004, 09:10 AM   #9
nos
 
Posts: n/a
Default Re: Add an empty dir to a Zip file programmatically


"nos" <> wrote in message
news:mW6bc.59224$JO3.37141@attbi_s04...
>
> "Tony Morris" <> wrote in message
> news:c4ira3$jge$...
> >
> > "nos" <> wrote in message
> > news:M56bc.58157$K91.141189@attbi_s02...
> > > or you can just try it, i did and it was
> > > easy to create a zip file that contains only an empty directory
> > > with zip

> >
> > I did try it with both Winzip and jar, hence the suspicion (and not the
> > conclusion).
> > What did you use to create teh archive ?
> >

>
> Here is my screen
> -----------------------------------
> C:\>mkdir sss
>
> C:\>zip jones.zip sss
> adding: sss/ (stored 0%)
>
> C:\>unzip -t jones
> Archive: jones.zip
> testing: sss/ OK
> No errors detected in compressed data of jones.zip.
>
> -----------------------------------


I just also tried to do the same thing with "jar"
and was not able to do it, so maybe that is a
feature of java to help you to not waste space.
You should be able to add an empty directory
to a jar file with zip however.

> > --
> > Tony Morris
> > (BInfTech, Cert 3 I.T.)
> > Software Engineer
> > (2003 VTR1000F)
> > Sun Certified Programmer for the Java 2 Platform (1.4)
> > Sun Certified Developer for the Java 2 Platform
> >
> >

>
>



  Reply With Quote
Old 04-02-2004, 02:13 PM   #10
Covington Bradshaw
 
Posts: n/a
Default Re: OK I found out! --- Add an empty dir to a Zip file programmatically

Ok I found out!
The trick is to postpend /. to the directory name, or programmatically as
follows
System.getProperty("file.separator")+"."


import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
...
try { ZipEntry dirEntry = new
ZipEntry(dirFile.getPath()+System.getProperty("fil e.separator")+".");
zipOutputStream.putNextEntry(dirEntry);
}
catch (IOException e) {}

This adds an empty file with name "." of zero size, which stands for the
directory iself!



  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump