Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3617
  • Last Modified:

Add an empty directory to Zip (using java.util.zip)

I came across a jar-file that contains a ZipEntry for which isDirectory() yields true.

How can I write such an entry into a jar/zip-file. Creating a ZipEntry that ends with File.separator does not write anything into the file and creating an entry just by putNextEntry(..);closeEntry() produces only a simple file in the zip.

I like to create a zip that creates some empty folders upon extraction.

Can this be done?


1 Solution
Can you post the code you're using?
Tommy BraasCommented:
The short version:
You don't need to enter directories separately to the zip file. They will appear automatically when you add files at a path containing a directory not yet in the zip file.

The longer version:
A directory exists only by the fact that it contains files, as far as a Zip file is concerned. You shouldn't try to enter directories directly into the Zip file, unless you have a specific purpose for doing so,a nd in the case that they are empty. That purpose could be that you want to recreate directory structure where the zip file is decompressed. Even in that case you would have to special case that behavior and look for zero byte files in the zip file.

Ya, its possible, try the following,

fis  = new FileInputStream( strInputFilePath );
//strInputFilePath - input file location
zos.putNextEntry( new ZipEntry( strBaseDirName + strFileSeparator + strFileName ) );
//strBaseDirName - the directory name under which the file to be Zipped
//strFileSeparator - file separator
//strFileName - name of the file

     while(( byteVal = fis.read( buf ) ) != -1)
       zos.write( buf, 0, byteVal );

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

csarAuthor Commented:
OK, here is the code:
  private int recurseWrite(File root) throws IOException{
      int fileCount =0;
      File [] files = root.listFiles();
      for (int ii=0;ii<files.length;ii++) {
        boolean isFile = ! (files[ii].isDirectory());
        if (isFile ||  ( 0 == recurseWrite(files[ii]) )) {
          String name = files[ii].getCanonicalPath().substring(parent.getCanonicalPath().length());
//          if (!isFile) name += name+File.separator;
          ZipEntry entry = new ZipEntry(name);
          if (isFile) {
            BufferedInputStream bi = new BufferedInputStream(new FileInputStream(files[ii]));
            byte[] buf = new byte[512];
            long toread = files[ii].length();
            while (toread>0) {
              int read = bi.read(buf);
              toread -= read;
      return fileCount;

zo is a ZipOutputStream.

@orangehead: The latter you describe is what I what to do: Create a directory-structure that contains some empty folders. I know that I can fix folders by putting an empty file in it, but I'd like to avoid this. I have customers that ask me what a file named "delete.me" might contain...

@kvisanath: Opening an InputStream on a directory gives me an IOException (Access denied?!)...

The point is that creating a directory ZipEntry must be possible, since I have a jar (mts.jar of the Jdeveloper-distribution) that contains a directory mf/. This directory ist of course not empty, but why is the entry there if it is not needed at all?
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.
byte[] buf = new byte[1024];
      ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
    private void run(String prefix,File root) throws IOException{
        File[] children = root.listFiles();
        for(int i=0; i<children.length; ++i){
                  out.putNextEntry(new ZipEntry(prefix+"\\"+children[i].getName()+"\\"));
                  out.putNextEntry(new ZipEntry(prefix+"/"+children[i].getName()));
                  FileInputStream in = new FileInputStream(children[i]);
                  // Add ZIP entry to output stream.
                  // Transfer bytes from the file to the ZIP file
                  int len;
                  while ((len = in.read(buf)) > 0) {
                      out.write(buf, 0, len);

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now