001: package com.javujavu.javux.app.update;
002:
003: import java.io.BufferedReader;
004: import java.io.File;
005: import java.io.FileOutputStream;
006: import java.io.FileReader;
007: import java.io.IOException;
008: import java.io.InputStream;
009: import java.net.URL;
010: import java.util.Enumeration;
011: import java.util.zip.ZipEntry;
012: import java.util.zip.ZipFile;
013:
014: import com.javujavu.javux.misc.FileFuns;
015: import com.javujavu.javux.proptree.PropTree;
016: import com.javujavu.javux.proptree.PropTreeDocReader;
017:
018: public class UpdateFuns {
019: public static void updateClean(String path, boolean delUpdate) {
020: System.out.println("update clean");
021:
022: String up = path + "update";
023: File fu = new File(up);
024: up += File.separator;
025: String[] dd = fu.list();
026: boolean ok;
027:
028: for (int i = 0; dd != null && i < dd.length; i++) {
029: if (dd[i].endsWith(".del")) {
030: try {
031: String updateFile = up + dd[i];
032: BufferedReader br = new BufferedReader(
033: new FileReader(updateFile));
034:
035: String l;
036: while ((l = br.readLine()) != null) {
037: File f = new File(path
038: + l.replace('/', File.separatorChar));
039: if (f.isFile()) {
040: ok = f.delete();
041: System.out.println("delete " + l
042: + ((ok) ? " OK" : " FAILED"));
043: } else if (f.isDirectory()) {
044: String[] ll = f.list();
045: if (ll == null || ll.length == 0) {
046: ok = f.delete();
047: System.out.println("delete " + l
048: + ((ok) ? " OK" : " FAILED"));
049: }
050: }
051: }
052: br.close();
053:
054: (new File(updateFile)).delete();
055: } catch (IOException e) {
056: }
057: } else if (delUpdate
058: && (dd[i].endsWith(".zip") || dd[i]
059: .equals("updater.jar"))) {
060: ok = (new File(up + dd[i])).delete();
061: System.out.println("delete " + dd[i]
062: + ((ok) ? " OK" : " FAILED"));
063: }
064: }
065: dd = fu.list();
066: if (dd == null || dd.length == 0) {
067: fu.delete();
068: }
069: }
070:
071: private static final String[] UPDATE_URLS = {
072: "http://127.0.0.1:81/download/",
073: "http://javujavu.com/download/",
074: "http://programics.com/download/" };
075:
076: public static PropTree checkUpdate(String module, String version,
077: boolean javuDebug) {
078: for (int i = (javuDebug) ? 0 : 1; i < UPDATE_URLS.length; i++) {
079: try {
080: URL u = new URL(UPDATE_URLS[i] + "?update=" + module
081: + "&version=" + version);
082: InputStream is = u.openStream();
083: PropTree t = new PropTree();
084: PropTreeDocReader r = new PropTreeDocReader();
085: r.load(is, t);
086: return t;
087: } catch (IOException e) {
088: }
089: }
090: return null;
091: }
092:
093: public static boolean unpackUpdater(String startPath, String zipPath) {
094: return unpackFile(startPath, zipPath, "update/updater.jar");
095: }
096:
097: public static boolean unpackFile(String startPath, String zipPath,
098: String file) {
099: boolean r = false;
100: try {
101: ZipFile zip = new ZipFile(zipPath);
102:
103: String prefix = null;
104: Enumeration ee = zip.entries();
105: while (prefix == null && ee.hasMoreElements()) {
106: ZipEntry e = (ZipEntry) ee.nextElement();
107: String n = e.getName();
108: int i = n.indexOf('/');
109: if (i == -1)
110: continue;
111: prefix = n.substring(0, i + 1);
112: }
113:
114: if (prefix != null) {
115: ZipEntry e = zip.getEntry(prefix + file);
116: if (e != null) {
117: r = unpackFile(zip, startPath, file, e);
118: }
119: }
120: zip.close();
121: } catch (IOException e) {
122: }
123: return r;
124: }
125:
126: public static boolean unpackFile(ZipFile zip, String startPath,
127: String file, ZipEntry e) {
128: System.out.print("unpack " + file);
129: try {
130: String p = startPath
131: + file.replace('/', File.separatorChar);
132: int i = p.lastIndexOf(File.separatorChar);
133: if (i != -1) {
134: File f = new File(p.substring(0, i));
135: if (!f.exists())
136: f.mkdirs();
137: }
138: File f = new File(p);
139: if (f.isFile()) {
140: if (!f.delete()) {
141: System.out.println(" SKIP");
142: return false;
143: }
144: }
145:
146: InputStream is = zip.getInputStream(e);
147: FileOutputStream os = new FileOutputStream(p);
148:
149: byte[] buf = new byte[64 * 1024];
150: int len;
151: while ((len = is.read(buf)) > 0) {
152: os.write(buf, 0, len);
153: }
154: os.flush();
155: os.close();
156: is.close();
157:
158: FileFuns.setLastModified(new File(p), e.getTime());
159:
160: System.out.println(" OK");
161: return true;
162: } catch (IOException e1) {
163: }
164: System.out.println(" FAILED");
165: return false;
166: }
167:
168: public static void update(String startPath) {
169: String p1 = startPath + "update";
170: File f = new File(p1);
171: String[] ll = f.list();
172: for (int i = 0; ll != null && i < ll.length; i++) {
173: if (ll[i].endsWith(".zip")) {
174: update(startPath, p1 + File.separator + ll[i]);
175: }
176: }
177: }
178:
179: public static void update(String startPath, String zipPath) {
180: System.out.println("unpacking " + zipPath);
181: try {
182: ZipFile z = new ZipFile(zipPath);
183:
184: int prefixLen = -1;
185: Enumeration ee = z.entries();
186: while (ee.hasMoreElements()) {
187: ZipEntry e = (ZipEntry) ee.nextElement();
188: String n = e.getName();
189:
190: if (prefixLen == -1) {
191: int i = n.indexOf('/');
192: if (i == -1)
193: continue;
194: prefixLen = i + 1;
195: }
196: if (n.length() <= prefixLen)
197: continue;
198: String n2 = n.substring(prefixLen);
199:
200: if (n2.startsWith("update/") && !e.isDirectory()) {
201: UpdateFuns.unpackFile(z, startPath, n2, e);
202: }
203: }
204: if (prefixLen == -1) {
205: z.close();
206: return;
207: }
208:
209: UpdateFuns.updateClean(startPath, false);
210:
211: ee = z.entries();
212: while (ee.hasMoreElements()) {
213: ZipEntry e = (ZipEntry) ee.nextElement();
214: String n = e.getName();
215:
216: if (n.length() <= prefixLen)
217: continue;
218: String n2 = n.substring(prefixLen);
219:
220: if (!n2.startsWith("update/") && !e.isDirectory()) {
221: UpdateFuns.unpackFile(z, startPath, n2, e);
222: }
223: }
224:
225: z.close();
226:
227: (new File(zipPath)).delete();
228: } catch (IOException e) {
229: }
230: }
231:
232: }
|