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 gzipTag extends TagSupport 28 { 29 private String bdir = ""; 33 private File baseDir; 34 private String zipfile; 35 private File zf; 36 private String src; 37 private File source; 38 private ServletContext ctx; 39 private FileUtil fut; 40 41 42 private void setZF(String zipfile){ 43 44 zf = fut.resolveFile(null, zipfile); 45 } 46 47 private void setSource(String src) { 48 49 source = fut.resolveFile(null, src); 50 } 51 52 public void setZipfile(String zipfile) { 53 54 this.zipfile = zipfile; 55 } 56 57 public void setSrc(String src) { 58 59 this.src = src; 60 61 } 62 63 public int doStartTag() throws JspException { 64 ctx = pageContext.getServletContext(); 65 if (!bdir.equals("")) 66 { 67 baseDir = new File(src); 68 setZF(zipfile); 70 return SKIP_BODY; 71 } 72 else 73 { 74 baseDir = new File(bdir); 75 setSource(src); 76 setZF(zipfile); 78 return SKIP_BODY; 79 } 80 } 81 82 public int doEndTag() throws JspException { 83 if (zf == null) 84 { 85 throw new JspTagException("gzip: zipfile attribute is required"); 86 } 87 88 if (source == null) 89 { 90 throw new JspTagException("gzip: src attribute is required"); 91 } 92 ctx.log("gzip: Building gzip: " + zf.getAbsolutePath()); 93 94 GZIPOutputStream zOut = null; 95 try 96 { 97 zOut = new GZIPOutputStream(new FileOutputStream(zf)); 98 99 if (source.isDirectory()) 100 { 101 ctx.log ("gzip: Cannot Gzip a directory!"); 102 } 103 else 104 { 105 zipFile(source, zOut); 106 } 107 } 108 catch (IOException ioe) 109 { 110 String msg = "gzip: Problem gzipping file: " + ioe.getMessage(); 111 throw new JspTagException(msg); 112 } 113 finally 114 { 115 if (zOut != null) 116 { 117 try 118 { 119 zOut.close(); 120 } 121 catch (IOException e) 122 { 123 e.printStackTrace(); 124 } 125 } 126 } 127 return EVAL_PAGE; 128 } 129 130 private void zipFile(InputStream in, GZIPOutputStream zOut) 131 throws IOException 132 { 133 byte[] buffer = new byte[8 * 1024]; 134 int count = 0; 135 do 136 { 137 zOut.write(buffer, 0, count); 138 count = in.read(buffer, 0, buffer.length); 139 } 140 while (count != -1); 141 } 142 143 private void zipFile(File file, GZIPOutputStream zOut) 144 throws IOException 145 { 146 FileInputStream fIn = new FileInputStream(file); 147 try 148 { 149 zipFile(fIn, zOut); 150 } 151 finally 152 { 153 fIn.close(); 154 } 155 } 156 157 public String getZipfile() { 158 return zipfile; 159 } 160 161 public String getSrc() { 162 return src; 163 } 164 } 165 | Popular Tags |