Browse Source

hacky check for plugin download validity

Change-Id: Ibcf92364d4db791ee4e01ad9fa163147280f6b52
Reviewed-on: http://gerrit.dmdirc.com/2608
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Shane Mc Cormack <shane@dmdirc.com>
tags/0.7
Greg Holmes 11 years ago
parent
commit
ea301900fa
1 changed files with 34 additions and 1 deletions
  1. 34
    1
      src/com/dmdirc/updater/components/PluginComponent.java

+ 34
- 1
src/com/dmdirc/updater/components/PluginComponent.java View File

@@ -29,6 +29,9 @@ import com.dmdirc.updater.UpdateComponent;
29 29
 import com.dmdirc.updater.Version;
30 30
 
31 31
 import java.io.File;
32
+import java.io.IOException;
33
+import java.util.zip.ZipException;
34
+import java.util.zip.ZipFile;
32 35
 
33 36
 import lombok.RequiredArgsConstructor;
34 37
 
@@ -106,7 +109,11 @@ public class PluginComponent implements UpdateComponent {
106 109
         // If it doesn't work then keep the plugin in a .update file untill
107 110
         // the next restart.
108 111
         // If it does, update the metadata.
109
-        if (requiresRestart() || !new File(path).renameTo(target)) {
112
+        final File newPlugin = new File(path);
113
+        if (!isValid(newPlugin)) {
114
+            return false;
115
+        }
116
+        if (requiresRestart() || !newPlugin.renameTo(target)) {
110 117
             // Windows rocks!
111 118
             final File newTarget = new File(plugin.getMetaData().getPluginUrl().getPath() + ".update");
112 119
 
@@ -123,4 +130,30 @@ public class PluginComponent implements UpdateComponent {
123 130
         return returnCode;
124 131
     }
125 132
 
133
+    /**
134
+     * Test is a file is a valid zip file.
135
+     *
136
+     * @param file Zip file
137
+     *
138
+     * @return true if the file is valid
139
+     */
140
+    private boolean isValid(final File file) {
141
+        ZipFile zipfile = null;
142
+        try {
143
+            zipfile = new ZipFile(file);
144
+            return true;
145
+        } catch (ZipException e) {
146
+            return false;
147
+        } catch (IOException e) {
148
+            return false;
149
+        } finally {
150
+            try {
151
+                if (zipfile != null) {
152
+                    zipfile.close();
153
+                    zipfile = null;
154
+                }
155
+            } catch (IOException e) {
156
+            }
157
+        }
158
+    }
126 159
 }

Loading…
Cancel
Save