Go
Go
Compress Text Feed to Binary
See more Compression Examples
This example receives incoming text data in chunks, compresses as a stream, and accumulates the compressed binary data.Chilkat Go Downloads
success := false
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
bdCompressed := chilkat.NewBinData()
compress := chilkat.NewCompression()
compress.SetAlgorithm("deflate")
compress.SetCharset("utf-8")
sbUncompressedChunk := chilkat.NewStringBuilder()
compress.SetFirstChunk(true)
compress.SetLastChunk(false)
var i int
for i = 0; i <= 24; i++ {
if i == 24 {
compress.SetLastChunk(true)
}
sbUncompressedChunk.Clear()
sbUncompressedChunk.AppendInt(i)
sbUncompressedChunk.Append(": This is a line of data to be compressed...\r\n")
compress.CompressSb(sbUncompressedChunk,bdCompressed)
compress.SetFirstChunk(false)
}
// Show the compressed data in hex format:
fmt.Println("The hex encoded compressed text:")
fmt.Println(*bdCompressed.GetEncoded("hex"))
// Now decompress in one call. It is important to set both FirstChunk and LastChunk = true
bdDecompressed := chilkat.NewBinData()
compress.SetFirstChunk(true)
compress.SetLastChunk(true)
success = compress.DecompressBd2(bdCompressed,bdDecompressed)
if success == false {
fmt.Println(compress.LastErrorText())
bdCompressed.DisposeBinData()
compress.DisposeCompression()
sbUncompressedChunk.DisposeStringBuilder()
bdDecompressed.DisposeBinData()
return
}
originalText := bdDecompressed.GetString("utf-8")
fmt.Println(*originalText)
bdCompressed.DisposeBinData()
compress.DisposeCompression()
sbUncompressedChunk.DisposeStringBuilder()
bdDecompressed.DisposeBinData()