Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Tcl) Decompress Large Text File in BlocksDecompresses a large text file in blocks, and compares the restored (decompressed) file with the original to make sure it's correct.
load ./chilkat.dll # 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 != 1} 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 != 1} then { puts [CkFileAccess_lastErrorText $facDest] delete_CkCompression $compress delete_CkFileAccess $fac delete_CkFileAccess $facSrc delete_CkFileAccess $facDest exit } set compressedBytes [new_CkByteData] set i 0 while {$i < $numBlocks} { set success [CkFileAccess_ReadBlock $facSrc $i $blockSize $compressedBytes] if {$i == 0} then { set decompressedStr [CkCompression_beginDecompressString $compress $compressedBytes] } else { set decompressedStr [CkCompression_moreDecompressString $compress $compressedBytes] } CkFileAccess_AppendText $facDest $decompressedStr "utf-8" set i [expr $i + 1] } # At the very end, flush any remaining content, if any. set decompressedStr [CkCompression_endDecompressString $compress] CkFileAccess_AppendText $facDest $decompressedStr "utf-8" 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 |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.