Skip to content

Commit dd31f68

Browse files
committed
Fix: handling .ggb files
1 parent 4746dd6 commit dd31f68

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

desktop/src/main/java/org/geogebra/desktop/GeoGebra3D.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,27 @@ public static void main(String[] args) {
9696
fileArgs[i] = args[i];
9797
}
9898

99-
prefs = new AppPrefs(objCfg, modPath);
100-
GeoGebraFrame3D wnd = new GeoGebraFrame3D();
101-
App3D app = new App3D(fileArgs, wnd, prefs);
102-
wnd.init(app);
99+
App3D app = appInstance(fileArgs);
103100

104101
if (!interactiveEh)
105102
return;
106103

107104
Scanner scanner = new Scanner(System.in);
108105
while (scanner.hasNextLine()) {
109106
String line = scanner.nextLine();
110-
wnd.getApplication().getFullGuiManager()
107+
app.getFullGuiManager()
111108
.getFullAlgebraInput().sendCmd(line);
112109
}
113110
scanner.close();
114111
}
115-
112+
113+
public static App3D appInstance(String[] fileArgs) {
114+
GeoGebraFrame3D wnd = new GeoGebraFrame3D();
115+
App3D app = new App3D(fileArgs, wnd, new AppPrefs(objCfg, modPath));
116+
wnd.init(app);
117+
return app;
118+
}
119+
116120
private static void version() {
117121
System.out.println(""
118122
+ "GeoSquared v" + GeoGebraConstants.VERSION_STRING + " "

desktop/src/main/java/org/geogebra/desktop/main/AppD.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.awt.event.WindowListener;
4444
import java.awt.image.BufferedImage;
4545
import java.awt.image.MemoryImageSource;
46+
import java.io.BufferedInputStream;
4647
import java.io.BufferedReader;
4748
import java.io.BufferedWriter;
4849
import java.io.ByteArrayInputStream;
@@ -474,7 +475,15 @@ public AppD(String[] args, JFrame frame, AppPrefs prefs) {
474475
// loadModule clears up previous constructions
475476
this.prefs = prefs;
476477
this.prefs.applyTo(this);
478+
477479
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+
}
478487

479488
// initialize GUI
480489
if (isUsingFullGui()) {
@@ -2684,30 +2693,37 @@ public String getExt(Path p) {
26842693
return fileName.substring(idx + 1);
26852694
}
26862695

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+
26872709
public boolean loadModule(Path p) {
26882710
String ext = getExt(p);
26892711
if (ext == null || ext.equals(""))
26902712
return false;
26912713

26922714
InputStream fstream;
26932715
try {
2694-
fstream = new FileInputStream(p.toFile());
2716+
fstream = Files.newInputStream(p);
26952717
} catch (Exception e) {return false;}
2696-
26972718
// Decompress first
26982719
if (ext.equals("ggb")) {
2699-
ZipInputStream zstream = new ZipInputStream(fstream);
2700-
ZipEntry entry;
27012720
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;
27082725
}
2709-
}
2710-
catch (Exception e) {}
2726+
} catch (Exception e) {}
27112727
}
27122728

27132729
Reader freader = null;

0 commit comments

Comments
 (0)