gpt4 book ai didi

android - 如何从 ZipInputStream 获取每个 ZipFile 条目的字节数组

转载 作者:行者123 更新时间:2023-11-30 01:15:41 25 4
gpt4 key购买 nike

我有一个字节数组,其内容是从一个 zip 文件填充的。我想将该 zip 存档中的每个文件作为内存中的一个单独的字节数组来随机访问它们。我不想在 sdcard 中写入任何内容并将数组保存在内存中。

我搜索并找到了 this .这是我的代码:

protected Map<String,ZipFile> ZipDic;

class ZipFile{
public byte[] contents;
public String name;
public long size;
}


public makeDictionary(byte[] zipFileArray) {
try {
InputStream is = new ByteArrayInputStream(zipFileArray);
ZipInputStream zipInputStream = new ZipInputStream(is);
ZipDic = new HashMap<>();
ZipEntry ze = null;

while ((ze = zipInputStream.getNextEntry()) != null) {
ZipFile zf=new ZipFile();
zf.name="/testepub/"+ze.getName();
zf.size=ze.getSize();
try {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n;
while ((n = is.read(buf, 0, 1024)) != -1) {
output.write(buf, 0, n);
}
zf.contents=output.toByteArray();
}catch (Exception ex){
ex.printStackTrace();
zf.contents=null;
}
ZipDic.put(zf.name,zf);
}

}catch (Exception ex){
ex.printStackTrace();
}
}

我的问题是当 while 循环执行第一个循环时,zf.contents 是一个字节数组,其大小略小于 zipFileArray 并在第二个循环中获取异常:

java.util.zip.ZipException: CRC mismatch.

如何将 zipFileArray 中的每个文件作为未压缩的字节数组分别获取?

最佳答案

改变

while ((n = is.read(buf, 0, 1024)) != -1) {
output.write(buf, 0, n);
}

进入

while ((n = zipInputStream.read(buf, 0, 1024)) != -1) {
output.write(buf, 0, n);
}

关于android - 如何从 ZipInputStream 获取每个 ZipFile 条目的字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37894669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com