java 如何创建一个 zip 文件并下载它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15780270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:50:22  来源:igfitidea点击:

How to create a zip file and download it?

java

提问by Sagar

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.

我正在创建一个 zip 文件并下载它。我能够下载所有文件而没有任何错误。但是 zip 中的文件根据文件真实路径放置在嵌套文件夹中,例如 C>Users>workspace>.metadata>.plugins>org.eclipse.wst.server.core>tmp0>wtpwebapps>finaldebrief 而我正在尝试仅 zip 'finaldebrief' 文件夹。我想直接在 zip 文件中获取“finaldebrief”文件夹。有人可以帮我吗。提前致谢。

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(); 
    } 
}

回答by Howard

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()).

您在 zip 文件中指定路径(即f.getPath())。如果您想要更短的路径或根本没有路径,只需操纵这部分(例如f.getName())。