Here you can find the source of compressToFile(File dest, String str)
public static void compressToFile(File dest, String str) throws IOException
//package com.java2s; import java.io.*; import java.util.zip.GZIPOutputStream; public class Main { public static void compressToFile(File dest, String str) throws IOException { if (str == null) { return; }/* w w w . j av a 2s. com*/ GZIPOutputStream zipOut = null; try { zipOut = new GZIPOutputStream(new FileOutputStream(dest)); zipOut.write(str.getBytes()); } finally { if (zipOut != null) { try { zipOut.close(); } catch (IOException e) { e.printStackTrace(); } } } } }