![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Node.js) Compressing StringBuilder Data Using CompressSb (Single Call and Chunked)See more Compression ExamplesThis example demonstrates how to compress text stored in aStringBuilder using the CompressSb method in two ways:
The example shows how compressed output is appended to a Key Points
var os = require('os'); if (os.platform() == 'win32') { var chilkat = require('@chilkat/ck-node23-win64'); } else if (os.platform() == 'linux') { if (os.arch() == 'arm') { var chilkat = require('@chilkat/ck-node23-linux-arm'); } else if (os.arch() == 'arm64') { var chilkat = require('@chilkat/ck-node23-linux-arm64'); } else { var chilkat = require('@chilkat/ck-node23-linux-x64'); } } else if (os.platform() == 'darwin') { var chilkat = require('@chilkat/ck-node23-mac-universal'); } function chilkatExample() { var success = false; // This example assumes the Chilkat API has already been unlocked. // See Global Unlock Sample for sample code. var compress = new chilkat.Compression(); compress.Algorithm = "zlib"; // ================================================================ // 1) Single-call compression (entire data in one call) // ================================================================ var sb = new chilkat.StringBuilder(); sb.Append("The quick brown fox jumps over the lazy dog. "); sb.Append("This is a simple example using CompressSb."); var bdCompressed = new chilkat.BinData(); // When both FirstChunk and LastChunk are true (the defaults), // the entire compression happens in a single call. compress.FirstChunk = true; compress.LastChunk = true; success = compress.CompressSb(sb,bdCompressed); if (success == false) { console.log(compress.LastErrorText); return; } var compressedBase64 = bdCompressed.GetEncoded("base64"); console.log("Single-call compressed (base64):"); console.log(compressedBase64); // ================================================================ // 2) Chunked compression using FirstChunk / LastChunk // ================================================================ var bdChunkedOut = new chilkat.BinData(); // First chunk compress.FirstChunk = true; compress.LastChunk = false; var sbPart = new chilkat.StringBuilder(); sbPart.Append("The quick brown fox "); success = compress.CompressSb(sbPart,bdChunkedOut); if (success == false) { console.log(compress.LastErrorText); return; } // Middle chunk compress.FirstChunk = false; compress.LastChunk = false; sbPart.Clear(); sbPart.Append("jumps over the lazy dog. "); success = compress.CompressSb(sbPart,bdChunkedOut); if (success == false) { console.log(compress.LastErrorText); return; } // Final chunk compress.FirstChunk = false; compress.LastChunk = true; sbPart.Clear(); sbPart.Append("This is a chunked CompressSb example."); success = compress.CompressSb(sbPart,bdChunkedOut); if (success == false) { console.log(compress.LastErrorText); return; } var chunkedBase64 = bdChunkedOut.GetEncoded("base64"); console.log("Chunked compressed (base64):"); console.log(chunkedBase64); // ================================================================ // 3) Decompress to verify correctness // ================================================================ var sbDecompressed = new chilkat.StringBuilder(); // Decompress in a single call (entire data already assembled) compress.FirstChunk = true; compress.LastChunk = true; success = compress.DecompressSb(bdChunkedOut,sbDecompressed); if (success == false) { console.log(compress.LastErrorText); return; } console.log("Decompressed text:"); console.log(sbDecompressed.GetAsString()); } chilkatExample(); |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.