Add zip feature and make zip method conditional

This commit is contained in:
Moritz Hölting 2024-03-27 21:40:14 +01:00
parent af2c9dbe09
commit 6572243ce4
2 changed files with 10 additions and 7 deletions

View File

@ -5,8 +5,11 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["zip"]
zip = ["dep:zip"]
[dependencies]
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
zip = { version = "0.6.6", default-features = false, features = ["deflate", "time"] }
zip = { version = "0.6.6", default-features = false, features = ["deflate", "time"], optional = true }

View File

@ -1,13 +1,9 @@
//! Virtual file system for creating and manipulating files and folders in memory.
use std::{
collections::HashMap,
fs,
io::{self, Write},
path::Path,
};
use std::{collections::HashMap, fs, io, path::Path};
use serde::{Deserialize, Serialize};
#[cfg(feature = "zip")]
use zip::ZipWriter;
/// Folder representation in virtual file system
@ -143,8 +139,11 @@ impl VFolder {
Ok(())
}
#[cfg(feature = "zip")]
/// Zip the folder and its contents into a zip archive.
pub fn zip(&self, path: &Path) -> io::Result<()> {
use io::Write;
let file = fs::File::create(path)?;
let mut writer = ZipWriter::new(file);
let virtual_files = self.flatten();
@ -166,6 +165,7 @@ impl VFolder {
Ok(())
}
#[allow(dead_code)]
/// Flatten the folder and its contents into a list of files with full paths.
fn flatten(&self) -> Vec<(String, &VFile)> {
let mut files = self