JScratch
Loading...
Searching...
No Matches
ProjectManager.java
Go to the documentation of this file.
1package com.jscratch;
2
3import com.google.gson.Gson;
4import com.google.gson.GsonBuilder;
5import java.io.*;
6import java.nio.file.*;
7import java.util.*;
8import java.util.zip.*;
9
13public class ProjectManager {
14 private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
15
16 public static void saveProject(Project project, File targetFile) throws IOException {
17 try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(targetFile))) {
18 // Write project.json
19 ZipEntry jsonEntry = new ZipEntry("project.json");
20 zos.putNextEntry(jsonEntry);
21 zos.write(gson.toJson(project).getBytes());
22 zos.closeEntry();
23
24 // Assets will be handled here (copying files into ZIP)
25 // ... logic to include costumes/sounds ...
26 }
27 }
28
29 public static Project loadProject(File sourceFile) throws IOException {
30 try (ZipFile zipFile = new ZipFile(sourceFile)) {
31 ZipEntry jsonEntry = zipFile.getEntry("project.json");
32 if (jsonEntry == null) throw new IOException("Not a valid .jscratch project.");
33
34 try (InputStream is = zipFile.getInputStream(jsonEntry);
35 InputStreamReader reader = new InputStreamReader(is)) {
36 return gson.fromJson(reader, Project.class);
37 }
38 }
39 }
40}
static void saveProject(Project project, File targetFile)
static Project loadProject(File sourceFile)