Classic ASP
Classic ASP
Chunked Compression Using CompressBd2 with FirstChunk and LastChunk
See more Compression Examples
This example demonstrates how to compress data in multiple segments using theCompressBd2 method together with the FirstChunk and LastChunk properties. Instead of compressing all data in a single call, the input is divided into smaller chunks and processed sequentially, which is useful when handling streaming data or large inputs.
The example shows how to correctly mark the first, middle, and final chunks so that the compression stream is properly initialized and finalized. The compressed output is accumulated in a separate BinData object without modifying the input chunks. Finally, the example decompresses the combined result to verify that the original data is restored correctly.
This approach is ideal for scenarios where data is received incrementally, such as reading from a network stream, processing large files in parts, or handling real-time data feeds.
Chilkat Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0
' This example assumes the Chilkat API has already been unlocked.
' See Global Unlock Sample for sample code.
set compress = Server.CreateObject("Chilkat.Compression")
compress.Algorithm = "zlib"
' This will accumulate the compressed output.
set bdOut = Server.CreateObject("Chilkat.BinData")
' ------------------------------------------------------------------
' Simulate input arriving in chunks.
' ------------------------------------------------------------------
part1 = "The quick brown fox "
part2 = "jumps over the lazy dog. "
part3 = "This text is split into chunks."
' ------------------------------------------------------------------
' Compress the first chunk
' ------------------------------------------------------------------
compress.FirstChunk = 1
compress.LastChunk = 0
set bdIn = Server.CreateObject("Chilkat.BinData")
success = bdIn.AppendString(part1,"utf-8")
success = compress.CompressBd2(bdIn,bdOut)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( compress.LastErrorText) & "</pre>"
Response.End
End If
' ------------------------------------------------------------------
' Compress a middle chunk
' ------------------------------------------------------------------
compress.FirstChunk = 0
compress.LastChunk = 0
success = bdIn.Clear()
success = bdIn.AppendString(part2,"utf-8")
success = compress.CompressBd2(bdIn,bdOut)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( compress.LastErrorText) & "</pre>"
Response.End
End If
' ------------------------------------------------------------------
' Compress the final chunk
' ------------------------------------------------------------------
compress.FirstChunk = 0
compress.LastChunk = 1
success = bdIn.Clear()
success = bdIn.AppendString(part3,"utf-8")
success = compress.CompressBd2(bdIn,bdOut)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( compress.LastErrorText) & "</pre>"
Response.End
End If
' Get the final compressed result as base64 for display
compressedBase64 = bdOut.GetEncoded("base64")
Response.Write "<pre>" & Server.HTMLEncode( "Compressed data (base64):") & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( compressedBase64) & "</pre>"
' ------------------------------------------------------------------
' Decompress to verify correctness
' ------------------------------------------------------------------
set bdDecompressed = Server.CreateObject("Chilkat.BinData")
compress.FirstChunk = 1
compress.LastChunk = 1
success = compress.DecompressBd2(bdOut,bdDecompressed)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( compress.LastErrorText) & "</pre>"
Response.End
End If
' Convert decompressed bytes back to a string
resultText = bdDecompressed.GetString("utf-8")
Response.Write "<pre>" & Server.HTMLEncode( "Decompressed text:") & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( resultText) & "</pre>"
%>
</body>
</html>