1

I am creating a zip file and downloading it. I am able to download all file files with out any error. But the files in the zip are placing in nested folders according to the files real path like C>Users>workspace>.metadata>.plugins>org.eclipse.wst.server.core>tmp0>wtpwebapps>finaldebrief while I am trying to zip 'finaldebrief' folder only. I want to get 'finaldebrief' folder directly in zip file. can someone help me out. Thanks in Advance.

Here is my code

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    int workshopid = Integer.parseInt(request.getParameter("currentworkshopid"));

    javax.servlet.ServletContext context = getServletConfig().getServletContext();
    String path = context.getRealPath("finaldebrief");

    try 
    { 
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment;filename=finaldebrief.zip");

        ZipOutputStream zos = new 
               ZipOutputStream(response.getOutputStream()); 

        zipDir(path + "/", zos); 

        zos.close(); 
    } 
    catch(Exception e) 
    { 
        e.printStackTrace();
    } 

}

public void zipDir(String dir2zip, ZipOutputStream zos) 
{
    try 
    { 
        //create a new File object based on the directory we have to zip File    
        File zipDir = new File(dir2zip); 
        //get a listing of the directory content 
        String[] dirList = zipDir.list(); 
        byte[] readBuffer = new byte[2156]; 
        int bytesIn = 0; 
        //loop through dirList, and zip the files 
        for(int i=0; i<dirList.length; i++) 
        { 
            File f = new File(zipDir, dirList[i]); 
            if(f.isDirectory()) 
            { 
                //if the File object is a directory, call this 
                //function again to add its content recursively 
                String filePath = f.getPath();
            //  String filePath = f.getCanonicalPath();

                zipDir(filePath, zos);
                //loop again 
                continue; 
            } 
            //if we reached here, the File object f was not  a directory 
            //create a FileInputStream on top of f 
            FileInputStream fis = new FileInputStream(f); 
            // create a new zip entry 
            ZipEntry anEntry = new ZipEntry(f.getPath()); 
            //place the zip entry in the ZipOutputStream object 
            zos.putNextEntry(anEntry); 
            //now write the content of the file to the ZipOutputStream 
            while((bytesIn = fis.read(readBuffer)) != -1) 
            { 
                zos.write(readBuffer, 0, bytesIn); 
            } 
            //close the Stream 
            fis.close(); 
        } 
    } 
    catch(Exception e) 
    { 
        e.printStackTrace(); 
    } 
}
Sagar
  • 484
  • 3
  • 10
  • 24
  • I think the problem has to do with the use of `f.getPath()`. This is likely to return the full path to the file. You need to strip off the prefix of the path so it is within the context of the base directory – MadProgrammer Apr 03 '13 at 06:29

1 Answers1

3

Within the line

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

you specify the path inside your zip file (i.e. f.getPath()). If you want a shorter path or none at all just manipulate this part (e.g. f.getName()).

Howard
  • 38,639
  • 9
  • 64
  • 83
  • @Sagar Is it ok to use something like `f.getParent().getName()+File.separator+f.getName()`? Or do you need arbitrary relative paths? You may then have a look into http://stackoverflow.com/q/204784/577423. – Howard Apr 03 '13 at 06:45
  • I removed my comment, Actually I have 3 subfolders and in every subfolder I have one file in common. And I have the same file in finaldebrief folder also. So I am getting "duplicate entry" error. I think that is why subfolders are not getting zip.. – Sagar Apr 03 '13 at 06:55