How to check if a generated zip file is corrupted?

后端 未结 8 1672
梦毁少年i
梦毁少年i 2020-12-05 10:33

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 corrupte

相关标签:
8条回答
  • 2020-12-05 10:38

    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) {
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-05 10:39

    in my implementation it looks like that. maybe it helps you:

    //[...]
    
    try {
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
    
        zos.putNextEntry(new ZipEntry(file.getName()));
    
        try {
            final byte[] buf = new byte[BUFFER_SIZE];
            while (true) {
                final int len = bis.read(buf);
                if (len == -1) {
                    break;
                }
                zos.write(buf, 0, len);
            }
            zos.flush();
            zos.closeEntry();
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                LOG.debug("Buffered Stream closing failed");
            } finally {
                fis.close();
            }
        }
    } catch (IOException e) {
        throw new Exception(e);
    }
    
    //[...]
    zos.close
    
    0 讨论(0)
  • 2020-12-05 10:41
    new ZipFile(file) 
    

    compress again the file, so duplicate efforts and that is not what you are looking for. Despite of the fact that only check one file and the question compress n-files.

    Take a look to this: http://www.kodejava.org/examples/336.html

    Create a checksum for your zip:

    CheckedOutputStream checksum = new CheckedOutputStream(fos, new CRC32());
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(checksum));
    ...
    

    And when you finish the compression show it

    System.out.println("Checksum   : " + checksum.getChecksum().getValue());
    

    You must do the same reading the zip with java or others tools checking if checksums match.

    see https://stackoverflow.com/a/10689488/848072 for more information

    0 讨论(0)
  • 2020-12-05 10:49

    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;
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-12-05 10:50

    I think you'll see correspondent exception stack trace during zip-file generation. So, you probably wan't to enhance your exception handling.

    0 讨论(0)
  • 2020-12-05 10:54

    Perhaps swap the following two lines?;

    fis.close();
    zos.closeEntry();
    

    I can imagine that the closeEntry() will still read some data from the stream.

    0 讨论(0)
提交回复
热议问题