Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Add an empty dir to a Zip file programmatically

Reply
Thread Tools

Add an empty dir to a Zip file programmatically

 
 
Covington Bradshaw
Guest
Posts: n/a
 
      04-02-2004
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

 
Reply With Quote
 
 
 
 
Tony Morris
Guest
Posts: n/a
 
      04-02-2004

"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
 
nos
Guest
Posts: n/a
 
      04-02-2004
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
 
Tony Morris
Guest
Posts: n/a
 
      04-02-2004

"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
 
Andrew Thompson
Guest
Posts: n/a
 
      04-02-2004
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
 
nos
Guest
Posts: n/a
 
      04-02-2004

"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
 
Andrew Thompson
Guest
Posts: n/a
 
      04-02-2004
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
 
Dario
Guest
Posts: n/a
 
      04-02-2004
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
 
nos
Guest
Posts: n/a
 
      04-02-2004

"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
 
Covington Bradshaw
Guest
Posts: n/a
 
      04-02-2004
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

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Programmatically creating rdlc file for .net 3.5 Bala ASP .Net Web Controls 0 09-09-2008 11:32 AM
Delete a Non-empty directory Programmatically Raman C Programming 6 10-29-2007 04:15 PM
Replacing a running file programmatically Phlip C++ 15 09-12-2007 11:46 AM
file download with WriteFile method gives an empty file ? =?Utf-8?B?RXJpYyBC?= ASP .Net 1 02-06-2006 10:33 AM
File Move Programmatically Karl Heinz Buchegger C++ 11 06-03-2004 12:00 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57