1 package org.netbeans.modules.logger; 2 11 12 16 17 import java.io.File ; 18 import java.io.FileInputStream ; 19 import java.io.FileNotFoundException ; 20 import java.io.FileOutputStream ; 21 import java.io.FilenameFilter ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.Enumeration ; 25 import java.util.zip.ZipEntry ; 26 import java.util.zip.ZipFile ; 27 import java.util.zip.ZipOutputStream ; 28 import org.netbeans.modules.logger.listeners.ListenerTools; 29 30 31 32 35 public class Zipper { 36 37 38 39 43 public void zipUI(){ 44 zipUIFiles("<--xxx-->"); 45 } 46 47 53 public void zipUIExceptCurrent() { 54 zipUIFiles(ListenerTools.TS); 55 } 56 57 62 private void zipUIFiles(String avoidPrefix) { 63 try { 64 File [] files = getDirContent(ListenerTools.LOG_PATH, ".log", avoidPrefix); 66 if(files==null||files.length==0){ 67 return; 68 } 69 int dotIndex = files[0].getName().lastIndexOf("."); 71 String withoutLog = files[0].getName().substring(0, dotIndex); 72 dotIndex = withoutLog.lastIndexOf("."); 74 String id = files[0].getName().substring(0, dotIndex); 76 id = ListenerTools.LOG_PATH + id + ".session.zip"; 78 ZipOutputStream zos = new ZipOutputStream (new FileOutputStream (id)); 79 zipRecursively(files, zos, true); 80 zos.close(); 81 82 83 sessionZip(); 85 } catch(IOException e) { 86 ListenerTools.logError(e); 87 } 88 } 89 90 96 private void zipWindows(ZipOutputStream zos) { 97 try { 98 File [] files = getDirContent(ListenerTools.LAYOUT_PATH); 99 if(files==null||files.length==0){ 100 return; 101 } 102 103 File f = new File (ListenerTools.LAYOUT_PATH); 107 File g = f.getParentFile(); 108 File wm = new File (g.getAbsolutePath() + ListenerTools.SEP + "WindowManager.wswmgr"); 109 110 111 File layout = new File (ListenerTools.LOG_PATH+"layout.zip"); 112 ZipOutputStream zos2 = new ZipOutputStream (new FileOutputStream (layout)); 113 zipRecursively(files, zos2, false); 114 if(wm.exists()){ 115 zipRecursively(new File [] {wm}, zos2, false); 116 } 117 try{ 118 zos2.close(); 119 } catch(IOException e){ 120 ListenerTools.logError(e); 121 } 122 ZipEntry entry = new ZipEntry ("layout.zip"); 123 try{ 124 zos.putNextEntry(entry); 125 }catch(java.util.zip.ZipException e){ 126 ListenerTools.logError(e); 127 } 128 int bytesIn = 0; 129 byte[] readBuffer = new byte[2156]; 130 FileInputStream fis = new FileInputStream (layout); 131 while((bytesIn = fis.read(readBuffer)) != -1) { 132 zos.write(readBuffer, 0, bytesIn); 133 } 134 layout.delete(); 135 } catch(IOException e) { 136 ListenerTools.logError(e); 137 } 138 } 139 140 141 149 private File [] getDirContent(String path, String suffix, String avoidPrefix) { 150 final String suf = suffix; 151 final String pre = avoidPrefix; 152 File dir = new File (path); 153 FilenameFilter filter = new FilenameFilter () { 154 public boolean accept(File dir, String name) { 155 return (!name.startsWith(pre))&&name.endsWith(suf); 156 } 157 }; 158 return dir.listFiles(filter); 159 } 160 161 168 private File [] getDirContent(String path, String suffix) { 169 return getDirContent(path, suffix, "<--xxx-->"); 170 } 171 172 177 private File [] getDirContent(String path) { 178 return getDirContent(path, "", "<--xxx-->"); 179 } 180 181 190 private void zipRecursively(File [] files, ZipOutputStream zos, boolean delete) { 191 int bytesIn = 0; 192 boolean recursive = false; 193 byte[] readBuffer = new byte[2156]; 194 195 for(int i = 0; i<files.length; i++) { 197 recursive = false; 198 if(files[i].isDirectory()){ 200 File files2[] = getDirContent(files[i].getAbsolutePath()); 201 if(files2.length!=0){ 202 try{ 203 File zip = new File (files[i].getAbsolutePath()+".zip"); 204 ZipOutputStream zos2 = new ZipOutputStream (new FileOutputStream (zip)); 205 zipRecursively(files2, zos2, delete); 206 zos2.close(); 207 files[i] = zip; 209 recursive = true; 210 } catch(IOException e){ 211 ListenerTools.logError(e); 212 } 213 }else continue; 214 } 215 216 FileInputStream fis; 218 try{ 219 fis = new FileInputStream (files[i]); 220 } catch(FileNotFoundException e){ 221 ListenerTools.logError(e); 222 continue; 223 } 224 ZipEntry entry = new ZipEntry (files[i].getName()); 225 226 try{ 227 zos.putNextEntry(entry); 228 }catch(java.util.zip.ZipException e){ 229 if(delete || recursive)files[i].delete(); 231 continue; 232 }catch(IOException e){ 233 ListenerTools.logError(e); 234 } 235 236 try{ 237 while((bytesIn = fis.read(readBuffer))!=-1) { 238 zos.write(readBuffer, 0, bytesIn); 239 } 240 fis.close(); 241 }catch(IOException e){ 242 ListenerTools.logError(e); 243 } 244 245 if(delete || recursive) files[i].delete(); 246 } 247 } 248 249 253 private void sessionZip() { 254 try{ 255 File [] sessionFiles = getDirContent(ListenerTools.LOG_PATH, ".session.zip"); 257 if (sessionFiles.length==0)return; 258 259 int bytesIn = 0; 261 byte[] readBuffer = new byte[2156]; 262 263 File [] userFile = getDirContent(ListenerTools.LOG_PATH, ".user"); 264 265 File temp = new File ("temporary.zip"); 266 ZipOutputStream zos = new ZipOutputStream (new FileOutputStream (temp)); 267 268 269 zipWindows(zos); 270 271 String id; 272 273 if(userFile.length>0){ 275 id = userFile[0].getAbsolutePath(); 276 ZipFile existing = new ZipFile (id); 277 ZipEntry entry; 278 Enumeration e = existing.entries(); 279 while(e.hasMoreElements()){ 280 entry = (ZipEntry )e.nextElement(); 281 if(entry.getName().equals("layout.zip"))continue; 283 InputStream input = existing.getInputStream(entry); 284 zos.putNextEntry(entry); 285 while((bytesIn = input.read(readBuffer)) != -1) { 286 zos.write(readBuffer, 0, bytesIn); 287 } 288 input.close(); 289 } 290 File old = new File (ListenerTools.LOG_PATH+userFile[0]); 291 old.delete(); 292 } else { 293 int dotIndex = sessionFiles[0].getName().lastIndexOf(".session.zip"); 295 int firstDotIndex = sessionFiles[0].getName().indexOf("."); 296 id = ListenerTools.LOG_PATH + sessionFiles[0].getName().substring(firstDotIndex+1, dotIndex) + ".user"; 297 } 298 299 zipRecursively(sessionFiles, zos, true); 301 zos.close(); 302 303 File out = new File (id); 304 temp.renameTo(out); 305 } catch(IOException e){ 306 ListenerTools.logError(e); 307 } 308 } 309 310 } 311 | Popular Tags |