Search code examples
javazipchecksum

Compare each line in a .txt file to a array String in Java


public static boolean checkChecksum(String filePath, String checksumPath) throws Exception{
    File file = new File(filePath);
    File checksum1 = new File(checksumPath);
    try {
        ZipFile zipFile = new ZipFile(file);     
        Enumeration e = zipFile.entries(); 
        while(e.hasMoreElements())  {
            ZipEntry entry = (ZipEntry)e.nextElement(); 
            String entryName = entry.getName();
            long crc = entry.getCrc();
            String current_crc = crc+"";
    }
    zipFile.close();
    return true;
    }       
    catch (IOException ioe) {
        log.debug("Zip file may be corrupted");
        System.out.println("Error opening zip file" + ioe);
        return false;
    } 
}

I want to compare each line in checksum1 file to each string value current_crc I get from file.zip.

How could I do it? And whether I have other solution to check files inside a zip file after moving from other to another PC.


Solution

  • whether I have other solution to check files inside a zip file after moving from other to another PC.

    Generally we use hash checksums for checking a file's integrity - they are much more efficient and almost as reliable. Example:

    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    byte[] hash = digest.digest(data);
    

    This code generates a small byte[] from the input byte[] (data). For checking the zip file it is enough to create a hash from the original zip and check if the copied zip's hash is the same. You can make use of the Arrays.equals() method or you can convert the byte arrays into Base64 strings for easier readability.

    Fun fact: The chance of two 256-bit hashes matching is roughly equivalent of correctly guessing a 49 characters long alphanumeric password in the first try.