4

我开发了一个简单的媒体库,您可以在其中选择一组图像并下载它们。当客户端请求下载时,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 的数据就打开和关闭通道(读取的问题相同,但对于读取,我使用了此代码并且它有效)。或者 write() 方法抛出空异常

使用正常的 outputStream 打开和关闭通道,不会出现问题,就像这段代码

但是处理一个 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 异常

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

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

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

4

1 回答 1

2

您应该创建自己的可以操作频道的流。当达到 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();
    }
}
}

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

于 2013-06-04T18:20:57.343 回答