Sample code for 30+ languages & platforms
AutoIt Requires Chilkat v11.0.0+

Decompress Large Text File in Blocks

See more Compression Examples

Decompresses a large text file in blocks, and compares the restored (decompressed) file with the original to make sure it's correct.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  This example requires the Chilkat API to have been previously unlocked.
;  See Global Unlock Sample for sample code.

;  First, let's compress a text file.
;  We'll then decompress in blocks, and compare the decompressed with the original file.

;  Compress a text file:
$oCompress = ObjCreate("Chilkat.Compression")
$oCompress.Algorithm = "deflate"

$bSuccess = $oCompress.CompressFile("qa_data/hamlet.xml","qa_data/hamlet_compressed.dat")
If ($bSuccess = False) Then
    ConsoleWrite($oCompress.LastErrorText & @CRLF)
    Exit
EndIf

$oFac = ObjCreate("Chilkat.FileAccess")

;  Examine the uncompressed and compressed sizes:
Local $sOriginalPath = "qa_data/hamlet.xml"
;  Note: The FileSize method returns a signed 32-bit integer.  If the file is potentially larger than 2GB, call FileSizeStr instead to return
;  the size of the file as a string, then convert to an integer value.
ConsoleWrite("uncompressed size: " & $oFac.FileSize($sOriginalPath) & @CRLF)
ConsoleWrite("compressed size: " & $oFac.FileSize("qa_data/hamlet_compressed.dat") & @CRLF)

;  Decompress in blocks..
$oFacSrc = ObjCreate("Chilkat.FileAccess")
$oFacDest = ObjCreate("Chilkat.FileAccess")

$oFacSrc.OpenForRead("qa_data/hamlet_compressed.dat")

;  If we compress in 32K chunks, find out how many blocks there will be.
Local $iBlockSize = 32768
Local $iNumBlocks = $oFacSrc.GetNumBlocks($iBlockSize)

;  Open an output file for the decompressed data.
Local $sRestoredPath = "qa_output/hamlet_restored.xml"
$bSuccess = $oFacDest.OpenForWrite($sRestoredPath)
If ($bSuccess = False) Then
    ConsoleWrite($oFacDest.LastErrorText & @CRLF)
    Exit
EndIf

Local $sDecompressedStr
Local $oCompressedBytes

;  Assuming numBlocks > 1
$oCompress.FirstChunk = True
$oCompress.LastChunk = False

Local $i = 0
While $i < $iNumBlocks
    $oCompressedBytes = $oFacSrc.ReadBlock($i,$iBlockSize)
    $sDecompressedStr = $oCompress.DecompressString($oCompressedBytes)

    $oFacDest.AppendText($sDecompressedStr,"utf-8")

    $i = $i + 1

    $oCompress.FirstChunk = False
    If ($i = ($iNumBlocks - 1)) Then
        $oCompress.LastChunk = True
    EndIf

Wend

$oFacSrc.FileClose 
$oFacDest.FileClose 

;  Examine the size of the restored file.
ConsoleWrite("restored size: " & $oFac.FileSize($sRestoredPath) & @CRLF)

;  Compare the contents of the original with the restored.
Local $bEqualContents = $oFac.FileContentsEqual($sRestoredPath,$sOriginalPath)
ConsoleWrite("Contents Equal: " & $bEqualContents & @CRLF)