com.qatickets.common.ZIPHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.qatickets.common.ZIPHelper.java

Source

/*
The MIT License (MIT)
    
Copyright (c) 2015 QAXML Pty Ltd
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.qatickets.common;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

import org.apache.commons.io.Charsets;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class ZIPHelper {

    private static final Log log = LogFactory.getLog(ZIPHelper.class);

    public static byte[] compress(String string) {
        try {
            return compress(string.getBytes(Charsets.UTF_8.displayName()));
        } catch (UnsupportedEncodingException e) {
            log.error(e);
        }
        return null;
    }

    public static byte[] compress(byte[] data) {

        // Create the compressor with highest level of compression
        final Deflater compressor = new Deflater();
        compressor.setLevel(Deflater.BEST_COMPRESSION);

        // Give the compressor the data to compress
        compressor.setInput(data);
        compressor.finish();

        // Create an expandable byte array to hold the compressed data.
        // You cannot use an array that's the same size as the orginal because
        // there is no guarantee that the compressed data will be smaller than
        // the uncompressed data.
        final ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);

        // Compress the data
        byte[] buf = new byte[1024];
        while (!compressor.finished()) {
            final int count = compressor.deflate(buf);
            bos.write(buf, 0, count);
        }
        try {
            bos.close();
        } catch (IOException e) {
        }

        // Get the compressed data
        final byte[] compressedData = bos.toByteArray();

        compressor.end();

        return compressedData;
    }

    public static byte[] decompressAsBytes(byte[] data) {

        if (data == null) {
            return null;
        }

        // Create the decompressor and give it the data to compress
        final Inflater decompressor = new Inflater();
        decompressor.setInput(data);

        // Create an expandable byte array to hold the decompressed data
        final ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);

        // Decompress the data
        final byte[] buf = new byte[1024];
        while (!decompressor.finished()) {
            try {
                final int count = decompressor.inflate(buf);
                bos.write(buf, 0, count);
            } catch (DataFormatException e) {
            }
        }
        try {
            bos.close();
        } catch (IOException e) {
        }

        // Get the decompressed data
        final byte[] decompressedData = bos.toByteArray();

        decompressor.end();

        return decompressedData;
    }

    public static String decompress(byte[] data) {

        if (data == null) {
            return null;
        }

        // Get the decompressed data
        final byte[] decompressedData = decompressAsBytes(data);

        try {
            return new String(decompressedData, Charsets.UTF_8.displayName());
        } catch (UnsupportedEncodingException e) {
            log.error(e);
        }
        return null;
    }

}