下面是一些代码,它从只包含一个文件的zip文件中提取一个文件。但是,提取的文件与通过WinZip或其他zip实用程序提取的文件不匹配。如果文件包含奇数字节(因为我的缓冲区大小为2,读取失败时我只是中止),那么它可能会被一个字节关闭。但是,当分析(使用WinMerge或Diff)使用下面的代码提取的文件与通过Winzip提取的文件时,有几个区域缺少字节。有人知道为什么或者我怎么能解决这个问题吗?
package zipinputtest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipInputStream;
public class test2 {
public static void main(String[] args) {
try {
ZipInputStream zis = new ZipInputStream(new FileInputStream("C:\\temp\\sample3.zip"));
File outputfile = new File("C:\\temp\\sample3.bin");
OutputStream os = new BufferedOutputStream(new FileOutputStream(outputfile));
byte[] buffer2 = new byte[2];
zis.getNextEntry();
while(true) {
if(zis.read(buffer2) != -1) {
os.write(buffer2);
}
else break;
}
os.flush();
os.close();
zis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我能够使用此映像生成错误(将其保存为sample3.zip并在其上运行代码),但任何足够大小的二进制文件都应显示差异。
发布于 2017-04-19 18:01:25
while (true) {
if(zis.read(buffer2) != -1) {
os.write(buffer2);
}
else break;
}
常见的问题。你忽略了伯爵。应:
int count;
while ((count = zis.read(buffer2)) != -1)
{
os.write(buffer2, 0, count);
}
注:
flush()
在close()
之前是多余的。发布于 2017-04-19 17:39:32
您可以使用一种更逐字的方法来检查是否所有字节都是读和写的,例如
public int extract(ZipInputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
int total = 0;
int read;
while ((read = in.read(buffer)) != -1) {
total += read;
out.write(buffer, 0, read);
}
return total;
}
如果read
参数未在write()
中使用,则该方法假设将写出buffer
的全部内容,如果buffer
未完全填充,则可能不正确。
OutputStream
可以在extract()
方法内部或外部被刷新和关闭。调用close()
就足够了,因为它也调用了flush()
。
无论如何,Java的“标准”I/O代码(就像java.util.zip
包一样)已经过广泛的测试和使用,所以它不太可能有一个非常基本的错误,以至于很容易丢失字节。
https://stackoverflow.com/questions/43508690
复制