Java Zip String zipText(String text)

Here you can find the source of zipText(String text)

Description

zip Text

License

Open Source License

Declaration

public static byte[] zipText(String text) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayOutputStream;

import java.util.zip.GZIPOutputStream;

public class Main {
    public static byte[] zipText(String text) {
        byte[] ba = null;
        GZIPOutputStream gzos = null;
        ByteArrayOutputStream baos = null;
        try {//  w  ww . j  a  v  a2  s  .  c  om
            baos = new ByteArrayOutputStream();
            if (baos != null) {
                gzos = new GZIPOutputStream(baos);
                byte[] stringAsBytes = (text).getBytes("UTF-8");
                gzos.write(stringAsBytes, 0, stringAsBytes.length);
                gzos.finish();
                baos.close();
                ba = baos.toByteArray();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (baos != null) {
                try {
                    gzos.close();
                    //baos.close();
                } catch (Exception e1) {
                    System.out
                            .println("Exception while closing output stream");
                }
            }
        }
        //System.out.println("Zipped\n"+baos.toString());
        return ba;
    }
}

Related

  1. zip_Str(String in_str)
  2. zipString(String input)
  3. zipString(String str)
  4. zipStringAsFile(String contents, File destZipFile, String internalFilename, String comment)
  5. zipStringToBytes(String input)