tags:

views:

37

answers:

1

I have a java program as below for zipping a folder as a whole.

public static void zipDir(String dir2zip, ZipOutputStream zos) 

{ 

try 

{ 

           File zipDir= new File(dir2zip); 

            String[] dirList = zipDir.list(); 

          byte[] readBuffer = new byte[2156]; 

          int bytesIn = 0; 

      for(int i=0; i<dirList.length; i++) 

       { 

         File f = new File(zipDir, dirList[i]); 

      if(f.isDirectory()) 

      { 

                    String filePath = f.getPath(); 

           zipDir(filePath, zos); 

         continue; 

     } 

          FileInputStream fis = new FileInputStream(f); 

            ZipEntry anEntry = new ZipEntry(f.getPath()); 

                zos.putNextEntry(anEntry); 

       while((bytesIn = fis.read(readBuffer)) != -1) 

       { 

           zos.write(readBuffer, 0, bytesIn); 

        } 

      fis.close(); 

    } 

} 

catch(Exception e) 

{ 

 e.printStackTrace();

} 


}

public static void main(){
String date=new java.text.SimpleDateFormat("MM-dd-yyyy").format(new java.util.Date());

         ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("Output/" + date + "_RB" + ".zip"));

         zipDir("Output/" + date + "_RB", zos); 

         zos.close();
}

My query here is. The target folder(+date+_RB) to be zipped is present inside the folder named Output. After successful zipping, when I extract the zipped file, I find a folder Output inside which the (+date+_RB) required folder is present. I need not want that Output folder after the extraction of the zipped file, rather it should directly extract the required folder alone. Please advise on the same.

UPDATE:

I tried Isaac's answer. While extracting the resultant zip file, no folders are getting extracted. Only the files inside all the folders are getting extracted. I just dont need the folder "Output" alone in the resultant zip file. But what the program does is, it doesnt extracts all other folders inside the Output folder, rather it just extracts the files inside those folders. Kindly advise on how to proceed...

+2  A: 

It happens because of this:

ZipEntry anEntry = new ZipEntry(f.getPath());

f.getPath() will return Output/ at the beginning of the string. This is due to the flow of your program and how it (mis)uses File objects.

I suggest you construct a File object called, say, tmp:

File tmp = new File(dirList[i]);

The change the construction of f:

File f = new File(zipDir, tmp.getPath());

Then, change this:

ZipEntry anEntry = new ZipEntry(f.getPath());

To this:

ZipEntry anEntry = new ZipEntry(tmp.getPath());

I didn't have time to actually test it, but in a nutshell, your problem is due to how the File object is constructed.

Isaac
@Isaac pls check out my updated question
LGAP
Now, that happens because you're not including directory entries into your ZIP. Add this to the `f.isDirectory()` condition: `zos.putNextEntry(new ZipEntry(filePath))`.
Isaac
Sorry, I could not get you Isaac! Can you explain me more briefly?
LGAP
"More briefly" means "less details". Creating a ZIP file involves adding not just the contents of files, but also "entries" for the files. That's what `putNextEntry` does; it doesn't actually add the file's contents - it adds the proper ZIP header. You did it for files, so you need to do it for directory entries too.
Isaac