在 Google App Engine (Java) 中创建 ZIP 档案 [英] Creating ZIP archives in Google App Engine ( Java)

查看:20
本文介绍了在 Google App Engine (Java) 中创建 ZIP 档案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试克隆一个模板(带有嵌套子文件夹的文件夹),替换一些文件,将其压缩并提供给用户.由于没有本地存储,这可以在 App Engine 中完成吗?

I am trying to clone a template ( folder with nested sub-folders) , replace a few files , ZIP it up and serve it to the user. Can this be done in App Engine since there is no local storage?

*** 更新****困难在于在内存中构建一个目录结构,然后将其压缩.幸运的是,我在 stackoverflow 上找到了这篇文章:java.util.zip - 重新创建目录结构

*** UPDATE**** The difficulty there was to build out a directory structure in memory and then zipping it up. Fortunately I found this post on stackoverflow : java.util.zip - Recreating directory structure

其余的都是微不足道的.

The rest is trivial.

谢谢大家,

推荐答案

是的,这是可以做到的.

Yes, this can be done.

据我所知,您想提供一个 zip 文件.在将 zip 文件作为 servlet 的响应发送到客户端之前,您不需要保存它.您可以在生成/即时生成 zip 文件时将其直接发送到客户端.(请注意,AppEngine 将缓存响应并在您的响应准备就绪时将其作为整体发送,但这在这里无关紧要.)

As I understand you, you want to serve a zip file. You don't need to save a zip file prior it is sent to the client as the response of a servlet. You can send the zip file directly to the client as it is generated / on-the-fly. (Note that AppEngine will cache the response and send it as the whole when your response is ready, but that's irrelevant here.)

将内容类型设置为 application/zip,您可以通过实例化一个 ZipOutputStream 将 servlet 的输出流作为构造函数参数传递来创建和发送响应 zip 文件.

Set the content type to application/zip, and you can create and send the response zip file by instantiating a ZipOutputStream passing the servlet's output stream as a constructor parameter.

以下是如何使用 HttpServlet 执行此操作的示例:

Here's an example how to do it with an HttpServlet:

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws
        ServletException, IOException {
    // Process parameters and do other stuff you need

    resp.setContentType("application/zip");
    // Indicate that a file is being sent back:
    resp.setHeader("Content-Disposition", "attachment;filename=template.zip");

    try (ZipOutputStream out = new ZipOutputStream(resp.getOutputStream())) {
        // Here go through your template / folders / files, optionally 
        // filter them or replace them with the content you want to include

        // Example adding a file to the output zip file:
        ZipEntry e = new ZipEntry("some/folder/image.png");
        // Configure the zip entry, the properties of the file
        e.setSize(1234);
        e.setTime(System.currentTimeMillis());
        // etc.
        out.putNextEntry(e);
        // And the content of the file:
        out.write(new byte[1234]);
        out.closeEntry();

        // To add another file to the output zip,
        // call putNextEntry() again with the entry,
        // write its content with the write() method and close with closeEntry().

        out.finish();
    } catch (Exception e) {
        // Handle the exception
    }
}

注意:您可能想要禁用响应 zip 文件的缓存,具体取决于您的情况.如果要禁用代理和浏览器缓存结果,请在启动 ZipOutputStream 之前添加这些行:

Note: You might wanna disable caching of your response zip file, depending on your case. If you want to disable caching the result by proxies and browsers, add these lines before you start the ZipOutputStream:

resp.setHeader("Cache-Control", "no-cache"); // For HTTP 1.1
resp.setHeader("Pragma", "no-cache"); // For HTTP 1.0
resp.setDateHeader("Expires", 0); // For proxies

这篇关于在 Google App Engine (Java) 中创建 ZIP 档案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆