Add zip feature and make zip method conditional
This commit is contained in:
parent
af2c9dbe09
commit
6572243ce4
|
@ -5,8 +5,11 @@ edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["zip"]
|
||||||
|
zip = ["dep:zip"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde = { version = "1.0.197", features = ["derive"] }
|
serde = { version = "1.0.197", features = ["derive"] }
|
||||||
serde_json = "1.0.114"
|
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 }
|
||||||
|
|
|
@ -1,13 +1,9 @@
|
||||||
//! Virtual file system for creating and manipulating files and folders in memory.
|
//! Virtual file system for creating and manipulating files and folders in memory.
|
||||||
|
|
||||||
use std::{
|
use std::{collections::HashMap, fs, io, path::Path};
|
||||||
collections::HashMap,
|
|
||||||
fs,
|
|
||||||
io::{self, Write},
|
|
||||||
path::Path,
|
|
||||||
};
|
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
#[cfg(feature = "zip")]
|
||||||
use zip::ZipWriter;
|
use zip::ZipWriter;
|
||||||
|
|
||||||
/// Folder representation in virtual file system
|
/// Folder representation in virtual file system
|
||||||
|
@ -143,8 +139,11 @@ impl VFolder {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "zip")]
|
||||||
/// Zip the folder and its contents into a zip archive.
|
/// Zip the folder and its contents into a zip archive.
|
||||||
pub fn zip(&self, path: &Path) -> io::Result<()> {
|
pub fn zip(&self, path: &Path) -> io::Result<()> {
|
||||||
|
use io::Write;
|
||||||
|
|
||||||
let file = fs::File::create(path)?;
|
let file = fs::File::create(path)?;
|
||||||
let mut writer = ZipWriter::new(file);
|
let mut writer = ZipWriter::new(file);
|
||||||
let virtual_files = self.flatten();
|
let virtual_files = self.flatten();
|
||||||
|
@ -166,6 +165,7 @@ impl VFolder {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
/// Flatten the folder and its contents into a list of files with full paths.
|
/// Flatten the folder and its contents into a list of files with full paths.
|
||||||
fn flatten(&self) -> Vec<(String, &VFile)> {
|
fn flatten(&self) -> Vec<(String, &VFile)> {
|
||||||
let mut files = self
|
let mut files = self
|
||||||
|
|
Loading…
Reference in New Issue