【问题标题】:Unzip in memory for google App Engine Java在内存中解压 google App Engine Java
【发布时间】:2012-11-05 17:12:14
【问题描述】:

我想解压缩上传到 servlet 的文件,并将所有解压缩文件以 byte[] 的形式存储到 DataStore。由于 GAE 中没有文件系统,所以我必须将所有内容都放在内存中。假设我有 byte[] allzipdata 来存储原始 zip 文件数据。如何解压缩文件,尤其是如何从内存中的每个 zipentry 获取输入流?

ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(allzipdata));
ZipEntry ze = zis.getNextEntry();
while(ze!=null){
}

那么 while 循环中有什么?

另外,如果我上传文件,我可以使用 item.getContentType(); 知道 contentType;其中 item 是 FileItemStream。那么对于一个 zipentry,有没有办法知道 contentType?

【问题讨论】:

    标签: java google-app-engine servlets memory unzip


    【解决方案1】:

    要从ZipInputStream 读取图像数据,我建议使用Apache Commons-IO 库。它将输入流的 ZIP 条目转换为字节数组:

        ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(allzipdata));
        ZipEntry ze = null;
        while ((ze = zis.getNextEntry()) != null) {
           // write your code to use zip entry e.g. below:
           String filename = ze.getName();
           System.out.println("File Name of Entry file="+fileName);
           byte[] data = IOUtils.toByteArray(zis);
           // now work with the image `data`
        }
    

    【讨论】:

    • 有一个错字错误文件名 var 声明没有骆驼符号。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多