KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > logger > Zipper


1 package org.netbeans.modules.logger;
2 /*
3  * Zipper.java
4  *
5  * Created on May 17, 2005, 2:44 PM
6  *
7  * To change this template, choose Tools | Options and locate the template under
8  * the Source Creation and Management node. Right-click the template and choose
9  * Open. You can then make changes to the template in the Source Editor.
10  */

11
12 /**
13  *
14  * @author loicsegapelli
15  */

16
17 import java.io.File JavaDoc;
18 import java.io.FileInputStream JavaDoc;
19 import java.io.FileNotFoundException JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.FilenameFilter JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.zip.ZipEntry JavaDoc;
26 import java.util.zip.ZipFile JavaDoc;
27 import java.util.zip.ZipOutputStream JavaDoc;
28 import org.netbeans.modules.logger.listeners.ListenerTools;
29
30
31
32 /**
33  * Zips log files as well as layout files located in <I>Windows2Local</I>
34  */

35 public class Zipper {
36     
37     
38     
39     /**
40      * Zips all log files located in <I>userdir</I>/var/log/ui, as well as layout
41      * files and directories inside <I>userdir</I>/config/Windows2Local/Modes/
42      */

43     public void zipUI(){
44         zipUIFiles("<--xxx-->");
45     }
46     
47     /**
48      * same as <CODE>zipUI</CODE> except we want to avoid files with some prefix.
49      * This method is used at launchtime so that we do not zip the log files
50      * currently in use during this session together with old log files. Old log
51      * files can be found at launchtime if the IDE crashed during previous session.
52      */

53     public void zipUIExceptCurrent() {
54         zipUIFiles(ListenerTools.TS);
55     }
56     
57     /**
58      * called by <CODE>zipUI</CODE> or <CODE>zipUIExceptCurrent</CODE>
59      * @param avoidPrefix a filename prefix (in our case a timestamp) that we should avoid
60      * zipping.
61      */

62     private void zipUIFiles(String JavaDoc avoidPrefix) {
63         try {
64             //take log files
65
File JavaDoc [] files = getDirContent(ListenerTools.LOG_PATH, ".log", avoidPrefix);
66             if(files==null||files.length==0){
67                 return;
68             }
69             // a log file is: ts.level.name.kind.log
70
int dotIndex = files[0].getName().lastIndexOf(".");
71             String JavaDoc withoutLog = files[0].getName().substring(0, dotIndex);
72             // now we have: ts.level.name.kind
73
dotIndex = withoutLog.lastIndexOf(".");
74             // id is: ts.level.name
75
String JavaDoc id = files[0].getName().substring(0, dotIndex);
76             // finally, id is: path/ts.level.name.session.zip
77
id = ListenerTools.LOG_PATH + id + ".session.zip";
78             ZipOutputStream JavaDoc zos = new ZipOutputStream JavaDoc(new FileOutputStream JavaDoc(id));
79             zipRecursively(files, zos, true);
80             zos.close();
81             
82             
83             //now add the new session.zip into user.zip
84
sessionZip();
85         } catch(IOException JavaDoc e) {
86             ListenerTools.logError(e);
87         }
88     }
89     
90     /**
91      * finds the window layout files and folders, zips them and put them into the
92      * bigger zip file (<I>.user</I> file). Please note we want to have <B>only one</B>
93      * layout archive per <I>.user</I> file.
94      * @param zos the <I>.user</I> file being created.
95      */

96     private void zipWindows(ZipOutputStream JavaDoc zos) {
97         try {
98             File JavaDoc [] files = getDirContent(ListenerTools.LAYOUT_PATH);
99             if(files==null||files.length==0){
100                 return;
101             }
102             
103             // now this bunch of code is aiming at retrieving
104
// WindowManager.wswmgr which for some reason contains the editor
105
// layout and is of course not located in the same directory...
106
File JavaDoc f = new File JavaDoc(ListenerTools.LAYOUT_PATH);
107             File JavaDoc g = f.getParentFile();
108             File JavaDoc wm = new File JavaDoc(g.getAbsolutePath() + ListenerTools.SEP + "WindowManager.wswmgr");
109             
110             
111             File JavaDoc layout = new File JavaDoc(ListenerTools.LOG_PATH+"layout.zip");
112             ZipOutputStream JavaDoc zos2 = new ZipOutputStream JavaDoc(new FileOutputStream JavaDoc(layout));
113             zipRecursively(files, zos2, false);
114             if(wm.exists()){
115                 zipRecursively(new File JavaDoc [] {wm}, zos2, false);
116             }
117             try{
118                 zos2.close();
119             } catch(IOException JavaDoc e){
120                 ListenerTools.logError(e);
121             }
122             ZipEntry JavaDoc entry = new ZipEntry JavaDoc("layout.zip");
123             try{
124                 zos.putNextEntry(entry);
125             }catch(java.util.zip.ZipException JavaDoc e){
126                 ListenerTools.logError(e);
127             }
128             int bytesIn = 0;
129             byte[] readBuffer = new byte[2156];
130             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(layout);
131             while((bytesIn = fis.read(readBuffer)) != -1) {
132                 zos.write(readBuffer, 0, bytesIn);
133             }
134             layout.delete();
135         } catch(IOException JavaDoc e) {
136             ListenerTools.logError(e);
137         }
138     }
139     
140     
141     /**
142      * explores and returns the content of a given directory according to some
143      * constaints given as parameters.
144      * @param path the directory path
145      * @param suffix we want files having this suffix
146      * @param avoidPrefix we should not retain files with this prefix
147      * @return array of filenames
148      */

149     private File JavaDoc [] getDirContent(String JavaDoc path, String JavaDoc suffix, String JavaDoc avoidPrefix) {
150         final String JavaDoc suf = suffix;
151         final String JavaDoc pre = avoidPrefix;
152         File JavaDoc dir = new File JavaDoc(path);
153         FilenameFilter JavaDoc filter = new FilenameFilter JavaDoc() {
154             public boolean accept(File JavaDoc dir, String JavaDoc name) {
155                 return (!name.startsWith(pre))&&name.endsWith(suf);
156             }
157         };
158         return dir.listFiles(filter);
159     }
160     
161     /**
162      * explores and returns the content of a given directory according to some
163      * constaints given as parameters.
164      * @param path the directory path
165      * @param suffix we want files with this suffix
166      * @return array of filenames
167      */

168     private File JavaDoc [] getDirContent(String JavaDoc path, String JavaDoc suffix) {
169         return getDirContent(path, suffix, "<--xxx-->");
170     }
171     
172     /**
173      * returns the content of a given directory
174      * @param path path of the directory
175      * @return array of filenames
176      */

177     private File JavaDoc [] getDirContent(String JavaDoc path) {
178         return getDirContent(path, "", "<--xxx-->");
179     }
180     
181     /**
182      * Given an array of filenames, zips all its content; recursively zips inner
183      * directories
184      * @param files array of filenames
185      * @param theirPath absolute path to the array of filenames
186      * @param zos stream into which we will put zip entires
187      * @param delete shall we delete the original file? Yes if it is one of our <I>.log</I>
188      * files, no if it is a NB window layout file
189      */

190     private void zipRecursively(File JavaDoc [] files, ZipOutputStream JavaDoc zos, boolean delete) {
191         int bytesIn = 0;
192         boolean recursive = false;
193         byte[] readBuffer = new byte[2156];
194         
195         // loop through all dir content
196
for(int i = 0; i<files.length; i++) {
197             recursive = false;
198             // if directory: recursively zip
199
if(files[i].isDirectory()){
200                 File JavaDoc files2[] = getDirContent(files[i].getAbsolutePath());
201                 if(files2.length!=0){
202                     try{
203                         File JavaDoc zip = new File JavaDoc(files[i].getAbsolutePath()+".zip");
204                         ZipOutputStream JavaDoc zos2 = new ZipOutputStream JavaDoc(new FileOutputStream JavaDoc(zip));
205                         zipRecursively(files2, zos2, delete);
206                         zos2.close();
207                         // instead of pointing to a dir, it now points to a zip
208
files[i] = zip;
209                         recursive = true;
210                     } catch(IOException JavaDoc e){
211                         ListenerTools.logError(e);
212                     }
213                 }else continue;
214             }
215             
216             // add file content into zip output
217
FileInputStream JavaDoc fis;
218             try{
219                 fis = new FileInputStream JavaDoc(files[i]);
220             } catch(FileNotFoundException JavaDoc e){
221                 ListenerTools.logError(e);
222                 continue;
223             }
224             ZipEntry JavaDoc entry = new ZipEntry JavaDoc(files[i].getName());
225             
226             try{
227                 zos.putNextEntry(entry);
228             }catch(java.util.zip.ZipException JavaDoc e){
229                 //this exception is probably due to a duplicate entry; do not log
230
if(delete || recursive)files[i].delete();
231                 continue;
232             }catch(IOException JavaDoc 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 JavaDoc e){
242                 ListenerTools.logError(e);
243             }
244             
245             if(delete || recursive) files[i].delete();
246         }
247     }
248     
249     /**
250      * takes all <I>.session</I> files and zip them altogether into the <I>.user</I>
251      * file
252      */

253     private void sessionZip() {
254         try{
255             // is there any session file to add?
256
File JavaDoc [] sessionFiles = getDirContent(ListenerTools.LOG_PATH, ".session.zip");
257             if (sessionFiles.length==0)return;
258             
259             // open a zip output thing
260
int bytesIn = 0;
261             byte[] readBuffer = new byte[2156];
262             
263             File JavaDoc [] userFile = getDirContent(ListenerTools.LOG_PATH, ".user");
264             
265             File JavaDoc temp = new File JavaDoc("temporary.zip");
266             ZipOutputStream JavaDoc zos = new ZipOutputStream JavaDoc(new FileOutputStream JavaDoc(temp));
267             
268             
269             zipWindows(zos);
270             
271             String JavaDoc id;
272             
273             // copy existing content into other zip
274
if(userFile.length>0){
275                 id = userFile[0].getAbsolutePath();
276                 ZipFile JavaDoc existing = new ZipFile JavaDoc(id);
277                 ZipEntry JavaDoc entry;
278                 Enumeration JavaDoc e = existing.entries();
279                 while(e.hasMoreElements()){
280                     entry = (ZipEntry JavaDoc)e.nextElement();
281                     //ther should be only one layout zip file: the most recent
282
if(entry.getName().equals("layout.zip"))continue;
283                     InputStream JavaDoc 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 JavaDoc old = new File JavaDoc(ListenerTools.LOG_PATH+userFile[0]);
291                 old.delete();
292             } else {
293                 // session file is: ts.level.name.session.zip
294
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             //now put the new session files
300
zipRecursively(sessionFiles, zos, true);
301             zos.close();
302             
303             File JavaDoc out = new File JavaDoc(id);
304             temp.renameTo(out);
305         } catch(IOException JavaDoc e){
306             ListenerTools.logError(e);
307         }
308     }
309     
310 }
311
Popular Tags