fix zip failing if parent directory does not exists
This commit is contained in:
parent
46499b6abe
commit
6fefa27f2f
|
@ -166,31 +166,7 @@ impl VFolder {
|
||||||
where
|
where
|
||||||
P: AsRef<std::path::Path>,
|
P: AsRef<std::path::Path>,
|
||||||
{
|
{
|
||||||
use std::{fs, io::Write};
|
self.zip_with_optional_comment(path.as_ref(), None)
|
||||||
|
|
||||||
// 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)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
writer.set_comment("Data pack created with Shulkerbox");
|
|
||||||
|
|
||||||
writer.finish()?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Zip the folder and its contents into a zip archive with the given comment.
|
/// Zip the folder and its contents into a zip archive with the given comment.
|
||||||
|
@ -203,8 +179,20 @@ impl VFolder {
|
||||||
P: AsRef<std::path::Path>,
|
P: AsRef<std::path::Path>,
|
||||||
S: Into<String>,
|
S: Into<String>,
|
||||||
{
|
{
|
||||||
|
self.zip_with_optional_comment(path.as_ref(), Some(comment.into()))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(all(feature = "fs_access", feature = "zip"))]
|
||||||
|
fn zip_with_optional_comment(
|
||||||
|
&self,
|
||||||
|
path: &std::path::Path,
|
||||||
|
comment: Option<String>,
|
||||||
|
) -> std::io::Result<()> {
|
||||||
use std::{fs, io::Write};
|
use std::{fs, io::Write};
|
||||||
|
|
||||||
|
if let Some(parent) = path.parent() {
|
||||||
|
fs::create_dir_all(parent)?;
|
||||||
|
}
|
||||||
// open target file
|
// open target file
|
||||||
let file = fs::File::create(path)?;
|
let file = fs::File::create(path)?;
|
||||||
let mut writer = ZipWriter::new(file);
|
let mut writer = ZipWriter::new(file);
|
||||||
|
@ -223,10 +211,11 @@ impl VFolder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let comment: String = comment.into();
|
if let Some(comment) = comment {
|
||||||
if !comment.is_empty() {
|
if !comment.is_empty() {
|
||||||
writer.set_comment(comment);
|
writer.set_comment(comment);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
writer.finish()?;
|
writer.finish()?;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue