Java Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

Java Examples

Quick Start
Java Unicode
Java Certs
Java Email
Java Encryption
Java FTP
HTML-to-XML
Java HTTP
Java IMAP
Java MHT
Java MIME
Java RSA
Java S/MIME
Java Signatures
Java Socket
Java Spider
Java Tar
Java Upload
Java XML
Java XMP
Java Zip

More Examples...
Email Object
POP3
SMTP
RSS
Atom
String
Byte Array
Self-Extractor
Service
PPMD
Deflate
DH Key Exchange
DSA
SSH Key
SSH
SSH Tunnel
SFTP

Unreleased...
Bzip2
LZW
Bz2
Icon

 

 

 

 

 

 

 

GZip Compress String

Compress and uncompress strings using the deflate algorithm. The underlying compression algorithm used by GZip is the deflate compression algorithm, which is also the most common algorithm used in the Zip file format (.zip). This method compresses a string by first converting it to the specified charset. This is necessary for programming languages where strings are represented as 2-byte/char Unicode. By indicating a charset, the size is already "comrpessed" by 50% (if the characters are an iso-8859 or windows-125* charset).

After charset conversion, the bytes are deflated. Finally, the resultant binary data is encoded according to the specified encoding ("base64", "hex", "url", "quoted-printable", etc.) and returned as a printable string. The most efficient encoding is Base64, which encodes 3 binary bytes in 4 printable characters.

Note: it only makes sense to compress strings that are long enough such that the expansion caused by (base64) encoding is relatively small.

Download Chilkat Java Library

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[])
  {
    CkGzip gzip = new CkGzip();

    bool success;

    //  Any string unlocks the component for the 1st 30-days.
    success = gzip.UnlockComponent("Anything for 30-day trial");
    if (success != true) {
        System.out.println(gzip.lastErrorText());
        return;
    }

    //  Create a string that is somewhat large.
    const char * s;
    s = "A friend called me up the other day and talked about investing in a dot-com";
    s = s + " that sells lobsters. Internet lobsters. Where will this end? --Donald Trump";
    s = s + s;
    s = s + s;

    //  Deflate and print the compressed string.
    const char * cs;
    cs = gzip.deflateStringENC(s,"windows-1252","base64");
    System.out.println(cs);

    //  Inflate to restore the string:
    const char * s2;
    s2 = gzip.inflateStringENC(cs,"windows-1252","base64");
    System.out.println(s2);
  }
}

 

Need a specific example? Send a request to support@chilkatsoft.com

© 2000-2008 Chilkat Software, Inc. All Rights Reserved.