1 4 package com.openedit.util; 5 6 import java.io.File ; 7 import java.io.FileInputStream ; 8 import java.io.FileOutputStream ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.io.OutputStream ; 12 import java.util.ArrayList ; 13 import java.util.Date ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.zip.ZipEntry ; 17 import java.util.zip.ZipInputStream ; 18 import java.util.zip.ZipOutputStream ; 19 20 import com.openedit.OpenEditException; 21 22 23 public class ZipUtil 24 { 25 protected List fieldExcludes; 26 protected OutputFiller filler = new OutputFiller(); 27 protected String fieldFindFileName; 28 protected boolean fieldExitOnFirstFind; 29 protected File fieldRoot; 30 31 protected List getExcludes() 32 { 33 if (fieldExcludes == null) 34 { 35 fieldExcludes = new ArrayList (); 36 } 37 38 return fieldExcludes; 39 } 40 public void addExclude(String inPath) 41 { 42 getExcludes().add(inPath); 44 } 45 public void unzip(String inPage, String inDest) throws Exception 46 { 47 unzip(new File ( inPage), new File ( inDest )); 48 } 49 public void unzip(File inPage, File inDest) throws IOException 50 { 51 InputStream in = null; 52 try 53 { 54 in = new FileInputStream (inPage); 55 ZipInputStream unzip = new ZipInputStream (in); 56 ZipEntry entry = unzip.getNextEntry(); 57 58 while( entry != null) 59 { 60 if( fieldFindFileName != null ) 61 { 62 if( ! PathUtilities.match(entry.getName(),fieldFindFileName)) { 64 entry = unzip.getNextEntry(); 65 continue; 66 } 67 } 68 69 if( !entry.isDirectory() ) 71 { 72 File ufile = new File ( inDest, entry.getName()); 73 ufile.getParentFile().mkdirs(); 74 FileOutputStream tout = new FileOutputStream (ufile); 75 try 76 { 77 filler.fill(unzip,tout); 78 } 79 finally 80 { 81 FileUtils.safeClose(tout); 82 } 83 ufile.setLastModified(entry.getTime()); 84 if( isExitOnFirstFind() ) 85 { 86 return; 87 } 88 } 89 entry = unzip.getNextEntry(); 90 } 91 } 92 finally 93 { 94 FileUtils.safeClose(in); 95 } 96 } 97 103 130 public void addTozip( String inContent, String inName, ZipOutputStream finalZip ) throws IOException 131 { 132 ZipEntry entry = new ZipEntry ( inName ); 133 entry.setSize( inContent.length() ); 134 entry.setTime( new Date ().getTime() ); 135 finalZip.putNextEntry( entry ); 136 finalZip.write(inContent.getBytes("UTF-8")); 137 finalZip.closeEntry(); 138 139 } 140 141 public void zipFile( String inOpenEditPath, OutputStream inToBrowser ) throws IOException , OpenEditException 142 { 143 ZipOutputStream finalZip = new ZipOutputStream (inToBrowser); 144 147 159 File file = new File ( getRoot(), inOpenEditPath); 160 addToZip(file, finalZip); 161 162 finalZip.close(); 163 } 164 protected void addToZip(File inPath,ZipOutputStream inFinalZip) throws IOException , OpenEditException 165 { 166 167 String zipname = inPath.getAbsolutePath().substring(getRoot().getAbsolutePath().length()); 169 170 String openeditpath = zipname.replace('\\', '/'); 172 while (zipname.startsWith("/")) 175 { 176 zipname = zipname.substring(1, zipname.length()); 177 } 178 179 if( inPath.isDirectory() && !openeditpath.endsWith("/")) 180 { 181 openeditpath = openeditpath + "/"; } 183 184 if( !isValidEntry( openeditpath )) 186 { 187 return; 188 } 189 190 if( inPath.isDirectory()) 192 { 193 File [] files = inPath.listFiles(); 194 if ( files != null) 195 { 196 for (int i = 0; i < files.length; i++) 197 { 198 addToZip(files[i],inFinalZip); 199 } 200 } 201 } 202 else 203 { 204 ZipEntry entry = new ZipEntry ( zipname ); 205 entry.setSize( inPath.length() ); 206 entry.setTime( inPath.lastModified() ); 207 FileInputStream fis = new FileInputStream ( inPath ); 208 inFinalZip.putNextEntry( entry ); 209 try 210 { 211 new OutputFiller().fill( fis, inFinalZip ); 212 } 213 finally 214 { 215 fis.close(); 216 } 217 inFinalZip.closeEntry(); 218 } 219 } 220 protected boolean isValidEntry(String inPath) throws OpenEditException 221 { 222 if( exclude( inPath )) 223 { 224 return false; 225 } 226 return true; 227 } 228 protected boolean exclude(String inOpenEditPath) 229 { 230 if( fieldExcludes != null) 231 { 232 for (Iterator iter = getExcludes().iterator(); iter.hasNext();) 233 { 234 String wild = (String )iter.next(); 235 if ( PathUtilities.match(inOpenEditPath, wild ) ) 236 { 237 return true; 238 } 239 } 240 } 241 return false; 242 } 243 public String getFindFileName() 244 { 245 return fieldFindFileName; 246 } 247 public void setFindFileName(String inFindFileName) 248 { 249 fieldFindFileName = inFindFileName; 250 } 251 public boolean isExitOnFirstFind() 252 { 253 return fieldExitOnFirstFind; 254 } 255 public void setExitOnFirstFind(boolean inExitOnFirstFind) 256 { 257 fieldExitOnFirstFind = inExitOnFirstFind; 258 } 259 public File getRoot() 260 { 261 return fieldRoot; 262 } 263 public void setRoot(File inRoot) 264 { 265 fieldRoot = inRoot; 266 } 267 } | Popular Tags |