From 7efe73eb80fb66d3d9eb67eeb7bcb10727462956 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20H=C3=B6lting?= <87192362+moritz-hoelting@users.noreply.github.com> Date: Fri, 21 Jun 2024 09:40:06 +0200 Subject: [PATCH] add zip_with_comment for VFolder --- src/util/compile.rs | 2 +- src/virtual_fs.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/util/compile.rs b/src/util/compile.rs index cb39275..a584634 100644 --- a/src/util/compile.rs +++ b/src/util/compile.rs @@ -15,7 +15,7 @@ use super::extendable_queue::ExtendableQueue; pub struct CompileOptions { /// Whether to compile in debug mode. pub(crate) debug: bool, - + /// Pack format of the datapack. pub(crate) pack_formats: RangeInclusive, } diff --git a/src/virtual_fs.rs b/src/virtual_fs.rs index 30593af..4622c12 100644 --- a/src/virtual_fs.rs +++ b/src/virtual_fs.rs @@ -177,6 +177,47 @@ impl VFolder { } } + writer.set_comment("Data pack created with Shulkerbox"); + + writer.finish()?; + + Ok(()) + } + + #[cfg(feature = "zip")] + /// Zip the folder and its contents into a zip archive with the given comment. + /// + /// # Errors + /// - If the zip archive cannot be written + pub fn zip_with_comment(&self, path: &Path, comment: S) -> io::Result<()> + where + S: Into, + { + use io::Write; + + // open target file + let file = fs::File::create(path)?; + let mut writer = ZipWriter::new(file); + let virtual_files = self.flatten(); + + // write each file to the zip archive + for (path, file) in virtual_files { + writer.start_file(path, zip::write::SimpleFileOptions::default())?; + match file { + VFile::Text(text) => { + writer.write_all(text.as_bytes())?; + } + VFile::Binary(data) => { + writer.write_all(data)?; + } + } + } + + let comment: String = comment.into(); + if !comment.is_empty() { + writer.set_comment(comment); + } + writer.finish()?; Ok(())