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