java - Google Appengine JAVA - Zip 大量保存在 Blobstore 中的图片

标签 java google-app-engine zip blobstore

我开发了一个简单的媒体库,您可以在其中选择一组图像并下载它们。 当客户端请求下载时,servlet 接收用于创建 zip 文件的 blob key ,然后为该过程启动任务

任务遍历接收到的 blob key 并将图像压缩到存档中。任务完成后,会向用户发送一封带有下载链接的邮件。

这是我的问题:

FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
OutputStream blobOutputStream = Channels.newOutputStream(writeChannel);
ZipOutputStream zip = new ZipOutputStream(blobOutputStream);   

单个 channel 只能处理这个字节数

BlobstoreService.MAX_BLOB_FETCH_SIZE

正因为如此,我必须每写入 1mb 数据就打开和关闭 channel (读取同样的问题,但对于读取我使用了 this code 并且它有效)。或者 write() 方法抛出空异常

使用正常的 outputStream 打开和关闭 channel ,不会出现问题,如 this code

但是处理 Zip 文件我也必须管理

ZipOutputStream zip = new ZipOutputStream(blobOutputStream);   
ZipEntry zipEntry = new ZipEntry(image_file_name);
zipOut.putNextEntry(zipEntry);
// while the image has bytes to write
   zipOut.write(bytesToWrite);

在 ZipEntry 中写入 1MB 数据后,我必须关闭 channel 并再次打开它。

所以这里的问题是:在我打开一个新 channel 的地方,我无法访问我正在写入的前一个 zipEntry,然后我无法继续写入我正在处理的图像的下一个 1MB。

并且,在打开一个新 channel 后,如果我尝试在 zipEntry 对象上写入(没有重新初始化它),我会得到一个 ClosedChannel 异常

Here是我写的示例代码,我知道它不起作用,但解释了我正在尝试做的事情。

那么我的问题是:如何(如果可能的话,当然)我可以创建一个每次写入 1MB 的 zip 文件吗?

我也可以使用其他方法,我需要的是将一些图像压缩成一个 zip 并将其保存到 blobstore,如果您有其他想法,请告诉我

最佳答案

您应该创建自己的可以操纵 channel 的流。当达到 blob 大小限制时,您的流将关闭当前 channel 并打开一个新 channel 。

本地文件示例:

public class ZipChannels {
public static void main(String[] args) throws IOException {
    File dirToZip = new File("target\\dependency");

    //create zip-files
    ChannelOutput out = new ChannelOutput();
    ZipOutputStream zip = new ZipOutputStream(out);
    int b = 0;
    for(File file: dirToZip.listFiles()) {
        ZipEntry zipEntry = new ZipEntry(file.getName());
        zip.putNextEntry(zipEntry);
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        while((b = bis.read()) != -1) {
            zip.write(b);
        }
        bis.close();
        zip.closeEntry();
    }
    zip.close();

    //merge all into one file for check it
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("package_all.zip"));
    for (int i = 0; i < out.getChannelCount(); i++) {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("package_" + i + ".zip"));
        while((b = bis.read()) != -1) {
            bos.write(b);
        }
        bis.close();
    }
    bos.close();
}

public static class ChannelOutput extends OutputStream {
    private OutputStream channel;
    private int count = 0;
    final private int MAX = 1000000;

    @Override
    public void write(int b) throws IOException {
        if(count++ % MAX == 0) {
            openNewChannel();
        }
        channel.write(b);
    }

    protected void openNewChannel() throws IOException {
        if(channel != null) {
            channel.close();
        }
        channel = new BufferedOutputStream(new FileOutputStream("package_" + (count / MAX) + ".zip")); 
    }
    public int getChannelCount() {
        return count / MAX + 1;
    }

    @Override
    public void close() throws IOException {
        channel.close();
    }
    @Override
    public void flush() throws IOException {
        channel.flush();
    }
}
}

如果您有任何问题,请随时提出。

关于java - Google Appengine JAVA - Zip 大量保存在 Blobstore 中的图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16735716/

相关文章:

Python子进程模块将路径作为字符串发送

java - 使用 Gson 将 JSON 字符串解析为 Dictionary<String, Integer>

java - 如何在屏幕上查找 JTree 节点的位置

java - 语法错误: Constructor call must be the first statement in a constructor

android - Google Cloud Datastore 与 google drive 与其他存储服务的数据备份

google-app-engine - gcloud 应用程序部署给出 400/禁止错误/无法将 img 推送到谷歌容器注册表

java - 创建新目录

java - 检查 Google App Engine 服务器参数

Javascript 解压缩/解密 Zip 文件 Apache Cordova

postman - API端点通过Postman通过POST方法保存多部分文件