German English
Data Processing

GZIP - Compression/Decompression

In this example, a data block with a length of 1024 bytes is compressed. For the compression of the text message (Lorem_ipsum_1024) the library "compress_j2me" is used which provides the GZIP algorithm. This library was originally developed for mobile phones with Java ME but can also be used on the radio module Preon32.
Compression of data is useful to reduce the amount of data which is to be transferred and therefore to reduce the time which is needed for whole data transmission. In this example, the text message is reduced from 1024 to 396 bytes. In case of a IEEE 802.15.4 frame which has 103 bytes of payload only 4 instead of 10 frames are needed to be transmitted.
The receiver of the data decompresses the data to get the original data.
/*
 * Copyright (c) 2011., Virtenio GmbH
 * All rights reserved.
 */

package com.virtenio.webdemo;

import com.googlecode.compress_j2me.gzip.Gzip;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class GzipCompressDecompress {

    private final static String Lorem_ipsum_1024 =
            "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy " +
            "eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam " +
            "voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet " +
            "clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit " +
            "amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam " +
            "nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, " +
            "sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum." +
            "Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor " +
            "sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed " +
            "diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat," +
            "sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum." +
            "Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor " +
            "sit amet. \n" +
            "\n" +
            "Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse " +
            "molestie consequat, vel illum dolore eu feugiat nulla facilisis a";

    public static void main(String[] args) {

        byte[] original = Lorem_ipsum_1024.getBytes();  // 1024 bytes total

        // Compress data with gzip
        byte[] compressed = new byte[0];
        try {
            ByteArrayInputStream in1 = new ByteArrayInputStream(original);
            ByteArrayOutputStream out1 = new ByteArrayOutputStream();
            int num1 = Gzip.gzip(in1, out1);
            compressed = out1.toByteArray();            // 396 bytes total
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Decompress data with gzip 
        byte[] decompressed = new byte[0];
        try {
            ByteArrayInputStream in2 = new ByteArrayInputStream(compressed);
            ByteArrayOutputStream out2 = new ByteArrayOutputStream();
            Gzip num2 = Gzip.gunzip(in2, out2);
            decompressed = out2.toByteArray();          // 1024 bytes total
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}