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