GZIP a File and return byte array - Android File Input Output

Android examples for File Input Output:Zip File

Description

GZIP a File and return byte array

Demo Code


//package com.java2s;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import java.util.zip.GZIPOutputStream;

public class Main {

    public static byte[] encryptGZIP(String str) {
        if (str == null || str.length() == 0) {
            return null;
        }/* w w  w . ja  v a  2s  . c o  m*/

        try {
            // gzip
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            GZIPOutputStream gzip = new GZIPOutputStream(baos);
            gzip.write(str.getBytes("UTF-8"));

            gzip.close();

            byte[] encode = baos.toByteArray();

            baos.flush();
            baos.close();

            // base64 
            return encode;
            //          return new String(encode, "UTF-8");

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

Related Tutorials