?
Solved

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

Posted on 2003-03-03
6
Medium Priority
?
3,346 Views
Last Modified: 2012-06-27
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
Comment
Question by:csar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8058812
Can you post the code you're using?
0
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 400 total points
ID: 8058972
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
 
LVL 26

Expert Comment

by:ksivananth
ID: 8062534
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 2

Author Comment

by:csar
ID: 8063136
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
 

Expert Comment

by:CleanupPing
ID: 9058955
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
 

Expert Comment

by:SkywireMoncton01
ID: 23685471
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question