AutoIt
AutoIt
Streaming Compression
See more Compression Examples
Compress and decompress using a stream.Chilkat AutoIt Downloads
Local $bSuccess = False
; This example assumes the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
$oFac = ObjCreate("Chilkat.FileAccess")
$oCompress = ObjCreate("Chilkat.Compression")
$oCompress.Algorithm = "deflate"
$oStreamC = ObjCreate("Chilkat.Stream")
; This example sets the source and sink of the stream to files.
; A stream can have use other streams as a source or sink,
; or the application can itself be the source/sink by directly
; reading or writing a stream. (See below for an example of this..)
$oStreamC.SourceFile = "qa_data/hamlet.xml"
$oStreamC.SinkFile = "qa_output/hamlet_compressed.dat"
; Compress from source to sink.
$bSuccess = $oCompress.CompressStream($oStreamC)
If ($bSuccess <> True) Then
ConsoleWrite($oCompress.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("File-to-file deflate compression successful." & @CRLF)
; 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("Original size = " & $oFac.FileSize($oStreamC.SourceFile) & @CRLF)
ConsoleWrite("Compressed size = " & $oFac.FileSize($oStreamC.SinkFile) & @CRLF)
; Now do file-to-file decompression
$oStreamD = ObjCreate("Chilkat.Stream")
$oStreamD.SourceFile = "qa_output/hamlet_compressed.dat"
$oStreamD.SinkFile = "qa_output/hamlet_restored.xml"
; Decompress from source to sink.
$bSuccess = $oCompress.DecompressStream($oStreamD)
If ($bSuccess <> True) Then
ConsoleWrite($oCompress.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("File-to-file deflate decompression successful." & @CRLF)
; Let's double-check to see that the files are equal in size and content:
Local $bFilesEqual = $oFac.FileContentsEqual($oStreamC.SourceFile,$oStreamD.SinkFile)
If ($bFilesEqual <> True) Then
ConsoleWrite("The output file is not equal to the input file!" & @CRLF)
Else
ConsoleWrite("The file was successfully compressed and decompressed." & @CRLF)
EndIf
; ---------------------------------------------------------------------
; Now let's decompress again, but this time w/ the application reading
; the decompressed data directly from a stream.
$oStreamA = ObjCreate("Chilkat.Stream")
$oStreamA.SourceFile = "qa_output/hamlet_compressed.dat"
; Start decompressing in a background thread.
Local $oTask = $oCompress.DecompressStreamAsync($oStreamA)
$bSuccess = $oTask.Run()
; Read decompressed data from streamA:
Local $sDecompressedText
While ($oStreamA.EndOfStream <> True)
If ($oStreamA.DataAvailable = True) Then
$sDecompressedText = $oStreamA.ReadString()
ConsoleWrite($sDecompressedText & @CRLF)
EndIf
Wend
; Let's make sure the background task finished and that the decompress was successful.
; It should already be the case that the task is finished.
While ($oTask.Finished <> True)
$oTask.SleepMs 20
Wend
; The decompressor may have finished, but it is possible that data
; remains to be flushed.
If ($oStreamA.DataAvailable = True) Then
$sDecompressedText = $oStreamA.ReadString()
ConsoleWrite($sDecompressedText & @CRLF)
EndIf
; Did streamA succeed in reading the entire file?
If ($oTask.TaskSuccess <> True) Then
ConsoleWrite("async decompress failed:" & @CRLF)
ConsoleWrite($oTask.ResultErrorText & @CRLF)
$bSuccess = False
EndIf
ConsoleWrite("The async decompress was successful." & @CRLF)