|
43 | 43 | import java.awt.event.WindowListener; |
44 | 44 | import java.awt.image.BufferedImage; |
45 | 45 | import java.awt.image.MemoryImageSource; |
| 46 | +import java.io.BufferedInputStream; |
46 | 47 | import java.io.BufferedReader; |
47 | 48 | import java.io.BufferedWriter; |
48 | 49 | import java.io.ByteArrayInputStream; |
@@ -474,7 +475,15 @@ public AppD(String[] args, JFrame frame, AppPrefs prefs) { |
474 | 475 | // loadModule clears up previous constructions |
475 | 476 | this.prefs = prefs; |
476 | 477 | this.prefs.applyTo(this); |
| 478 | + |
477 | 479 | boolean fileLoaded = false; |
| 480 | + if (args != null && args[0] != null && !args[0].equals("")) { |
| 481 | + fileLoaded = loadModule(Paths.get(args[0])); |
| 482 | + if (!fileLoaded) { |
| 483 | + System.out.println("Invalid file"); |
| 484 | + System.exit(1); |
| 485 | + } |
| 486 | + } |
478 | 487 |
|
479 | 488 | // initialize GUI |
480 | 489 | if (isUsingFullGui()) { |
@@ -2684,30 +2693,37 @@ public String getExt(Path p) { |
2684 | 2693 | return fileName.substring(idx + 1); |
2685 | 2694 | } |
2686 | 2695 |
|
| 2696 | + public static InputStream ggb2gsq(Path ggbPath) throws IOException { |
| 2697 | + String target = "geogebra.xml"; |
| 2698 | + ZipInputStream zstream = new ZipInputStream(Files.newInputStream(ggbPath)); |
| 2699 | + ZipEntry entry; |
| 2700 | + |
| 2701 | + while ((entry = zstream.getNextEntry()) != null) |
| 2702 | + if (entry.getName().equals(target)) |
| 2703 | + return new BufferedInputStream(zstream); |
| 2704 | + |
| 2705 | + zstream.close(); |
| 2706 | + return null; |
| 2707 | + } |
| 2708 | + |
2687 | 2709 | public boolean loadModule(Path p) { |
2688 | 2710 | String ext = getExt(p); |
2689 | 2711 | if (ext == null || ext.equals("")) |
2690 | 2712 | return false; |
2691 | 2713 |
|
2692 | 2714 | InputStream fstream; |
2693 | 2715 | try { |
2694 | | - fstream = new FileInputStream(p.toFile()); |
| 2716 | + fstream = Files.newInputStream(p); |
2695 | 2717 | } catch (Exception e) {return false;} |
2696 | | - |
2697 | 2718 | // Decompress first |
2698 | 2719 | if (ext.equals("ggb")) { |
2699 | | - ZipInputStream zstream = new ZipInputStream(fstream); |
2700 | | - ZipEntry entry; |
2701 | 2720 | try { |
2702 | | - while ((entry = zstream.getNextEntry()) != null) { |
2703 | | - // Construction file |
2704 | | - if (entry.getName().equals("geogebra.xml")) { |
2705 | | - fstream = new InflaterInputStream(zstream); |
2706 | | - break; |
2707 | | - } |
| 2721 | + fstream = ggb2gsq(p); |
| 2722 | + if (fstream == null) { |
| 2723 | + Log.error("Invalid GGB file"); |
| 2724 | + return false; |
2708 | 2725 | } |
2709 | | - } |
2710 | | - catch (Exception e) {} |
| 2726 | + } catch (Exception e) {} |
2711 | 2727 | } |
2712 | 2728 |
|
2713 | 2729 | Reader freader = null; |
|
0 commit comments