we have a piece of code which generates a zip file on our system. Everything is ok, but sometimes this zip file while opened by FilZip or WinZip is considered to be corrupted.
So here is my question: how can we check programatically if a generated zip file is corrupted?
Here is the code we are using to generate our zip files:
try { ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tmpFile)); byte[] buffer = new byte[16384]; int contador = -1; for (DigitalFile digitalFile : document.getDigitalFiles().getContent()) { ZipEntry entry = new ZipEntry(digitalFile.getName()); FileInputStream fis = new FileInputStream(digitalFile.getFile()); try { zos.putNextEntry(entry); while ((counter = fis.read(buffer)) != -1) { zos.write(buffer, 0, counter); } fis.close(); zos.closeEntry(); } catch (IOException ex) { throw new OurException("It was not possible to read this file " + arquivo.getId()); } } try { zos.close(); } catch (IOException ex) { throw new OurException("We couldn't close this stream", ex); }
Is there anything we are doing wrong here?
EDIT: Actually, the code above is absolutely ok. My problem was that I was redirecting the WRONG stream for my users. So, instead of opening a zip file they where opening something completely different. Mea culpa :(
BUT the main question remains: how programatically I can verify if a given zip file is not corrupted?
To access the test function, open the Unzip tab (the Zip pane must be the active pane). Click the top part of the Diagnostics button to test the Zip file and view a summary report. To receive a more detailed report, click on the bottom half of the Diagnostics button and click Detailed on the dropdown menu.
ZIP files can get corrupted during the download process. If the download was interrupted, due to a power outage or an unexpected program closure even for a moment, unreadable data can end up becoming part of the downloaded ZIP file and make it difficult for the data to be extracted.
You can use the ZipFile
class to check your file :
static boolean isValid(final File file) { ZipFile zipfile = null; try { zipfile = new ZipFile(file); return true; } catch (IOException e) { return false; } finally { try { if (zipfile != null) { zipfile.close(); zipfile = null; } } catch (IOException e) { } } }
I know its been a while that this has been posted, I have used the code that all of you provided and came up with this. This is working great for the actual question. Checking if the zip file is corrupted or not
private boolean isValid(File file) { ZipFile zipfile = null; ZipInputStream zis = null; try { zipfile = new ZipFile(file); zis = new ZipInputStream(new FileInputStream(file)); ZipEntry ze = zis.getNextEntry(); if(ze == null) { return false; } while(ze != null) { // if it throws an exception fetching any of the following then we know the file is corrupted. zipfile.getInputStream(ze); ze.getCrc(); ze.getCompressedSize(); ze.getName(); ze = zis.getNextEntry(); } return true; } catch (ZipException e) { return false; } catch (IOException e) { return false; } finally { try { if (zipfile != null) { zipfile.close(); zipfile = null; } } catch (IOException e) { return false; } try { if (zis != null) { zis.close(); zis = null; } } catch (IOException e) { return false; } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With