Android String Compress compressToBytes(String str)

Here you can find the source of compressToBytes(String str)

Description

compress To Bytes

Declaration

public static byte[] compressToBytes(String str) 

Method Source Code

//package com.java2s;
import java.io.*;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    public static byte[] compressToBytes(String str) {
        if (str == null) {
            return null;
        }//from w  ww.ja v a2 s  . co  m

        byte[] zipBytes = null;
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ZipOutputStream zipOut = null;

        try {
            zipOut = new ZipOutputStream(byteOut);
            zipOut.putNextEntry(new ZipEntry("zipEntry"));
            zipOut.write(str.getBytes());
            zipOut.closeEntry();
            zipBytes = byteOut.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zipOut != null) {
                try {
                    zipOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return zipBytes;
    }
}