1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package installer;
23 
24 import java.io.*;
25 import java.util.*;
26 import java.util.zip.*;
27 import java.awt.*;
28 import java.awt.event.*;
29 import javax.swing.*;
30 
31 public class ZipData
32 {
ZipData(String file)33     public ZipData(String file) {
34     }
35 
extractEntry(String entry, String destination, JLabel statusLabel)36     public boolean extractEntry(String entry, String destination,
37         JLabel statusLabel) {
38 
39         OutputStream out = null;
40         InputStream in = null;
41 
42         System.out.println("Copying: " + entry);
43         System.out.println("To: " + destination);
44 
45 	if (statusLabel != null) {
46 		statusLabel.setText("Copying " + entry);
47 	}
48 
49         String entryName;
50         if (entry.lastIndexOf("/") != -1) {
51             entryName = entry.substring(entry.lastIndexOf("/") + 1);
52         }
53         else {
54             entryName = entry;
55         }
56 
57         String destName;
58         if (destination.lastIndexOf(File.separator) != -1) {
59             destName = destination.substring(destination.lastIndexOf(File.separator) + 1);
60         }
61         else {
62             destName = destination;
63         }
64 
65         if (!destName.equals(entryName))
66             destination = destination.concat(entryName);
67 
68         System.out.println("Unzipping " + entry + " to " + destination);
69 
70 	try {
71             out = new FileOutputStream(destination);
72         }
73         catch (IOException ioe) {
74             System.err.println("Error opening " + destination +
75                 ": " + ioe.getMessage());
76 
77             if (statusLabel != null)
78                 statusLabel.setText("Error opening" + destination +
79                     "see SFramework.log for more information");
80 
81             return false;
82         }
83 
84         if (entry.startsWith("/") == false)
85             entry = "/" + entry;
86 
87         in = this.getClass().getResourceAsStream(entry);
88         if (in == null) {
89             System.err.println("File " + entry + " not found in jar file");
90 
91             if (statusLabel != null)
92                 statusLabel.setText("Failed extracting " + entry +
93                     "see SFramework.log for more information");
94 
95             return false;
96         }
97 
98         try {
99             byte[] bytes = new byte[1024];
100             int len;
101 
102             while ((len = in.read(bytes)) != -1)
103                 out.write(bytes, 0, len);
104         }
105         catch (IOException ioe) {
106             System.err.println("Error writing " + destination + ": " +
107                 ioe.getMessage());
108 
109             if (statusLabel != null)
110                 statusLabel.setText("Failed writing " + destination +
111                     "see SFramework.log for more information");
112             return false;
113         }
114         finally {
115             try {
116                 in.close();
117                 out.close();
118             }
119             catch (IOException ioe) {
120             }
121         }
122         return true;
123     }
124 }
125