KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > packaging > FileOperations


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Christophe Contreras
23 Contributor(s): ___________________________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.packaging;
28
29 /**
30  * This utils class is an helper for any File operations
31  * opening
32  *
33  * @author <a HREF="mailto:Christophe.Contreras@lifl.fr">Christophe Contreras</a>
34  *
35  */

36 public class FileOperations
37 {
38     /**
39      * File opening
40      *
41      * @param file The file to open
42      * @param frame The frame to send result to
43      */

44     public static void
45     open_file(
46         java.io.File JavaDoc file,
47         org.objectweb.apollon.gui.ApollonFrame frame)
48     {
49         if (! file.exists()
50             || ! file.isFile()
51             )
52         {
53             System.err.println("File : "+file.getName()+" : ignored");
54             return;
55         }
56         
57         if (file.getName().toLowerCase().endsWith("cad")
58             || file.getName().toLowerCase().endsWith("csd")
59             || file.getName().toLowerCase().endsWith("ccd")
60             || file.getName().toLowerCase().endsWith("cpf")
61             || file.getName().toLowerCase().endsWith("npd")
62             )
63         {
64             frame.open_file(file);
65         }
66         if (file.getName().toLowerCase().endsWith("aar")
67             || file.getName().toLowerCase().endsWith("car")
68             || file.getName().toLowerCase().endsWith("zip")
69             )
70         {
71             try
72             {
73                 // Open the ZIP file
74
java.util.zip.ZipInputStream JavaDoc in
75                  = new java.util.zip.ZipInputStream JavaDoc(
76                     new java.io.FileInputStream JavaDoc(file.getAbsolutePath())
77                     );
78                 // Go through the entries
79
java.util.zip.ZipEntry JavaDoc entry
80                  = in.getNextEntry();
81                 while (in.available() == 1)
82                 {
83                     java.io.File JavaDoc file_entry
84                      = new java.io.File JavaDoc(entry.getName());
85
86                     if (entry.isDirectory())
87                     {
88                         file_entry.mkdir();
89                     } else {
90                         if (entry.getName().indexOf("/")>-1)
91                         {
92                             java.io.File JavaDoc file_dir
93                              = new java.io.File JavaDoc(
94                                 entry.getName().substring(
95                                     0,
96                                     entry.getName().lastIndexOf("/")
97                                     )
98                                 );
99                             file_dir.mkdir();
100                         }
101                         // Opens an output file stream
102
java.io.OutputStream JavaDoc out
103                          = new java.io.FileOutputStream JavaDoc(file_entry);
104                         // Transfer bytes from ZIP to output file
105
byte[] buf = new byte[1024];
106                         int len;
107                         while ((len = in.read(buf)) > 0) {
108                             out.write(buf, 0, len);
109                         }
110                         // Close the file outut stream
111
out.close();
112                         // opens the file in the GUI
113
frame.open_file(new java.io.File JavaDoc(entry.getName()));
114                     }
115                     entry = in.getNextEntry();
116                 }
117                 in.close();
118             } catch (java.util.zip.ZipException JavaDoc exc) {
119                 System.err.println("*** ZIP archive file corrupted ***");
120                 exc.printStackTrace();
121             } catch (java.io.IOException JavaDoc exc) {
122                 System.err.println("*** error during file opening ***");
123                 exc.printStackTrace();
124             }
125         }
126     }
127 }
Popular Tags