1 package webman.stager; 2 3 import java.util.*; 4 import java.io.*; 5 import com.ringlord.archive.*; 6 7 26 class SitePacker extends SiteDocumentsListing 27 { 28 29 30 private final static String INTERNAL_VERSION = "1.15"; 31 32 private static PrintStream sysout; 33 34 private final static int BUFSIZE = 2048; 35 36 private final static String FS = File.separator; 37 38 private String documentRoot; 39 40 private SiteDocumentsListing oldListing; 41 42 52 SitePacker(String docRoot, String configFile, String targetDir, SiteDocumentsListing documentListing, 53 boolean filterFiles) throws IOException 54 { 55 super(); 56 sysout = System.out; documentRoot = docRoot; 58 oldListing = documentListing; 59 SiteStructureConfigReader sscr = new SiteStructureConfigReader(configFile,targetDir); 60 sysout.println("SitePacker-Version: " + INTERNAL_VERSION); 61 try 62 { 63 processDirectory(".", sscr, filterFiles); 64 } 65 catch ( IOException e ) 66 { 67 if ( e.getMessage().endsWith(documentRoot + FS + "." + FS + " is no directory or cannot be read.") ) 68 { 69 throw new IOException("DocumentRoot " + documentRoot + " cannot be read. Check that it's really a readable directory!"); 70 } 71 } 72 } 73 74 83 public File zipSite(String filename, String tempDir, boolean onlyChangedDocuments) throws IOException 84 { 85 File f = new File(tempDir); 87 createDir(f); 88 if ( !tempDir.endsWith(FS) && !tempDir.endsWith("/") ) 90 { 94 tempDir = tempDir + FS; 95 } 96 if ( !documentRoot.endsWith(FS) && !tempDir.endsWith("/") ) { 98 documentRoot = documentRoot + FS; 99 } 100 String [] dirs = getDirectories(); 102 for ( int k = 0; k < dirs.length; k++ ) 103 { 104 createDir(new File(tempDir, dirs[k])); 106 String [] files; 109 if ( onlyChangedDocuments ) 110 { 111 files = getChangedAndNewDocumentNames(dirs[k], oldListing); 112 } 113 else 114 { 115 files = getDocumentNames(dirs[k]); 116 } 117 String aDir = dirs[k] + File.separator; 119 for ( int i = 0; i < files.length; i++ ) 120 { 121 String fname = aDir + files[i]; 122 copyFile(new File(documentRoot, fname), new File(tempDir, fname)); 124 } 125 } 126 127 Archive archive = null; 128 File archiveFile = new File(filename); 129 try 130 { 131 archive = new ArchiveFactory().create(archiveFile); 132 if ( archive == null ) 133 { 134 throw new IOException("archive could not be created."); 135 } 136 archive.addDir("",new File(tempDir), true); 137 archive.close(); 138 } 139 catch (Exception e) 140 { 141 if ( e.getMessage().indexOf("ZIP file must have at least one entry") > -1 ) 142 { 143 sysout.println("Note: zipfile contains no data."); 144 archiveFile.createNewFile(); } 146 else 147 { 148 sysout.println(e); 149 e.printStackTrace(); 150 } 151 } 152 return archiveFile; 153 } 154 155 163 void processDirectory(String subDir, SiteStructureConfigReader sscr, boolean filterFiles) throws IOException 164 { 165 String curDir = new File(documentRoot + FS + subDir + FS).getCanonicalPath() + FS; 166 sysout.println("site assembler: processing directory " + curDir + "<br>"); 168 String target = sscr.getTargetDirectory(subDir + FS); 169 String [] list = (new File(curDir)).list(); 171 if ( list == null ) 173 { 174 throw new IOException(curDir + " is no directory or cannot be read."); 175 } 176 for ( int i = 0; i < list.length; i++ ) 178 { 179 File f = new File(curDir + list[i]); 181 if ( f.isDirectory() ) 182 { 183 if ( !filterFiles || sscr.belongsToSite(subDir + FS + list[i] + FS) ) 185 { 186 processDirectory(subDir + FS + list[i], sscr, filterFiles); 188 } 189 } 190 else 191 { 192 if ( !filterFiles || sscr.belongsToSite(subDir + FS + list[i]) ) 194 { 195 addDocument(subDir, list[i], f.lastModified(), f.length(), target); 196 } 198 } 199 } 200 } 201 202 208 static void copyFile(File source, File target) throws IOException 209 { 210 if ( source.isDirectory() ) { 212 if ( !target.mkdir() ) 213 { 214 throw new IOException("could not create temporary directory " + target.getPath()); 215 } 216 return; } 218 FileInputStream fis = null; 219 FileOutputStream fos = null; 220 try 221 { 222 fis = new FileInputStream(source); 223 fos = new FileOutputStream(target); 224 byte[] buf = new byte[BUFSIZE]; 225 int read; 227 228 while ( (read = fis.read(buf, 0, BUFSIZE)) != -1) 229 { 230 fos.write(buf, 0, read); 231 } 232 fos.flush(); 233 } 234 catch (IOException e) 235 { 236 throw new IOException("unable to copy file " + source + " to " + target + " (" + e + ")"); 237 } 238 finally 239 { 240 if ( fis != null ) 241 { 242 fis.close(); 243 } 244 if ( fos != null ) 245 { 246 fos.close(); 247 } 248 } 249 } 250 251 256 static void createDir(File f) throws IOException 257 { 258 if ( !f.exists() ) 259 { 260 if ( !f.mkdirs() ) 261 { 262 throw new IOException("unable to create directory: " + f); 263 } 264 } 265 else 266 { 267 if ( !f.isDirectory() ) 269 { 270 throw new IOException("unable to create directory: " + f + " - a file with that name exist"); 271 } 272 } 273 } 274 } | Popular Tags |