Here you can find the source of compress(String string)
public static byte[] compress(String string)
//package com.java2s; //License from project: Apache License import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.GZIPOutputStream; import javax.xml.bind.DatatypeConverter; public class Main { public static byte[] compress(String string) { byte[] compressed = null; try {//from ww w. ja va2 s . c o m ByteArrayOutputStream os = new ByteArrayOutputStream(string.length()); GZIPOutputStream gos = new GZIPOutputStream(os); gos.write(string.getBytes()); gos.close(); compressed = os.toByteArray(); os.close(); } catch (Exception e) { e.printStackTrace(); } return compressed; } public static void compress(String source_filepath, String destinaton_zip_filepath) throws Exception { byte[] buffer = new byte[1024]; FileOutputStream fileOutputStream = new FileOutputStream(destinaton_zip_filepath); GZIPOutputStream gzipOuputStream = new GZIPOutputStream(fileOutputStream); FileInputStream fileInput = new FileInputStream(source_filepath); int bytes_read; while ((bytes_read = fileInput.read(buffer)) > 0) { gzipOuputStream.write(buffer, 0, bytes_read); } fileInput.close(); gzipOuputStream.finish(); gzipOuputStream.close(); fileOutputStream.close(); } public static byte[] toByteArray(String s) { return DatatypeConverter.parseHexBinary(s); } }