如何从 ZipInputStream 获取每个 ZipFile 条目的字节数组
How to get byte array of each ZipFile entry from ZipInputStream
我有一个字节数组,其内容是从 zip 文件中填充的 file.I 想要将该 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);
}
我有一个字节数组,其内容是从 zip 文件中填充的 file.I 想要将该 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);
}