KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > snapper > parsers > ZIPParser


1 package org.enhydra.snapper.parsers;
2
3
4 import java.io.BufferedInputStream JavaDoc;
5 import java.io.BufferedOutputStream JavaDoc;
6 import java.io.File JavaDoc;
7 import java.io.FileNotFoundException JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.Vector JavaDoc;
13 import java.util.zip.ZipEntry JavaDoc;
14 import java.util.zip.ZipFile JavaDoc;
15
16
17 public class ZIPParser implements org.enhydra.snapper.api.Parser {
18       private String JavaDoc fileName;
19       private String JavaDoc parsedText;
20       private String JavaDoc title = "";
21       private String JavaDoc dest = "";
22       StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
23       Vector JavaDoc files;
24       private long time;
25       
26       public String JavaDoc parse(InputStream JavaDoc is) throws java.io.IOException JavaDoc{ return "";}
27       
28       public ZIPParser(String JavaDoc dest){
29               this.dest = dest;
30          
31       }
32       
33       /**
34        * Using HSSF in POI library, this method will parse the Excel file, and
35        * extract all the values for every sheet, and combine them into a single
36        * String.
37        */

38       
39       
40       public void parse () {
41           files = new Vector JavaDoc();
42           Vector JavaDoc zipData = new Vector JavaDoc();
43           File JavaDoc fZip = new File JavaDoc(fileName);
44           zipData.add(new Long JavaDoc(fZip.lastModified()));
45           zipData.add(fileName);
46           zipData.add("");
47           zipData.add("zip");
48           zipData.add(fZip.getName());
49           zipData.add("");
50           files.add(zipData);
51           try{
52           ZipFile JavaDoc zip = new ZipFile JavaDoc(fileName);
53           
54           Enumeration JavaDoc entries = zip.entries();
55           while(entries.hasMoreElements()) {
56       
57             String JavaDoc file = null;
58             ZipEntry JavaDoc entry = (ZipEntry JavaDoc)entries.nextElement();
59             
60             try{
61                 file = unZip(zip, entry);
62             }catch(Exception JavaDoc e){
63                 String JavaDoc strTempFile = (entry.getName()).substring(entry.getName().lastIndexOf("/")+1);
64                 Vector JavaDoc d = new Vector JavaDoc();
65                 d.add(null);
66                 String JavaDoc temp = fileName+"("+strTempFile+")";
67                 d.add(temp);
68                 d.add(null);
69                 d.add(null);
70                 d.add(null);
71                 d.add(null);
72                 files.add(d.clone());
73                 try{
74                 File JavaDoc tempF = new File JavaDoc(dest + strTempFile);
75                 tempF.delete();
76                 }catch(Exception JavaDoc ex){}
77                 continue;
78             }
79             
80             if("".equals(file))
81               continue;
82             
83             if (org.enhydra.snapper.utils.Utils.isZIP(file)){
84                 parse(file);
85                 continue;
86              }
87              Parser parser = new Parser();
88              File JavaDoc tempFile = new File JavaDoc(file);
89              Vector JavaDoc data = (Vector JavaDoc) parser.parse(tempFile).clone();
90              parser = null;
91                          
92              boolean parsedWell = true;
93              
94              for (int i = 0; i < data.size(); i++) {
95                 if (data.elementAt(i) == null) {
96                     parsedWell = false;
97                     try {
98                         String JavaDoc temp = fileName+"("+tempFile.getName()+")";
99                         data.setElementAt(temp,1);
100                         } catch (Exception JavaDoc ex) {
101                     }
102            }
103
104             }
105              
106              tempFile.delete();
107              tempFile = null;
108              
109              if(parsedWell)
110              data.setElementAt(fileName,1);
111              
112              
113              
114              files.add(data.clone());
115              data.removeAllElements();
116                   
117           }
118           zip.close();
119           zip = null;
120           
121           }
122           catch(Exception JavaDoc e){
123               ParserManager.logger.error("Error while extracting and parsing ZIP file : "+fileName);
124           }
125           
126         }
127
128     public Vector JavaDoc getFiles() {
129         return files;
130     }
131       /**
132        * @param zip
133        * @param entries
134        * @throws IOException
135        * @throws FileNotFoundException
136        */

137     //private String unZip(ZipFile zip, Enumeration entries) throws IOException, FileNotFoundException {
138
private String JavaDoc unZip(ZipFile JavaDoc zip, ZipEntry JavaDoc e) throws IOException JavaDoc, FileNotFoundException JavaDoc {
139     //ZipEntry e = (ZipEntry)entries.nextElement();
140

141          String JavaDoc strTempFile = (e.getName()).substring(e.getName().lastIndexOf("/")+1);
142
143          if("".equals(strTempFile))
144           return "";
145          
146          File JavaDoc f = new File JavaDoc(dest + strTempFile);
147          //f.setLastModified(e.getTime());
148
time = e.getTime();
149 // if(e.isDirectory()) {
150
// if(!f.exists() && !f.mkdirs())
151
// throw new IOException("Couldn't create dir " + f);
152
// }
153
// else {
154
if(!e.isDirectory()) {
155             BufferedInputStream JavaDoc is = null;
156             BufferedOutputStream JavaDoc os = null;
157             try {
158                is = new BufferedInputStream JavaDoc(zip.getInputStream(e));
159                File JavaDoc destDir = f.getParentFile();
160                if(!destDir.exists() && !destDir.mkdirs())
161                   throw new IOException JavaDoc("Couldn't create dir " + destDir);
162                os = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(f));
163                int b = -1;
164                while((b = is.read()) != -1)
165                   os.write(b);
166             }
167             finally {
168                if(is != null)
169                   is.close();
170                if(os != null)
171                   os.close();
172             }
173          }
174          return dest + strTempFile;
175     }
176
177     public void setFileName(String JavaDoc fileName) {
178         this.fileName = fileName;
179       }
180
181       public String JavaDoc getParsedText() {
182         return parsedText;
183       }
184       
185       public String JavaDoc getTitle() {
186         return title;
187       }
188       
189       private void parse( String JavaDoc zipName){
190           try{
191           ZipFile JavaDoc zip = new ZipFile JavaDoc(zipName);
192           Enumeration JavaDoc entries = zip.entries();
193           while(entries.hasMoreElements()) {
194              
195             //String file = unZip(zip, entries);
196

197             String JavaDoc file = null;
198             
199             ZipEntry JavaDoc entry = (ZipEntry JavaDoc)entries.nextElement();
200             
201             try{
202                 file = unZip(zip, entry);
203             }catch(Exception JavaDoc e){
204                 String JavaDoc strTempFile = (entry.getName()).substring(entry.getName().lastIndexOf("/")+1);
205                 Vector JavaDoc d = new Vector JavaDoc();
206                 d.add(null);
207                 String JavaDoc temp = fileName+"("+strTempFile+")";
208                 d.add(temp);
209                 d.add(null);
210                 d.add(null);
211                 d.add(null);
212                 d.add(null);
213                 files.add(d.clone());
214                 try{
215                     File JavaDoc tempF = new File JavaDoc(dest + strTempFile);
216                     tempF.delete();
217                     }catch(Exception JavaDoc ex){}
218                 continue;
219             }
220             
221             if("".equals(file))
222                   continue;
223             
224             if (org.enhydra.snapper.utils.Utils.isZIP(file)){
225                 parse(file);
226              }
227              Parser parser = new Parser();
228              File JavaDoc tempFile = new File JavaDoc(file);
229              tempFile.setLastModified(time);
230              Vector JavaDoc data = (Vector JavaDoc) parser.parse(tempFile).clone();
231              parser = null;
232              tempFile.delete();
233              tempFile = null;
234              data.setElementAt(fileName,1);
235              files.add(data.clone());
236              data.removeAllElements();
237           }
238           zip.close();
239           File JavaDoc tempFile = new File JavaDoc(zipName);
240           tempFile.delete();
241           tempFile = null;
242           }
243           catch(Exception JavaDoc e) {
244               ParserManager.logger.error("Error while extracting and parsing ZIP file");
245           }
246       }
247           
248         
249       
250       
251       
252       
253     }
Popular Tags