Fle*_*eur 3 java zip zlib unzip
我试图zlib在 Java ( 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;
}
Run Code Online (Sandbox Code Playgroud)
当我读取sample.txt文件并将其作为 InputStream 输入到此方法时,它会创建一个sample.zip文件。
但是,当我尝试使用unzip命令打开此 zip 文件时,它无法打开并显示以下错误:
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.
Run Code Online (Sandbox Code Playgroud)
我尝试使用jar xvf sample.zipwhich 工作并显示包含sample.txt文本文件来打开 zip 。这是有效的,因为jar命令不会End-of-central-directory signature在 zip 文件中查找 。
有人可以解释为什么这个签名不在 zip 文件中吗?非常感谢这方面的任何帮助。