1*cdf0e10cSrcweir package installer;
2*cdf0e10cSrcweir 
3*cdf0e10cSrcweir import java.io.*;
4*cdf0e10cSrcweir import java.util.*;
5*cdf0e10cSrcweir import java.util.zip.*;
6*cdf0e10cSrcweir import java.awt.*;
7*cdf0e10cSrcweir import java.awt.event.*;
8*cdf0e10cSrcweir import javax.swing.*;
9*cdf0e10cSrcweir 
10*cdf0e10cSrcweir public class ZipData
11*cdf0e10cSrcweir {
12*cdf0e10cSrcweir     public ZipData(String file) {
13*cdf0e10cSrcweir     }
14*cdf0e10cSrcweir 
15*cdf0e10cSrcweir     public boolean extractEntry(String entry, String destination,
16*cdf0e10cSrcweir         JLabel statusLabel) {
17*cdf0e10cSrcweir 
18*cdf0e10cSrcweir         OutputStream out = null;
19*cdf0e10cSrcweir         InputStream in = null;
20*cdf0e10cSrcweir 
21*cdf0e10cSrcweir         System.out.println("Copying: " + entry);
22*cdf0e10cSrcweir         System.out.println("To: " + destination);
23*cdf0e10cSrcweir 
24*cdf0e10cSrcweir 	if (statusLabel != null) {
25*cdf0e10cSrcweir 		statusLabel.setText("Copying " + entry);
26*cdf0e10cSrcweir 	}
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir         String entryName;
29*cdf0e10cSrcweir         if (entry.lastIndexOf("/") != -1) {
30*cdf0e10cSrcweir             entryName = entry.substring(entry.lastIndexOf("/") + 1);
31*cdf0e10cSrcweir         }
32*cdf0e10cSrcweir         else {
33*cdf0e10cSrcweir             entryName = entry;
34*cdf0e10cSrcweir         }
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir         String destName;
37*cdf0e10cSrcweir         if (destination.lastIndexOf(File.separator) != -1) {
38*cdf0e10cSrcweir             destName = destination.substring(destination.lastIndexOf(File.separator) + 1);
39*cdf0e10cSrcweir         }
40*cdf0e10cSrcweir         else {
41*cdf0e10cSrcweir             destName = destination;
42*cdf0e10cSrcweir         }
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir         if (!destName.equals(entryName))
45*cdf0e10cSrcweir             destination = destination.concat(entryName);
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir         System.out.println("Unzipping " + entry + " to " + destination);
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir 	try {
50*cdf0e10cSrcweir             out = new FileOutputStream(destination);
51*cdf0e10cSrcweir         }
52*cdf0e10cSrcweir         catch (IOException ioe) {
53*cdf0e10cSrcweir             System.err.println("Error opening " + destination +
54*cdf0e10cSrcweir                 ": " + ioe.getMessage());
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir             if (statusLabel != null)
57*cdf0e10cSrcweir                 statusLabel.setText("Error opening" + destination +
58*cdf0e10cSrcweir                     "see SFramework.log for more information");
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir             return false;
61*cdf0e10cSrcweir         }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir         if (entry.startsWith("/") == false)
64*cdf0e10cSrcweir             entry = "/" + entry;
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir         in = this.getClass().getResourceAsStream(entry);
67*cdf0e10cSrcweir         if (in == null) {
68*cdf0e10cSrcweir             System.err.println("File " + entry + " not found in jar file");
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir             if (statusLabel != null)
71*cdf0e10cSrcweir                 statusLabel.setText("Failed extracting " + entry +
72*cdf0e10cSrcweir                     "see SFramework.log for more information");
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir             return false;
75*cdf0e10cSrcweir         }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir         try {
78*cdf0e10cSrcweir             byte[] bytes = new byte[1024];
79*cdf0e10cSrcweir             int len;
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir             while ((len = in.read(bytes)) != -1)
82*cdf0e10cSrcweir                 out.write(bytes, 0, len);
83*cdf0e10cSrcweir         }
84*cdf0e10cSrcweir         catch (IOException ioe) {
85*cdf0e10cSrcweir             System.err.println("Error writing " + destination + ": " +
86*cdf0e10cSrcweir                 ioe.getMessage());
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir             if (statusLabel != null)
89*cdf0e10cSrcweir                 statusLabel.setText("Failed writing " + destination +
90*cdf0e10cSrcweir                     "see SFramework.log for more information");
91*cdf0e10cSrcweir             return false;
92*cdf0e10cSrcweir         }
93*cdf0e10cSrcweir         finally {
94*cdf0e10cSrcweir             try {
95*cdf0e10cSrcweir                 in.close();
96*cdf0e10cSrcweir                 out.close();
97*cdf0e10cSrcweir             }
98*cdf0e10cSrcweir             catch (IOException ioe) {
99*cdf0e10cSrcweir             }
100*cdf0e10cSrcweir         }
101*cdf0e10cSrcweir         return true;
102*cdf0e10cSrcweir     }
103*cdf0e10cSrcweir }
104