|
6 | 6 | import fr.insee.vtl.coverage.utils.JSONStructureLoader; |
7 | 7 |
|
8 | 8 | import java.io.File; |
| 9 | +import java.io.FileInputStream; |
9 | 10 | import java.io.IOException; |
| 11 | +import java.io.InputStream; |
10 | 12 | import java.nio.charset.StandardCharsets; |
11 | 13 | import java.nio.file.Files; |
12 | 14 | import java.nio.file.Path; |
|
18 | 20 | public class TCK { |
19 | 21 | private static final ObjectMapper objectMapper = new ObjectMapper(); |
20 | 22 |
|
21 | | - public static List<Folder> runTCK(String zipPath) { |
22 | | - File extractedFolder = null; |
| 23 | + public static List<Folder> runTCK(InputStream zipInputStream) { |
| 24 | + File extractedFolder; |
23 | 25 | try { |
24 | | - extractedFolder = init(new File(zipPath)); |
| 26 | + extractedFolder = init(zipInputStream); |
25 | 27 | } catch (IOException e) { |
26 | | - throw new RuntimeException(e); |
| 28 | + throw new RuntimeException("Error unzipping input stream", e); |
27 | 29 | } |
28 | | - List<Folder> folders; |
| 30 | + |
29 | 31 | try { |
30 | | - folders = loadInput(extractedFolder); |
| 32 | + return loadInput(extractedFolder); |
31 | 33 | } catch (Exception e) { |
32 | | - throw new RuntimeException(e); |
| 34 | + throw new RuntimeException("Error loading input from extracted folder", e); |
| 35 | + } finally { |
| 36 | + deleteDirectory(extractedFolder); |
33 | 37 | } |
34 | | - deleteDirectory(extractedFolder); |
35 | | - return folders; |
36 | 38 | } |
37 | 39 |
|
38 | | - private static File init(File zipFile) throws IOException { |
| 40 | + public static List<Folder> runTCK(File zipFile) { |
| 41 | + try (InputStream in = Files.newInputStream(zipFile.toPath())) { |
| 42 | + return runTCK(in); |
| 43 | + } catch (IOException e) { |
| 44 | + throw new RuntimeException("Error reading zip file: " + zipFile, e); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static List<Folder> runTCK(String zipPath) { |
| 49 | + return runTCK(new File(zipPath)); |
| 50 | + } |
| 51 | + |
| 52 | + private static File init(InputStream zipInputStream) throws IOException { |
39 | 53 | Path tempDir = Files.createTempDirectory("tck-unzip-"); |
40 | | - try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(zipFile.toPath()))) { |
| 54 | + try (ZipInputStream zis = new ZipInputStream(zipInputStream)) { |
41 | 55 | ZipEntry entry; |
42 | 56 | while ((entry = zis.getNextEntry()) != null) { |
43 | 57 | Path newPath = zipSlipProtect(entry, tempDir); |
|
0 commit comments