KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > store > impl > rdbms > StoreContentZip


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/impl/rdbms/StoreContentZip.java,v 1.6 2004/08/09 10:27:10 ozeigermann Exp $
3  * $Revision: 1.6 $
4  * $Date: 2004/08/09 10:27:10 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2003 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.store.impl.rdbms;
25
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.util.zip.ZipEntry JavaDoc;
32 import java.util.zip.ZipException JavaDoc;
33 import java.util.zip.ZipInputStream JavaDoc;
34 import java.util.zip.ZipOutputStream JavaDoc;
35
36 /**
37  * Title: StoreContentZip
38  *
39  * This util class can generally be used to zip/unzip an inputstream
40  * Returns the zip/unzip data as both output/input streams. This is used
41  * in the J2EEContentStore
42  * @version $Revision: 1.6 $
43  */

44
45 class StoreContentZip {
46
47     protected static final int ZIP_BUFFER = 2048;
48     private long contentLength = 0;
49     private long initialContentLength = -1;
50     private OutputStream JavaDoc theOS = null;
51
52     /**
53      * Constructor for StoreContentZip.
54      */

55     public StoreContentZip() {
56         super();
57         contentLength = 0;
58     }
59
60  /**
61    * This method compress the input stream and returns the outputstream
62    * @param InputStream inIPS
63    * @exception IOException,ZipException
64    * @return the compressed OutputStream
65    */

66
67     public void Zip(InputStream JavaDoc inIPS)
68                         throws IOException JavaDoc, ZipException JavaDoc{
69         int byteCount = 0;
70         contentLength = 0;
71                 initialContentLength = 0;
72         byte data[] = new byte[ZIP_BUFFER];
73         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
74         ZipOutputStream JavaDoc zoutp = new ZipOutputStream JavaDoc(baos);
75         zoutp.putNextEntry(new ZipEntry JavaDoc("zippedfile"));
76         while((byteCount = inIPS.read(data,0,ZIP_BUFFER)) != -1 ) {
77             zoutp.write(data,0,byteCount);
78                         initialContentLength += byteCount;
79         }
80         zoutp.finish();
81         zoutp.flush();
82         zoutp.close();
83         baos.flush();
84         baos.close();
85         contentLength = (long)baos.size();
86         theOS = baos;
87     }
88
89  /**
90    * This method decompress the input stream and returns the outputstream
91    * @param InputStream inIPS
92    * @exception IOException,ZipException
93    * @return the decompressed OutputStream
94    */

95     public void UnZip(InputStream JavaDoc inIPS)
96                         throws IOException JavaDoc, ZipException JavaDoc{
97         int byteCount = 0;
98         contentLength = 0;
99         byte indata[] = new byte[ZIP_BUFFER];
100         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
101         ZipInputStream JavaDoc zinp = new ZipInputStream JavaDoc(inIPS);
102         while (zinp.getNextEntry() != null) {
103             while ((byteCount = zinp.read(indata,0,ZIP_BUFFER)) != -1 ) {
104                 baos.write(indata,0,byteCount);
105             }
106         }
107         contentLength = (long)baos.size();
108         baos.flush();
109         baos.close();
110         zinp.close();
111         theOS = baos;
112     }
113
114  /**
115    * This method returns the compressed/decompressed stream as InputStream
116    * @param void
117    * @exception IOException,ZipException
118    * @return the processed InputStream
119    */

120     public InputStream JavaDoc getInputStream()
121                         throws IOException JavaDoc, ZipException JavaDoc{
122         return new ByteArrayInputStream JavaDoc(
123             ((ByteArrayOutputStream JavaDoc)theOS).toByteArray());
124     }
125
126  /**
127    * This method returns the compressed/decompressed stream as O/PStream
128    * @param void
129    * @exception IOException,ZipException
130    * @return the processed InputStream
131    */

132     public OutputStream JavaDoc getOutputStream()
133                         throws IOException JavaDoc, ZipException JavaDoc{
134         return theOS;
135     }
136
137     /**
138      * Gets the length.
139      * @return return the length of the un/compressed Stream
140      */

141     public long getContentLength() {
142         return contentLength;
143     }
144
145         public long getInitialContentLength() {
146         return initialContentLength;
147     }
148         
149 }
150
Popular Tags