|
2 | 2 |
|
3 | 3 | import static org.junit.Assert.fail;
|
4 | 4 |
|
| 5 | +import java.io.BufferedOutputStream; |
5 | 6 | import java.io.File;
|
| 7 | +import java.io.FileInputStream; |
| 8 | +import java.io.FileOutputStream; |
6 | 9 | import java.io.IOException;
|
7 |
| -import java.net.URISyntaxException; |
8 | 10 | import java.net.URL;
|
9 | 11 | import java.nio.file.Files;
|
10 | 12 | import java.nio.file.Paths;
|
| 13 | +import java.util.zip.ZipEntry; |
| 14 | +import java.util.zip.ZipInputStream; |
11 | 15 |
|
12 | 16 | import org.apache.commons.lang.SystemUtils;
|
13 | 17 | import org.eclipse.cdt.core.model.ICModelMarker;
|
@@ -90,20 +94,20 @@ public static void waitForAllJobsToFinish() {
|
90 | 94 | }
|
91 | 95 | }
|
92 | 96 |
|
93 |
| - public static IPath getTemplateFolder(String templateName) { |
94 |
| - try { |
| 97 | + public static IPath getTemplateFolder(String templateName) throws Exception { |
95 | 98 | Bundle bundle = Platform.getBundle("io.sloeber.tests");
|
96 | 99 | Path path = new Path("src/templates/" + templateName);
|
97 | 100 | URL fileURL = FileLocator.find(bundle, path, null);
|
98 | 101 | URL resolvedFileURL = FileLocator.toFileURL(fileURL);
|
99 | 102 | return new Path(resolvedFileURL.toURI().getPath());
|
100 |
| - } catch (URISyntaxException e) { |
101 |
| - e.printStackTrace(); |
102 |
| - } catch (IOException e) { |
103 |
| - e.printStackTrace(); |
104 |
| - } |
105 |
| - System.err.println("Failed to find templates in io.sloeber.tests plugin."); |
106 |
| - return new Path(new String()); |
| 103 | + } |
| 104 | + |
| 105 | + public static IPath getprojectZip(String zipFileName) throws Exception { |
| 106 | + Bundle bundle = Platform.getBundle("io.sloeber.tests"); |
| 107 | + Path path = new Path("src/projects/" + zipFileName); |
| 108 | + URL fileURL = FileLocator.find(bundle, path, null); |
| 109 | + URL resolvedFileURL = FileLocator.toFileURL(fileURL); |
| 110 | + return new Path(resolvedFileURL.toURI().getPath()); |
107 | 111 | }
|
108 | 112 |
|
109 | 113 | /**
|
@@ -261,4 +265,71 @@ public static String getLastFailMessage() {
|
261 | 265 | // TODO Auto-generated method stub
|
262 | 266 | return myLastFailMessage;
|
263 | 267 | }
|
| 268 | + |
| 269 | + // copied from |
| 270 | + // https://www.codejava.net/java-se/file-io/programmatically-extract-a-zip-file-using-java |
| 271 | + |
| 272 | + // /** |
| 273 | + // * This utility extracts files and directories of a standard zip file to |
| 274 | + // * a destination directory. |
| 275 | + // * @author www.codejava.net |
| 276 | + // * |
| 277 | + // */ |
| 278 | + // public class UnzipUtility { |
| 279 | + /** |
| 280 | + * Size of the buffer to read/write data |
| 281 | + */ |
| 282 | + private static final int BUFFER_SIZE = 4096; |
| 283 | + |
| 284 | + /** |
| 285 | + * Extracts a zip file specified by the zipFilePath to a directory specified by |
| 286 | + * destDirectory (will be created if does not exists) |
| 287 | + * |
| 288 | + * @param zipFilePath |
| 289 | + * @param destDirectory |
| 290 | + * @throws IOException |
| 291 | + */ |
| 292 | + public static void unzip(String zipFilePath, String destDirectory) throws IOException { |
| 293 | + File destDir = new File(destDirectory); |
| 294 | + if (!destDir.exists()) { |
| 295 | + destDir.mkdir(); |
| 296 | + } |
| 297 | + ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)); |
| 298 | + ZipEntry entry = zipIn.getNextEntry(); |
| 299 | + // iterates over entries in the zip file |
| 300 | + while (entry != null) { |
| 301 | + String filePath = destDirectory + File.separator + entry.getName(); |
| 302 | + if (!entry.isDirectory()) { |
| 303 | + // if the entry is a file, extracts it |
| 304 | + extractFile(zipIn, filePath); |
| 305 | + } else { |
| 306 | + // if the entry is a directory, make the directory |
| 307 | + File dir = new File(filePath); |
| 308 | + dir.mkdirs(); |
| 309 | + } |
| 310 | + zipIn.closeEntry(); |
| 311 | + entry = zipIn.getNextEntry(); |
| 312 | + } |
| 313 | + zipIn.close(); |
| 314 | + } |
| 315 | + |
| 316 | + /** |
| 317 | + * Extracts a zip entry (file entry) |
| 318 | + * |
| 319 | + * @param zipIn |
| 320 | + * @param filePath |
| 321 | + * @throws IOException |
| 322 | + */ |
| 323 | + private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException { |
| 324 | + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); |
| 325 | + byte[] bytesIn = new byte[BUFFER_SIZE]; |
| 326 | + int read = 0; |
| 327 | + while ((read = zipIn.read(bytesIn)) != -1) { |
| 328 | + bos.write(bytesIn, 0, read); |
| 329 | + } |
| 330 | + bos.close(); |
| 331 | + } |
| 332 | + // } |
| 333 | + // end copy from |
| 334 | + // https://www.codejava.net/java-se/file-io/programmatically-extract-a-zip-file-using-java |
264 | 335 | }
|
0 commit comments