I am trying to copy a zip file while is being read but the problem is that the copy is not the same as the source, even if the number of bytes are the same. Anyone can see/explain what I'm doing wrong? Thanks!
File fileIn = new File("In.zip");
File fileOut = new File("Out.zip");
final OutputStream out = new FileOutputStream(fileOut);
final AtomicInteger totalBytesRead = new AtomicInteger();
BufferedInputStream copy = new BufferedInputStream(new FileInputStream(fileIn)) {
@Override
public synchronized int read(byte[] b, int off, int len) throws IOException {
int total = super.read(b, off, len);
if (total != -1) {
totalBytesRead.addAndGet(total);
out.write(b, 0, total);
}
return total;
}
};
ZipInputStream zipIn = new ZipInputStream(copy);
ZipEntry zipEntry = null;
while ((zipEntry = zipIn.getNextEntry()) != null) {
zipIn.closeEntry();
}
IOUtils.copy(copy, new OutputStream() {
@Override
public void write(int b) throws IOException {
}
});
zipIn.close();
out.close();
System.out.println("Expected: " + fileIn.length() + ", Actual: " + totalBytesRead);
System.out.println(FileUtils.contentEquals(fileIn, fileOut));
The output is:
Expected: 3695, Actual: 3695
false