add zip_with_comment for VFolder

This commit is contained in:
Moritz Hölting 2024-06-21 09:40:06 +02:00
parent d9e3184a0e
commit 7efe73eb80
2 changed files with 42 additions and 1 deletions

View File

@ -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<u8>,
}

View File

@ -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<S>(&self, path: &Path, comment: S) -> io::Result<()>
where
S: Into<String>,
{
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(())