【问题标题】:Google Appengine JAVA - Zip lots of images saving in BlobstoreGoogle Appengine JAVA - 压缩保存在 Blobstore 中的大量图像
【发布时间】:2013-05-24 13:07:27
【问题描述】:

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

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

这是我的问题:

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

单个通道只能处理这么多字节

BlobstoreService.MAX_BLOB_FETCH_SIZE

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

使用正常的 outputStream 打开和关闭通道,不会出现问题,例如 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 数据后,我必须关闭通道并再次打开它。

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

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

Here 是我编写的示例代码,我知道它不起作用,但解释了我想要做什么。

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

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

【问题讨论】:

  • 我已经阅读的页面:jsfiddle.net/KVprB
  • 您应该以某种方式使用缓冲读数,就像stackoverflow.com/questions/4308276/… 建议的那样。该示例属于另一种类型的流,但主要原则可能适用。我认为写入之间的关闭是应该避免的,并且混合使用缓冲读取器会使这块石头变得更糟。
  • 我对 Blob 的读取完全没有问题。我的问题是当我编写 zip 文件时。因为我每个频道只能写 1MB,所以当我关闭并打开一个新频道时,我需要一种方法来保持“ZipEntry”

标签: java google-app-engine zip blobstore


【解决方案1】:

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

本地文件示例:

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();
    }
}
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 2020-04-27
    相关资源
    最近更新 更多