|
| 1 | +/* |
| 2 | + * Tai-e: A Static Analysis Framework for Java |
| 3 | + * |
| 4 | + * Copyright (C) 2022 Tian Tan <tiantan@nju.edu.cn> |
| 5 | + * Copyright (C) 2022 Yue Li <yueli@nju.edu.cn> |
| 6 | + * |
| 7 | + * This file is part of Tai-e. |
| 8 | + * |
| 9 | + * Tai-e is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Lesser General Public License |
| 11 | + * as published by the Free Software Foundation, either version 3 |
| 12 | + * of the License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * Tai-e is distributed in the hope that it will be useful,but WITHOUT |
| 15 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 | + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General |
| 17 | + * Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public |
| 20 | + * License along with Tai-e. If not, see <https://www.gnu.org/licenses/>. |
| 21 | + */ |
| 22 | + |
| 23 | +package pascal.taie.util; |
| 24 | + |
| 25 | +import pascal.taie.config.Options; |
| 26 | + |
| 27 | +import java.io.File; |
| 28 | +import java.io.FileInputStream; |
| 29 | +import java.io.FileOutputStream; |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Objects; |
| 34 | +import java.util.zip.ZipEntry; |
| 35 | +import java.util.zip.ZipOutputStream; |
| 36 | + |
| 37 | +public class IssuePackager { |
| 38 | + public static void createIssuePackage(Options options) { |
| 39 | + try { |
| 40 | + List<Source> filePaths = new ArrayList<>(); |
| 41 | + filePaths.add(new Source("output", new File(options.getOutputDir().getCanonicalPath()))); |
| 42 | + options.getClassPath().stream() |
| 43 | + .map(path -> new Source("cp", new File(path))) |
| 44 | + .forEach(filePaths::add); |
| 45 | + options.getAppClassPath().stream() |
| 46 | + .map(path -> new Source("acp", new File(path))) |
| 47 | + .forEach(filePaths::add); |
| 48 | + options.getInputClasses().stream() |
| 49 | + .map(path -> new Source("input-classes", new File(path))) |
| 50 | + .forEach(filePaths::add); |
| 51 | + createZipFile("package.zip", filePaths); |
| 52 | + } catch (IOException e) { |
| 53 | + throw new RuntimeException(e); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private static void createZipFile(String zipFileName, List<Source> filePaths) { |
| 58 | + try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFileName))) { |
| 59 | + for (Source source : filePaths) { |
| 60 | + File file = source.file; |
| 61 | + String category = source.category; |
| 62 | + addToZipFile(category, file, zos); |
| 63 | + } |
| 64 | + } catch (IOException e) { |
| 65 | + throw new RuntimeException(e); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static void addToZipFile(String category, File file, ZipOutputStream zos) { |
| 70 | + if (file.isDirectory()) { |
| 71 | + for (File subFile : Objects.requireNonNull(file.listFiles())) { |
| 72 | + addToZipFile(category + "/" + subFile.getName(), subFile, zos); |
| 73 | + } |
| 74 | + } else { |
| 75 | + try (FileInputStream fis = new FileInputStream(file)) { |
| 76 | + ZipEntry zipEntry = new ZipEntry(category + "/" + file.getName()); |
| 77 | + zos.putNextEntry(zipEntry); |
| 78 | + |
| 79 | + byte[] buffer = new byte[1024]; |
| 80 | + int length; |
| 81 | + while ((length = fis.read(buffer)) > 0) { |
| 82 | + zos.write(buffer, 0, length); |
| 83 | + } |
| 84 | + zos.closeEntry(); |
| 85 | + } catch (IOException e) { |
| 86 | + throw new RuntimeException(e); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private record Source(String category, File file) { |
| 92 | + } |
| 93 | +} |
0 commit comments