diff --git a/api/src/main/java/net/momirealms/customnameplates/api/util/ZipUtils.java b/api/src/main/java/net/momirealms/customnameplates/api/util/ZipUtils.java new file mode 100644 index 0000000..dfb25dd --- /dev/null +++ b/api/src/main/java/net/momirealms/customnameplates/api/util/ZipUtils.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) <2024> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.momirealms.customnameplates.api.util; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.stream.Stream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +public class ZipUtils { + + public static void zipDirectory(Path folderPath, Path zipFilePath) throws IOException { + try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilePath.toFile()))) { + try (Stream paths = Files.walk(folderPath)) { + for (Path path : (Iterable) paths::iterator) { + if (Files.isDirectory(path)) { + continue; + } + String zipEntryName = folderPath.relativize(path).toString().replace("\\", "/"); + ZipEntry zipEntry = new ZipEntry(zipEntryName); + try (InputStream is = Files.newInputStream(path)) { + addToZip(zipEntry, is, zos); + } + } + } + } + } + + public static void addToZip(ZipEntry zipEntry, InputStream is, ZipOutputStream zos) throws IOException { + zos.putNextEntry(zipEntry); + byte[] buffer = new byte[4096]; + int bytesRead; + while ((bytesRead = is.read(buffer)) != -1) { + zos.write(buffer, 0, bytesRead); + } + zos.closeEntry(); + } +} diff --git a/backend/src/main/java/net/momirealms/customnameplates/backend/feature/pack/ResourcePackManagerImpl.java b/backend/src/main/java/net/momirealms/customnameplates/backend/feature/pack/ResourcePackManagerImpl.java index 99692b9..cd6fd14 100644 --- a/backend/src/main/java/net/momirealms/customnameplates/backend/feature/pack/ResourcePackManagerImpl.java +++ b/backend/src/main/java/net/momirealms/customnameplates/backend/feature/pack/ResourcePackManagerImpl.java @@ -35,6 +35,7 @@ import net.momirealms.customnameplates.api.feature.nameplate.Nameplate; import net.momirealms.customnameplates.api.feature.pack.ResourcePackManager; import net.momirealms.customnameplates.api.helper.VersionHelper; import net.momirealms.customnameplates.api.util.CharacterUtils; +import net.momirealms.customnameplates.api.util.ZipUtils; import org.apache.commons.io.FileUtils; import javax.imageio.ImageIO; @@ -123,6 +124,12 @@ public class ResourcePackManagerImpl implements ResourcePackManager { this.setPackFormat(); // copy the resource pack to hooked plugins this.copyResourcePackToHookedPlugins(resourcePackFolder); + + try { + ZipUtils.zipDirectory(resourcePackFolder.toPath(), plugin.getDataFolder().toPath().resolve("resourcepack.zip")); + } catch (IOException e) { + plugin.getPluginLogger().warn("Failed to zip resourcepack.zip", e); + } } private void saveFont(JsonObject fontJson) {