Allow a Path to be used as a file/directory name
Instead of using string directly, also accept a Path object. This moves the responsibility from using the correct path separator and not using invalid path components to this library.
This commit is contained in:
parent
d1a0a7d472
commit
cdbdc008fc
2 changed files with 46 additions and 1 deletions
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
|
||||
name = "zip"
|
||||
version = "0.5.1"
|
||||
version = "0.5.2"
|
||||
authors = ["Mathijs van de Nes <git@mathijs.vd-nes.nl>"]
|
||||
license = "MIT"
|
||||
repository = "https://github.com/mvdnes/zip-rs.git"
|
||||
|
|
45
src/write.rs
45
src/write.rs
|
@ -258,6 +258,14 @@ impl<W: Write+io::Seek> ZipWriter<W>
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Starts a file, taking a Path as argument.
|
||||
///
|
||||
/// This function ensures that the '/' path seperator is used. It also ignores all non 'Normal'
|
||||
/// Components, such as a starting '/' or '..' and '.'.
|
||||
pub fn start_file_from_path(&mut self, path: &std::path::Path, options: FileOptions) -> ZipResult<()> {
|
||||
self.start_file(path_to_string(path), options)
|
||||
}
|
||||
|
||||
/// Add a directory entry.
|
||||
///
|
||||
/// You can't write data to the file afterwards.
|
||||
|
@ -282,6 +290,14 @@ impl<W: Write+io::Seek> ZipWriter<W>
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Add a directory entry, taking a Path as argument.
|
||||
///
|
||||
/// This function ensures that the '/' path seperator is used. It also ignores all non 'Normal'
|
||||
/// Components, such as a starting '/' or '..' and '.'.
|
||||
pub fn add_directory_from_path(&mut self, path: &std::path::Path, options: FileOptions) -> ZipResult<()> {
|
||||
self.add_directory(path_to_string(path.into()), options)
|
||||
}
|
||||
|
||||
/// Finish the last file and write all other zip-structures
|
||||
///
|
||||
/// This will return the writer, but one should normally not append any data to the end of the file.
|
||||
|
@ -519,6 +535,22 @@ fn build_extra_field(_file: &ZipFileData) -> ZipResult<Vec<u8>>
|
|||
Ok(writer)
|
||||
}
|
||||
|
||||
fn path_to_string(path: &std::path::Path) -> String {
|
||||
let mut path_str = String::new();
|
||||
for component in path.components() {
|
||||
match component {
|
||||
std::path::Component::Normal(os_str) => {
|
||||
if path_str.len() != 0 {
|
||||
path_str.push('/');
|
||||
}
|
||||
path_str.push_str(&*os_str.to_string_lossy());
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
path_str
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::io;
|
||||
|
@ -568,4 +600,17 @@ mod test {
|
|||
v.extend_from_slice(include_bytes!("../tests/data/mimetype.zip"));
|
||||
assert_eq!(result.get_ref(), &v);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn path_to_string() {
|
||||
let mut path = std::path::PathBuf::new();
|
||||
#[cfg(windows)] path.push(r"C:\");
|
||||
#[cfg(unix)] path.push("/");
|
||||
path.push("windows");
|
||||
path.push("..");
|
||||
path.push(".");
|
||||
path.push("system32");
|
||||
let path_str = super::path_to_string(&path);
|
||||
assert_eq!(path_str, "windows/system32");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue