KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > descriptor > ZipEntryRetriever


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Briclet Frederic
23 Contributor(s): ___________________________________________________.
24
25 ====================================================================*/

26
27
28 package org.objectweb.openccm.descriptor;
29
30 import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.util.LinkedList JavaDoc;
36 import java.util.zip.*;
37 import org.objectweb.openccm.descriptor.StreamManager;
38
39 /**
40  * The goal of thhis class is to provide facilities to
41  * retrieve a file into a zip file.
42  */

43 public class ZipEntryRetriever {
44     // ==================================================================
45
//
46
// Internal state.
47
//
48
// ==================================================================
49

50     //The main zip file
51
private ZipFile zf;
52     //The temporaryfile used to load the zipfile
53
private File JavaDoc tmpFile;
54     //The translation table to convert file path
55
private java.util.Hashtable JavaDoc table;
56     
57     private StreamManager streamManager;
58     
59     // ==================================================================
60
//
61
// Internal methods.
62
//
63
// ==================================================================
64

65     /**
66      * Internal method to setup the convert table
67      */

68     private void
69     setIdentifierTable()
70     {
71       String JavaDoc entryName;
72       
73       for(java.util.Enumeration JavaDoc entries=zf.entries();
74             entries.hasMoreElements();)
75         {
76             entryName=((java.util.zip.ZipEntry JavaDoc)entries.nextElement()).getName();
77             table.put(entryName.replace('\\','/').trim(),entryName);
78         }
79                 
80     }
81     
82     // ==================================================================
83
//
84
// Constructor.
85
//
86
// ==================================================================
87

88     /**
89      * Main constructor taking in prameter the zip file
90      * to wrap
91      * @param zf The zip file to wrap
92      */

93     public ZipEntryRetriever(ZipFile zf){
94      this.zf=zf;
95      table=new java.util.Hashtable JavaDoc();
96      setIdentifierTable();
97     }
98     
99     /**
100      *
101      * Build a new ZipEntryRetriever from a InputStream. The given
102      * inputStream will be entirely readed and buffered during initialization.
103      *
104      * @param in The inputStream to used to build the retriever
105      * @return
106      * @throws java.util.zip.ZipException
107      * @throws java.io.IOException
108      */

109     public
110     ZipEntryRetriever(InputStream JavaDoc in)
111     throws Exception JavaDoc
112     {
113         tmpFile=File.createTempFile("componentarchive"+System.currentTimeMillis(),".car");
114         FileOutputStream JavaDoc out=new FileOutputStream JavaDoc(tmpFile);
115         byte [] byteArray= new byte[32000];
116         
117         for(int readed=in.read(byteArray,0,32000); readed>0;
118             readed=in.read(byteArray,0,32000))
119             out.write(byteArray,0,readed);
120         
121         out.flush();
122         out.close();
123         zf=new ZipFile(tmpFile);
124         table=new java.util.Hashtable JavaDoc();
125         setIdentifierTable();
126         //tmpFile.deleteOnExit();
127
}
128     /**
129      *
130      * Build a new ZipEntryRetriever from a File already write
131      * on the current file system.
132      *
133      * @param in The File to used to build the retriever
134      * @return
135      * @throws java.util.zip.ZipException
136      * @throws java.io.IOException
137      */

138     public
139     ZipEntryRetriever(File JavaDoc file)
140     throws Exception JavaDoc
141     {
142         tmpFile=file;
143         zf=new ZipFile(tmpFile);
144      // getStreamManager().connectOpenZipFile(zf);
145
table=new java.util.Hashtable JavaDoc();
146         setIdentifierTable();
147     }
148     
149     // ==================================================================
150
//
151
// Public methods.
152
//
153
// ==================================================================
154

155     /**
156      * Return the ZipEntry denote by the given entryName. Directory delimiters
157      * can indefferently be '\' or '/' or a mix of both. In all case the entryName
158      * is translate in a internal representation to warrant the file resolution.
159      * @param entryName the entry file name.
160      */

161     public ZipEntry
162     retrieveZipEntryIgnoringDelimiterType(String JavaDoc entryName)
163     throws java.util.zip.ZipException JavaDoc, java.io.IOException JavaDoc
164     {
165        if(!table.containsKey(entryName.replace('\\','/').trim()))
166         throw new java.util.zip.ZipException JavaDoc("Cannot found zip entry:"+entryName) ;
167              
168        String JavaDoc originalName=(String JavaDoc)table.get(entryName.replace('\\','/'));
169        return zf.getEntry(originalName);
170     }
171     
172     /**
173      * This method return a list of entries that ended by the given
174      * @param ending.
175      *
176      * @param ending the suffix search in the entry
177      * @return
178      */

179     public String JavaDoc[]
180     getZipEntriesEndingBy(String JavaDoc ending)
181     {
182         LinkedList JavaDoc ll=new LinkedList JavaDoc();
183          for(java.util.Enumeration JavaDoc entries=zf.entries();
184                entries.hasMoreElements();)
185            {
186                ZipEntry ze=(java.util.zip.ZipEntry JavaDoc)entries.nextElement();
187                if(ze.getName().endsWith(ending))
188                 ll.add(ze.getName());
189            }
190        return (String JavaDoc[])ll.toArray(new String JavaDoc[ll.size()]);
191     }
192     
193     /**
194      * Return an InputStream open on the file denoted by the entryName.
195      * retrieveZipEntryIgnoringDelimiterType(String entryName) is used
196      * to found the zip entry.
197      * @param entryName the entry file name.
198      */

199     public java.io.InputStream JavaDoc
200     getZipEntryISIgnoringDelimiterType(String JavaDoc entryName)
201     throws java.util.zip.ZipException JavaDoc,java.io.IOException JavaDoc
202     {
203        java.io.InputStream JavaDoc is= zf.getInputStream(retrieveZipEntryIgnoringDelimiterType(entryName));
204        getStreamManager().connectOpenInputStream(is);
205        return is;
206     }
207     
208     /**
209      * This method open a new stream on the current zip file to allow a distant
210      * downloading.
211      *
212      * @return a new input stream on the current zip file
213      * @throws IOException is thrown if a problem occur using file.
214      */

215     public InputStream JavaDoc
216     getNewInputStream()
217     throws IOException JavaDoc
218     {
219         InputStream JavaDoc is=
220             new FileInputStream JavaDoc(tmpFile);
221         getStreamManager().connectOpenInputStream(is);
222         return is;
223     }
224     
225     /**
226      * Connect the default stream manager in charge to close
227      * all the open file and stream use by this zipEntryRetriever
228      */

229     public void
230     connectStreamManager(StreamManager streamManager)
231     {
232         this.streamManager=streamManager;
233         getStreamManager().connectOpenZipFile(zf);
234         if(tmpFile!=null)
235             getStreamManager().connectTemporaryFile(tmpFile);
236     }
237     
238     /**
239      * Return the current stream manager
240      */

241      public StreamManager
242      getStreamManager()
243      {
244          return streamManager;
245      }
246 }
247
Popular Tags