Sample code for 30+ languages & platforms
Tcl 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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  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:
set compress [new_CkCompression]

CkCompression_put_Algorithm $compress "deflate"

set success [CkCompression_CompressFile $compress "qa_data/hamlet.xml" "qa_data/hamlet_compressed.dat"]
if {$success == 0} then {
    puts [CkCompression_lastErrorText $compress]
    delete_CkCompression $compress
    exit
}

set fac [new_CkFileAccess]

#  Examine the uncompressed and compressed sizes:
set originalPath "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.
puts "uncompressed size: [CkFileAccess_FileSize $fac $originalPath]"
puts "compressed size: [CkFileAccess_FileSize $fac qa_data/hamlet_compressed.dat]"

#  Decompress in blocks..
set facSrc [new_CkFileAccess]

set facDest [new_CkFileAccess]

CkFileAccess_OpenForRead $facSrc "qa_data/hamlet_compressed.dat"

#  If we compress in 32K chunks, find out how many blocks there will be.
set blockSize 32768
set numBlocks [CkFileAccess_GetNumBlocks $facSrc $blockSize]

#  Open an output file for the decompressed data.
set restoredPath "qa_output/hamlet_restored.xml"
set success [CkFileAccess_OpenForWrite $facDest $restoredPath]
if {$success == 0} then {
    puts [CkFileAccess_lastErrorText $facDest]
    delete_CkCompression $compress
    delete_CkFileAccess $fac
    delete_CkFileAccess $facSrc
    delete_CkFileAccess $facDest
    exit
}

set compressedBytes [new_CkByteData]

#  Assuming numBlocks > 1
CkCompression_put_FirstChunk $compress 1
CkCompression_put_LastChunk $compress 0

set i 0
while {$i < $numBlocks} {
    set success [CkFileAccess_ReadBlock $facSrc $i $blockSize $compressedBytes]
    set decompressedStr [CkCompression_decompressString $compress $compressedBytes]

    CkFileAccess_AppendText $facDest $decompressedStr "utf-8"

    set i [expr $i + 1]

    CkCompression_put_FirstChunk $compress 0
    if {$i == [expr $numBlocks - 1]} then {
        CkCompression_put_LastChunk $compress 1
    }

}

CkFileAccess_FileClose $facSrc
CkFileAccess_FileClose $facDest

#  Examine the size of the restored file.
puts "restored size: [CkFileAccess_FileSize $fac $restoredPath]"

#  Compare the contents of the original with the restored.
set bEqualContents [CkFileAccess_FileContentsEqual $fac $restoredPath $originalPath]
puts "Contents Equal: $bEqualContents"

delete_CkCompression $compress
delete_CkFileAccess $fac
delete_CkFileAccess $facSrc
delete_CkFileAccess $facDest
delete_CkByteData $compressedBytes