From 6572243ce46b37146bc858868eefb3018ca3cdec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20H=C3=B6lting?= <87192362+moritz-hoelting@users.noreply.github.com> Date: Wed, 27 Mar 2024 21:40:14 +0100 Subject: [PATCH] Add zip feature and make zip method conditional --- Cargo.toml | 5 ++++- src/virtual_fs.rs | 12 ++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f586abc..1298cad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/virtual_fs.rs b/src/virtual_fs.rs index ff4980b..a0d3fd3 100644 --- a/src/virtual_fs.rs +++ b/src/virtual_fs.rs @@ -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