写入文件并从字节数组压缩

时间:2019-01-21 17:26:29

标签: java zip

我正在尝试在zip文件中写入一些文件并下载。

写入第一个文件后,便开始下载zip文件...但其他文件不存在。

public static void addToZipFile(List<byte[]> contentFiles, List<String> strDocFileNames, HttpServletResponse response) throws FileNotFoundException, IOException {


    String strDocFileName = null;
    byte [] contentFile = null;
    response.setContentType("application/zip");
    response.setHeader("Content-Disposition", "attachment;filename=test.zip");

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

    try  {

    for (int i = 0; i < strDocFileNames.size(); i++) {
        strDocFileName = strDocFileNames.get(i);
        contentFile = contentFiles.get(i);


        ZipEntry e = new ZipEntry(strDocFileName);
        e.setSize(contentFile.length);
        e.setTime(System.currentTimeMillis());
        out.putNextEntry(e);

        out.write(contentFile);
        out.closeEntry();

    }

        out.flush();
        out.close();



    } catch (Exception e) {
        e.getMessage();
    }   
}

怎么了?

0 个答案:

没有答案