gpt4 book ai didi

java - 使用 gzip 格式的缓冲区解压缩图像失败

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

我正在使用 压缩文件,但在解压缩它们时,我遇到了两个问题,

  1. 在没有缓冲区的情况下解压缩时,它会恢复到原始形式,但是当我使用缓冲区时,它无法正确完成

  2. 解压文件的大小大于原始文件

private static void writeFile(FileOutputStream fos, String zipFilePath) throws IOException {
try (FileInputStream fis = new FileInputStream(zipFilePath);
GZIPInputStream inflaterInputStream = new GZIPInputStream(fis)) {
int data;
**while ((data = inflaterInputStream.read()) != -1) {//without buffer**
fos.write(data);
}
}
}

private static void writeFile(FileOutputStream fos, String zipFilePath) throws IOException {
byte[] buffer = new byte[12048];
try (FileInputStream fis = new FileInputStream(zipFilePath);
GZIPInputStream inflaterInputStream = new GZIPInputStream(fis)) {
int data;
**while ((data = inflaterInputStream.read(buffer)) != -1) {//with buffer**
fos.write(data);
}
}
}

最佳答案

你写的不是buffer,而是data,它是读取字节的长度...

更正:

private static void writeFile(FileOutputStream fos, String zipFilePath) throws IOException {
byte[] buffer = new byte[12048];
try (InputStream fis = new FileInputStream(zipFilePath);
InputStream inflaterInputStream = new GZIPInputStream(fis)) {
int data;
while ((data = inflaterInputStream.read(buffer)) != -1) {//with buffer**
fos.write(buffer, 0, data);
}
}
}

你最好使用 apache.commons-io

private static void writeFile(FileOutputStream fos, String zipFilePath) throws IOException {
try (InputStream fis = new FileInputStream(zipFilePath);
InputStream inflaterInputStream = new GZIPInputStream(fis)) {
IOUtils.copy(fis, fos);
}
}

关于java - 使用 gzip 格式的缓冲区解压缩图像失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56053299/

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