小编Jav*_*-DK的帖子

在服务器上创建Zip文件并使用java下载该zip文件

我有以下代码从mkyong到本地的zip文件.但是,我的要求是在服务器上压缩文件并需要下载.任何人都可以帮忙.

代码写入zipFiles:

public void zipFiles(File contentFile, File navFile)
{
    byte[] buffer = new byte[1024];

    try{
        // i dont have idea on what to give here in fileoutputstream
        FileOutputStream fos = new FileOutputStream("C:\\MyFile.zip");
        ZipOutputStream zos = new ZipOutputStream(fos);
        ZipEntry ze= new ZipEntry(contentFile.toString());
        zos.putNextEntry(ze);
        FileInputStream in = new FileInputStream(contentFile.toString());

        int len;
        while ((len = in.read(buffer)) > 0) {
            zos.write(buffer, 0, len);
        }

        in.close();
        zos.closeEntry();

        //remember close it
        zos.close();

        System.out.println("Done");

    }catch(IOException ex){
       ex.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以在fileoutputstream中提供什么?contentfile和navigationfile是我从代码创建的文件.

java zip java-io

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×1

java-io ×1

zip ×1