View Single Post
Old 04-21-2009, 08:38 AM   #2 (permalink)
pravin
Knows Where the Search Button Is
 
pravin's Avatar
 
Join Date: Feb 2008
Model: 8130
PIN: N/A
Carrier: Sprint
Posts: 37
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this -

Code:
/**
     * Uncompress a previously compressed string;
     * this method is the inverse of the compress method.
     * @param byte array containing compressed data
     * @return uncompressed string
     * @throws IOException if the inflation fails
     */
    public static final byte[] uncompress(final byte[] compressed) throws IOException {
//        String uncompressed = "";
        byte[] result = null;
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(compressed);
            GZIPInputStream zis = new GZIPInputStream(bais);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int numBytesRead = 0;
            byte[] tempBytes = new byte[DEFAULT_BUFFER_SIZE];
            while ((numBytesRead = zis.read(tempBytes, 0, tempBytes.length)) != -1) {
                baos.write(tempBytes, 0, numBytesRead);
            }

            result = (baos.toByteArray());
        } catch (Exception e) {
            System.out.println("Error while unzip " + e.toString());
        }

        return result;
    }





/**
     * Compress a string
     * @param uncompressed string
     * @return byte array containing compressed data
     * @throws IOException if the deflation fails
     */
    public static final byte[] compress(final byte[] uncompressed) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream zos = new GZIPOutputStream(baos);

        byte[] uncompressedBytes = uncompressed;

        zos.write(uncompressedBytes, 0, uncompressedBytes.length);
        zos.close();

        return baos.toByteArray();
    }
Offline   Reply With Quote