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

x
  • 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?

Regards,

Carsten
0
csar
Asked:
csar
1 Solution
 
CEHJCommented:
Can you post the code you're using?
0
 
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.

0
 
ksivananthCommented:
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 );
     }

Siva
0
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);
          zo.putNextEntry(entry);
          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);
              zo.write(buf,0,read);
              toread -= read;
            }
            bi.close();
            zo.flush();
          }
          zo.closeEntry();
           
        }
        fileCount++;
      }
      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?
0
 
CleanupPingCommented:
csar:
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 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
SkywireMoncton01Commented:
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){
              
        System.out.println(children[i].getAbsolutePath());
            if(children[i].isDirectory()){
                  //out.closeEntry();
                  out.putNextEntry(new ZipEntry(prefix+"\\"+children[i].getName()+"\\"));
                  run(prefix+"/"+children[i].getName(),children[i]);
                  out.closeEntry();
            }
            if(children[i].isFile()){
                  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);
                  }
                  in.close();
                  out.closeEntry();
            }
        }
    }
0

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