Java 如何创建zip文件并下载它?

Java 如何创建zip文件并下载它?,java,Java,我正在创建一个zip文件并下载它。我能够下载所有的文件没有任何错误。但是zip中的文件是根据文件的实际路径放置在嵌套文件夹中的,比如C>Users>workspace>.metadata>.plugins>org.eclipse.wst.server.core>tmp0>wtpwebapps>finaldebrief,而我只尝试压缩“finaldebrief”文件夹。我想直接在zip文件中获取“finaldebrief”文件夹。有人能帮帮我吗。提前谢谢 这是我的密码 protected void

我正在创建一个zip文件并下载它。我能够下载所有的文件没有任何错误。但是zip中的文件是根据文件的实际路径放置在嵌套文件夹中的,比如C>Users>workspace>.metadata>.plugins>org.eclipse.wst.server.core>tmp0>wtpwebapps>finaldebrief,而我只尝试压缩“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(); 
    } 
}
protectedvoiddopost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
int workshopid=Integer.parseInt(request.getParameter(“currentworkshopid”);
javax.servlet.ServletContext=getServletConfig().getServletContext();
字符串路径=context.getRealPath(“finaldefirect”);
尝试
{ 
response.setContentType(“应用程序/zip”);
setHeader(“内容处置”、“附件;文件名=finaldefirect.zip”);
ZipoutStream zos=新
ZipOutputStream(response.getOutputStream());
zipDir(路径+“/”,zos);
zos.close();
} 
捕获(例外e)
{ 
e、 printStackTrace();
} 
}
public void zipDir(字符串dir2zip、ZipOutputStream zos)
{
尝试
{ 
//根据要压缩文件的目录创建一个新的文件对象
File zipDir=新文件(dir2zip);
//获取目录内容的列表
字符串[]dirList=zipDir.list();
字节[]读取缓冲区=新字节[2156];
int字节数=0;
//循环浏览目录列表,并压缩文件
对于(int i=0;i在直线内

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

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

我认为问题与
f.getPath()的使用有关
。这可能会返回文件的完整路径。您需要去掉路径的前缀,使其位于基本路径的上下文中directory@Sagar可以使用类似于
f.getParent().getName()+File.separator+f.getName()的内容吗
?或者您需要任意相对路径吗?然后您可以查看一下。我删除了我的注释,实际上我有3个子文件夹,每个子文件夹中都有一个共同的文件。finaldebrief文件夹中也有相同的文件。因此,我遇到了“重复输入”错误。我想这就是为什么子文件夹没有压缩的原因。。