1 16 17 package org.apache.taglibs.gnat; 18 19 import org.apache.taglibs.gnat.util.FileUtil; 20 import javax.servlet.ServletContext ; 21 import javax.servlet.jsp.*; 22 import javax.servlet.jsp.tagext.*; 23 import java.io.*; 24 import java.util.zip.*; 25 import java.util.*; 26 27 public class gunzipTag extends TagSupport 28 { 29 private String bdir = ""; 33 private File baseDir; 34 private String dest; private File destF; private String src; private File source; private ServletContext ctx; private FileUtil fut; 41 42 private void setDestF(String dest){ 43 44 destF = fut.resolveFile(null, dest); 45 } 46 47 private void setSource(String src) { 48 49 source = fut.resolveFile(null, src); 50 } 51 52 public void setDest(String dest) { 53 54 this.dest = dest; 55 } 56 57 public void setSrc(String src) { 58 59 this.src = src; 60 61 } 62 63 public int doStartTag() throws JspException 64 { 65 ctx = pageContext.getServletContext(); 66 67 70 if (!bdir.equals("")) 71 { 72 baseDir = new File(src); 73 setDestF(dest); 76 return SKIP_BODY; 77 } 78 else 79 { 80 baseDir = new File(bdir); 81 setSource(src); 82 setDestF(dest); 85 86 return SKIP_BODY; 87 } 88 } 89 90 public int doEndTag() throws JspException 91 { 92 if (source == null) 93 { 94 throw new JspTagException("gunzip: No source file for gunzip specified."); 95 } 96 97 if (!source.exists()) 98 { 99 throw new JspTagException("gunzip: Source file "+src+" doesn't exist."); 100 } 101 102 if (source.isDirectory()) 103 { 104 throw new JspException("gunzip: Cannot expand a directory. "+src+" is a directory."); 105 } 106 107 if (destF == null) 108 { 109 destF = new File(source.getParent()); 110 } 111 112 if (destF.isDirectory()) 113 { 114 String sourceName = source.getName(); 115 int len = sourceName.length(); 116 if (len > 3 117 && ".gz".equalsIgnoreCase(sourceName.substring(len-3))) 118 { 119 destF = new File(destF, sourceName.substring(0, len-3)); 120 } 121 else 122 { 123 destF = new File(destF, sourceName); 124 } 125 } 126 127 if (source.lastModified() > destF.lastModified()) 128 { 129 ctx.log("gunzip: Expanding "+ source.getAbsolutePath() + " to " 130 + destF.getAbsolutePath()); 131 132 FileOutputStream out = null; 133 GZIPInputStream zIn = null; 134 try 135 { 136 out = new FileOutputStream(destF); 137 zIn = new GZIPInputStream(new FileInputStream(source)); 138 byte[] buffer = new byte[8 * 1024]; 139 int count = 0; 140 do 141 { 142 out.write(buffer, 0, count); 143 count = zIn.read(buffer, 0, buffer.length); 144 } 145 while (count != -1); 146 } 147 catch (IOException ioe) 148 { 149 String msg = "gunzip: Problem expanding gzip " + ioe.getMessage(); 150 throw new JspTagException(msg); 151 } 152 finally 153 { 154 if (out != null) 155 { 156 try 157 { 158 out.close(); 159 } 160 catch (IOException ioex) 161 { 162 ioex.printStackTrace(); 163 } 164 } 165 if (zIn != null) 166 { 167 try 168 { 169 zIn.close(); 170 } 171 catch (IOException ioex) 172 { 173 ioex.printStackTrace(); 174 } 175 } 176 } 177 } 178 return EVAL_PAGE; 179 } 180 181 public String getDest() { 182 return dest; 183 } 184 185 public String getSrc() { 186 return src; 187 } 188 } 189 | Popular Tags |