1 16 package org.apache.myfaces.util.zip; 17 18 import org.apache.commons.codec.binary.Base64; 19 20 import java.io.*; 21 import java.util.zip.GZIPInputStream ; 22 import java.util.zip.GZIPOutputStream ; 23 24 32 public class ZipUtils 33 { 34 public static final String ZIP_CHARSET = "ISO-8859-1"; 35 36 37 private ZipUtils() 38 { 39 } 41 42 43 45 public static String unzipString(String s) 46 { 47 try 48 { 49 Base64 base64Codec = new Base64(); 50 ByteArrayInputStream decodedStream = new ByteArrayInputStream( base64Codec.decode( s.getBytes(ZIP_CHARSET) ) ); 51 InputStream unzippedStream = new GZIPInputStream (decodedStream); 52 53 StringBuffer buf = new StringBuffer (); 54 int c; 55 while ((c = unzippedStream.read()) != -1) 56 { 57 buf.append((char)c); 58 } 59 60 unzippedStream.close(); 61 decodedStream.close(); 62 63 return buf.toString(); 64 } 65 catch (IOException e) 66 { 67 throw new RuntimeException (e); 68 } 69 } 70 71 73 public static String zipString(String s) 74 { 75 try 76 { 77 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 78 OutputStream zos = new GZIPOutputStream (baos); 79 OutputStreamWriter writer = new OutputStreamWriter(zos, ZIP_CHARSET); 80 81 writer.write(s); 82 83 writer.close(); 84 zos.close(); 85 baos.close(); 86 87 Base64 base64Codec = new Base64(); 88 return new String (base64Codec.encode( baos.toByteArray() ), ZIP_CHARSET); 89 } 90 catch (IOException e) 91 { 92 throw new RuntimeException (e); 93 } 94 } 95 96 97 } 98 99 | Popular Tags |