gpt4 book ai didi

android - 解压 Byte zip 数据

转载 作者:行者123 更新时间:2023-11-29 18:13:39 24 4
gpt4 key购买 nike

我想解压缩一个 zip 文件。我将 zip 数据作为字节。我使用以下代码解压缩 zip 文件。但我可以从 zip 文件中的文件中读取名称。但是输出数据已损坏。我想我正在读取 ZipInputStream 中的全部数据。我如何读取每个文件数据?请帮助我谢谢..

ByteArrayInputStream binput = new ByteArrayInputStream(zipeddatainbyte);
ZipInputStream mzipstream = new ZipInputStream(binput);
File f2 = new File("/sdcard/Unziped Data/");
f2.mkdirs();
ZipEntry entry;
FileOutputStream out;
byte[] bout=null;
try {
while ((entry = mzipstream.getNextEntry()) != null) {
System.out.println("entry: " + entry.getName() + ", "
+ entry.getSize());
bout=new byte[(int) entry.getSize()];
mzipstream.read(bout);
out = new FileOutputStream("/sdcard/Unziped Data/"+entry.getName());
out.write(bout);
out.close();



}
} catch (IOException e) {

e.printStackTrace();
}

最佳答案

您不应该相信 entry.getSize(),而应该尝试读取每个 ZipEntry 的流的末尾并控制实际读取的内容。这不仅适用于 ZipInputStreams,也适用于任何 Java InputStream。此 fragment 摘自 here :

ZipEntry entrada;
while (null != (entrada=zis.getNextEntry()) ){
System.out.println(entrada.getName());

FileOutputStream fos = new FileOutputStream(entrada.getName());
int leido;
byte [] buffer = new byte[1024];
while (0<(leido=zis.read(buffer))){
fos.write(buffer,0,leido);
}
fos.close();
zis.closeEntry();
}

关于android - 解压 Byte zip 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9360185/

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