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

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

问题描述

我试图克隆一个模板(带有嵌套子文件夹的文件夹),替换几个文件,将其压缩并提供给用户。这可以在App Engine中完成,因为没有本地存储空间吗?



*** UPDATE ****
建立一个目录结构在内存中,然后将其压缩。
幸运的是,我在stackoverflow上发现了这篇文章:
java.util。 zip - 重新创建目录结构



其余部分是微不足道的。



谢谢All,

解决方案

是的,这可以做到。

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

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



下面是一个例子,说明如何使用 HttpServlet

  @Override 
public void doGet(HttpServletRequest req,HttpServletResponse resp)抛出
ServletException,IOException {
//处理参数并执行其他操作你需要

resp.setContentType(application / zip);
//表示正在发送一个文件:
resp.setHeader(Content-Disposition,attachment; filename = template.zip);

尝试(ZipOutputStream out = new ZipOutputStream(resp.getOutputStream())){
//这里通过您的模板/文件夹/文件,可选
//过滤它们或将它们替换为您想包含的内容

//将文件添加到输出zip文件中的示例:
ZipEntry e = new ZipEntry(some / folder / image.png);
//配置zip条目,文件的属性
e.setSize(1234);
e.setTime(System.currentTimeMillis());
//等等
out.putNextEntry(e);
//文件的内容:
out.write(new byte [1234]);
out.closeEntry();

//将另一个文件添加到输出zip,
//使用条目
//再次调用putNextEntry();使用write()方法写入其内容; closeEntry()。

out.finish();
} catch(Exception e){
//处理异常
}
}

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

  resp.setHeader(Cache-Control,no-cache); //对于HTTP 1.1 
resp.setHeader(Pragma,no-cache); //对于HTTP 1.0
resp.setDateHeader(Expires,0); //代理


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?

*** 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.

thanks All,

解决方案

Yes, this can be done.

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.)

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.

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
    }
}

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中创建ZIP档案(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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