KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > util > zip > ZipUtils


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
22 import java.util.zip.GZIPOutputStream JavaDoc;
23
24 /**
25  * DOCUMENT ME!
26  * @author Manfred Geiler (latest modification by $Author: matze $)
27  * @version $Revision: 1.9 $ $Date: 2004/10/13 11:51:01 $
28  *
29  * Revision 1.7 2004/04/09 21:13:10 Sylvain Vieujot
30  * Replace oreilly's Base64 encoder and decoder with Jakarta Commons Codec
31  */

32 public class ZipUtils
33 {
34     public static final String JavaDoc ZIP_CHARSET = "ISO-8859-1";
35
36
37     private ZipUtils()
38     {
39         // hide from public access
40
}
41
42
43     /**
44      */

45     public static String JavaDoc unzipString(String JavaDoc s)
46     {
47         try
48         {
49             Base64 base64Codec = new Base64();
50             ByteArrayInputStream decodedStream = new ByteArrayInputStream( base64Codec.decode( s.getBytes(ZIP_CHARSET) ) );
51             InputStream JavaDoc unzippedStream = new GZIPInputStream JavaDoc(decodedStream);
52
53             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc(e);
68         }
69     }
70
71     /**
72      */

73     public static String JavaDoc zipString(String JavaDoc s)
74     {
75         try
76         {
77             ByteArrayOutputStream baos = new ByteArrayOutputStream();
78             OutputStream JavaDoc zos = new GZIPOutputStream JavaDoc(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 JavaDoc(base64Codec.encode( baos.toByteArray() ), ZIP_CHARSET);
89         }
90         catch (IOException e)
91         {
92             throw new RuntimeException JavaDoc(e);
93         }
94     }
95
96
97 }
98
99
Popular Tags