Java Zip String zip(String str)

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

Description

zip

License

Apache License

Declaration

public static final String zip(String str) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {

    public static final String zip(String str) {
        if (str == null)
            return null;
        byte[] compressed;
        ByteArrayOutputStream out = null;
        ZipOutputStream zout = null;
        String compressedStr = null;
        try {/*from w  ww  .ja v a  2  s  .  co m*/
            out = new ByteArrayOutputStream();
            zout = new ZipOutputStream(out);
            zout.putNextEntry(new ZipEntry("0"));
            zout.write(str.getBytes());
            zout.closeEntry();
            compressed = out.toByteArray();
            compressedStr = new sun.misc.BASE64Encoder().encodeBuffer(compressed);
        } catch (IOException e) {
            compressed = null;
        } finally {
            if (zout != null) {
                try {
                    zout.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return compressedStr;
    }
}

Related

  1. addStringToZip(String text, String entryName, ZipOutputStream zOut)
  2. generateZip(String kmlContents)
  3. zip(String content)
  4. zip(String content)
  5. zip(String toCompress)
  6. zip_Str(String in_str)
  7. zipString(String input)
  8. zipString(String str)