0

我正在尝试使用 ZipInputStream 将存档中的每个文件放入 ArrayList 中。我可以用 ZipInputStream 做到这一点吗?

我的主要目标是解压缩 cbr/cbz 文件(仅包含图像 (jpg/png) 的档案),我试图将这些图像中的每一个放在 ArrayList 上,因此 ZipInputStream 到 ArrayList 是我最终将它们放到位图的计划,但是如果您可以直接从 ZipInputStream 将它们获取到位图,那就太好了!

4

1 回答 1

2

到头来,按我的计划去做,太占内存了!相反,我最终一次只取一个 ZipEntry,但只有我想要的一个,这样就不必每次都循环遍历每个。

public Bitmap getBitmapFromZip(final String zipFilePath, final String imageFileInZip){
    Bitmap result = null;
try {
    ZipEntry ze = zipfile.getEntry(imageFileInZip);
    InputStream in = zipfile.getInputStream(ze);
    result = BitmapFactory.decodeStream(in);
} catch (IOException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}
return result;

}

只需在开始时快速循环以获取所有名称

public ArrayList<String> unzip() { 
    ArrayList<String> fnames = ArrayList<String>();
    try  { 
        FileInputStream fin = new FileInputStream(_zipFile); 
        ZipInputStream zin = new ZipInputStream(fin); 
        ZipEntry ze = null; 
        while ((ze = zin.getNextEntry()) != null) { 

            if(ze.isDirectory()) { 
            } else { 
            fnames.add(ze.getName()/*fname[fname.length - 1]*/);
            zin.closeEntry(); 
            }          
        } 
        zin.close(); 
    } catch(Exception e) { 
    } 
    return fnames;
} 
于 2012-03-23T00:02:39.950 回答