【问题标题】:How to create a zip file and download it?如何创建一个 zip 文件并下载它?
【发布时间】:2013-04-03 06:24:39
【问题描述】:

我正在创建一个 zip 文件并下载它。我能够下载所有文件文件而不会出现任何错误。但是,当我试图仅压缩“finaldebrief”文件夹。我想直接在 zip 文件中获取“finaldebrief”文件夹。有人可以帮我吗。提前致谢。

这是我的代码

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

【问题讨论】:

  • 我认为问题与f.getPath() 的使用有关。这可能会返回文件的完整路径。您需要去掉路径的前缀,使其位于基本目录的上下文中

标签: java


【解决方案1】:

行内

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

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

【讨论】:

  • @Sagar 可以使用f.getParent().getName()+File.separator+f.getName() 之类的东西吗?还是您需要任意相对路径?然后您可以查看stackoverflow.com/q/204784/577423
  • 我删除了我的评论,实际上我有 3 个子文件夹,在每个子文件夹中我有一个共同的文件。我在 finaldebrief 文件夹中也有相同的文件。所以我收到“重复输入”错误。我认为这就是为什么子文件夹没有得到 zip..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-16
  • 1970-01-01
  • 2017-06-02
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
相关资源
最近更新 更多