![]() |
|
|
|||||||
![]() |
Java - Add an empty dir to a Zip file programmatically |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
"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 |
|
|
|
#3 |
|
Posts: n/a
|
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 > > |
|
|
|
#4 |
|
Posts: n/a
|
"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 |
|
|
|
#5 |
|
Posts: n/a
|
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 |
|
|
|
#6 |
|
Posts: n/a
|
"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 > > |
|
|
|
#7 |
|
Posts: n/a
|
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 |
|
|
|
#8 |
|
Posts: n/a
|
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 |
|
|
|
#9 |
|
Posts: n/a
|
"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 > > > > > > |
|
|
|
#10 |
|
Posts: n/a
|
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! |
|