Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

February 28, 2008

Zip CRC Calculation

The Chilkat.ZipCrc (.NET), CkZipCrc (C++, Perl, Java, Ruby, Python), and ChilkatZipCrc (ActiveX) class may be used to calculate Zip CRC’s from in-memory data or files. The ZipCrc class may also be used to calculate CRC’s independent of zips altogether. Effectively, it’s just another type of hash algorithm — the output happens to be 4 bytes (as opposed to SHA-1 which is 20 bytes, or MD5 which is 16 bytes).

This Visual Basic example demonstrates how to compare the CRC stored within a .zip with the CRC calculated from the file itself:

Private Sub Command15_Click()
	
    Dim zip As New ChilkatZip2
	
    Dim success As Long
	
    success = zip.UnlockComponent("Anything for 30-day trial")
    If (success <> 1) Then
        MsgBox zip.LastErrorText
        Exit Sub
    End If
	
    success = zip.NewZip("test.zip")
    If (success <> 1) Then
        MsgBox zip.LastErrorText
        Exit Sub
    End If
	
    success = zip.AppendOneFileOrDir("hamlet.xml", 0)
    If (success <> 1) Then
        MsgBox zip.LastErrorText
        Exit Sub
    End If
	
    ' Write the .zip, but don't close it because we're still going to use it...
    success = zip.WriteZip()
    If (success <> 1) Then
        MsgBox zip.LastErrorText
        Exit Sub
    End If
	
    ' Now we'll compare the CRC stored within the .zip for hamlet.xml against
    ' a CRC computed by reading hamlet.xml directly.  First, get the zip entry object
    ' for hamlet.xml and access the Crc property:
    Dim crcFromZip As Long
    Dim entry As ChilkatZipEntry2
	
    Set entry = zip.GetEntryByName("hamlet.xml")
    crcFromZip = entry.Crc
	
    ' The ChilkatZipCrc is used for calculating CRC's from data or files
    ' The FileCrc method calculates the CRC by reading a file.
    Dim crcCalculator As New ChilkatZipCrc
    crcFromFile = crcCalculator.FileCrc("hamlet.xml")
	
    ' Compare the CRC's, they should be equal:
    If (crcFromFile <> crcFromZip) Then
        MsgBox "CRC's are not equal!"
    Else
        MsgBox "CRC's are equal: " & Str(crcFromFile)
    End If
	
End Sub


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.