Add an empty dir to a Zip file programmatically

  • Thread starter Covington Bradshaw
  • Start date
C

Covington Bradshaw

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
 
T

Tony Morris

Covington Bradshaw said:
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
 
N

nos

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
 
T

Tony Morris

nos said:
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
 
A

Andrew Thompson

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.
 
N

nos

Tony Morris said:
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.

-----------------------------------
 
A

Andrew Thompson

...
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.
 
D

Dario

Tony said:
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
 
N

nos

nos said:
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.
 
C

Covington Bradshaw

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("file.separator")+".");
zipOutputStream.putNextEntry(dirEntry);
}
catch (IOException e) {}

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

nos

Covington Bradshaw said:
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("file.separator")+".");
zipOutputStream.putNextEntry(dirEntry);
}
catch (IOException e) {}

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

But you say it is a file !
 
Joined
May 30, 2007
Messages
1
Reaction score
0
Nice solve but with one mistake. If u want to make "ONLY" empty directory just write

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


I dont understand why u make empty file "."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top