Chilkat HOME Android™ ASP Visual Basic VB.NET C# iOS (IPhone) Objective-C C++ C Unicode C++ Unicode C MFC Delphi DLL Delphi ActiveX FoxPro Java Perl PHP Extension PHP ActiveX Python PowerShell Ruby SQL Server VBScript
|
(Java) Streaming Deflate StringDemonstrates streaming string compression using the Deflate algorithm. Data may be compressed in chunks by initially calling one of the BeginCompress* methods, followed by 0 or more calls to a MoreCompress* method, and terminating with a call to an EndCompress* method. Likewise, data may be decompressed by calling a BeginDecompress* method, followed by 0 or more MoreDecompress* calls, and ending with an EndDecompress* method call. This example demonstrates string-to-string deflate compression and decompression in chunks. (The deflate algorithm is most commonly used by the .zip file format.)
import com.chilkatsoft.*; public class ChilkatExample { static { try { System.loadLibrary("chilkat"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load.\n" + e); System.exit(1); } } public static void main(String argv[]) { CkCompression compress = new CkCompression(); // Any string argument automatically begins a 30-day trial. boolean success; success = compress.UnlockComponent("30-day trial"); if (success != true) { System.out.println(compress.lastErrorText()); return; } compress.put_Algorithm("deflate"); compress.put_Charset("ansi"); compress.put_EncodingMode("base64"); String strData; int i; String strCompressed; // Create a long string to compress. strData = "abcdefg1122334455667788"; strData = strData + "\r\n"; // Note: BeginCompressStringENC may return a zero-length // string. This is normal if the input is buffered and no // compressed data is yet available. // However, a null reference indicates an error -- which is // generally only possible if the component was never unlocked. strCompressed = compress.beginCompressStringENC(strData); // Compress 100 more chunks of data. Each call to // MoreCompressStringENC may or may not produce additional // compressed output. for (i = 0; i <= 99; i++) { strData = "abcdefg1122334455667788"; strData = strData + "\r\n"; strCompressed = strCompressed + compress.moreCompressStringENC(strData); } // Finalize the compression with a single call to EndCompressStringENC: strCompressed = strCompressed + compress.endCompressStringENC(); // Display the compressed string. // The result will be: // S0xKTklNSzc0NDIyNjYxMTU1MzM3t7Dg5UoclRiVGJUYlRiVGJUYlRiVGJUYchIA System.out.println(strCompressed); // Decompress back to the original in a single call. String strOriginal; strOriginal = compress.decompressStringENC(strCompressed); // Display the uncompressed original: System.out.println("--- Original ---"); System.out.println(strOriginal); // Inflate in multiple calls. It doesn't matter how the // compressed data is split -- it could be fed to the decompressor // one char at a time if desired. String chunk1; chunk1 = "S0xKTklNSzc0ND"; String chunk2; chunk2 = "Iy"; String chunk3; chunk3 = "NjYxMTU1MzM3t7Dg5UoclRiV"; String chunk4; chunk4 = "GJUYlRiVGJUYlRiVGJUYchIA"; strOriginal = ""; strOriginal = compress.beginDecompressStringENC(chunk1); strOriginal = strOriginal + compress.moreDecompressStringENC(chunk2); strOriginal = strOriginal + compress.moreDecompressStringENC(chunk3); strOriginal = strOriginal + compress.moreDecompressStringENC(chunk4); strOriginal = strOriginal + compress.endDecompressStringENC(); // Display the uncompressed original after inflating in chunks: System.out.println("--- Original after Inflating from Chunks ---"); System.out.println(strOriginal); } } |
© 2000-2013 Chilkat Software, Inc. All Rights Reserved.