无法解压缩使用Java中的zlib创建的zip文件

问题描述 投票:1回答:1

我正在尝试使用Java(C0)中的zlib来压缩文件,但是在创建文件后无法解压缩文件。我正在使用下面的代码:

java.util.zip

[当我读取 public static ByteArrayInputStream compress(InputStream inputStream, String targetFilePath) { String fileName = new File(targetFilePath).getName(); try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream); zipOutputStream.putNextEntry(new ZipEntry(fileName)); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); byte[] buffer = new byte[1000]; int len; while ((len = bufferedInputStream.read(buffer)) > 0) { zipOutputStream.write(buffer, 0, len); } bufferedInputStream.close(); zipOutputStream.closeEntry(); return new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); } catch (IOException ex) { log.error("The file does not exist"); } return null; } 文件并将其作为InputStream输入到此方法时,它将创建sample.txt文件。

但是,当我尝试使用sample.zip命令打开此zip文件时,无法打开并显示以下错误:

unzip

我尝试使用 End-of-central-directory signature not found. Either this file is not a zipfile, or it constitutes one disk of a multi-part archive. In the latter case the central directory and zipfile comment will be found on the last disk(s) of this archive. unzip: cannot find zipfile directory in one of sample.zip or sample.zip.zip, and cannot find sample.zip.ZIP, period. 打开zip,该文件有效并显示包含jar xvf sample.zip文本文件。之所以起作用,是因为sample.txt命令不在zip文件中查找jar

有人可以解释为什么zip文件中没有此签名的原因吗?非常感谢在这方面的任何帮助。

java zip zlib unzip
1个回答
0
投票

写完最后一个条目后,请确保关闭End-of-central-directory signature。这为流提供了完成创建ZIP存档的机会。

© www.soinside.com 2019 - 2024. All rights reserved.