1 package org.enhydra.snapper.parsers; 2 3 4 import java.io.BufferedInputStream ; 5 import java.io.BufferedOutputStream ; 6 import java.io.File ; 7 import java.io.FileNotFoundException ; 8 import java.io.FileOutputStream ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.util.Enumeration ; 12 import java.util.Vector ; 13 import java.util.zip.ZipEntry ; 14 import java.util.zip.ZipFile ; 15 16 17 public class ZIPParser implements org.enhydra.snapper.api.Parser { 18 private String fileName; 19 private String parsedText; 20 private String title = ""; 21 private String dest = ""; 22 StringBuffer sb = new StringBuffer (); 23 Vector files; 24 private long time; 25 26 public String parse(InputStream is) throws java.io.IOException { return "";} 27 28 public ZIPParser(String dest){ 29 this.dest = dest; 30 31 } 32 33 38 39 40 public void parse () { 41 files = new Vector (); 42 Vector zipData = new Vector (); 43 File fZip = new File (fileName); 44 zipData.add(new Long (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 zip = new ZipFile (fileName); 53 54 Enumeration entries = zip.entries(); 55 while(entries.hasMoreElements()) { 56 57 String file = null; 58 ZipEntry entry = (ZipEntry )entries.nextElement(); 59 60 try{ 61 file = unZip(zip, entry); 62 }catch(Exception e){ 63 String strTempFile = (entry.getName()).substring(entry.getName().lastIndexOf("/")+1); 64 Vector d = new Vector (); 65 d.add(null); 66 String 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 tempF = new File (dest + strTempFile); 75 tempF.delete(); 76 }catch(Exception 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 tempFile = new File (file); 89 Vector data = (Vector ) 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 temp = fileName+"("+tempFile.getName()+")"; 99 data.setElementAt(temp,1); 100 } catch (Exception 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 e){ 123 ParserManager.logger.error("Error while extracting and parsing ZIP file : "+fileName); 124 } 125 126 } 127 128 public Vector getFiles() { 129 return files; 130 } 131 137 private String unZip(ZipFile zip, ZipEntry e) throws IOException , FileNotFoundException { 139 141 String strTempFile = (e.getName()).substring(e.getName().lastIndexOf("/")+1); 142 143 if("".equals(strTempFile)) 144 return ""; 145 146 File f = new File (dest + strTempFile); 147 time = e.getTime(); 149 if(!e.isDirectory()) { 155 BufferedInputStream is = null; 156 BufferedOutputStream os = null; 157 try { 158 is = new BufferedInputStream (zip.getInputStream(e)); 159 File destDir = f.getParentFile(); 160 if(!destDir.exists() && !destDir.mkdirs()) 161 throw new IOException ("Couldn't create dir " + destDir); 162 os = new BufferedOutputStream (new FileOutputStream (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 fileName) { 178 this.fileName = fileName; 179 } 180 181 public String getParsedText() { 182 return parsedText; 183 } 184 185 public String getTitle() { 186 return title; 187 } 188 189 private void parse( String zipName){ 190 try{ 191 ZipFile zip = new ZipFile (zipName); 192 Enumeration entries = zip.entries(); 193 while(entries.hasMoreElements()) { 194 195 197 String file = null; 198 199 ZipEntry entry = (ZipEntry )entries.nextElement(); 200 201 try{ 202 file = unZip(zip, entry); 203 }catch(Exception e){ 204 String strTempFile = (entry.getName()).substring(entry.getName().lastIndexOf("/")+1); 205 Vector d = new Vector (); 206 d.add(null); 207 String 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 tempF = new File (dest + strTempFile); 216 tempF.delete(); 217 }catch(Exception 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 tempFile = new File (file); 229 tempFile.setLastModified(time); 230 Vector data = (Vector ) 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 tempFile = new File (zipName); 240 tempFile.delete(); 241 tempFile = null; 242 } 243 catch(Exception e) { 244 ParserManager.logger.error("Error while extracting and parsing ZIP file"); 245 } 246 } 247 248 249 250 251 252 253 } | Popular Tags |