pub struct ZipFilePath<'a>(/* private fields */);
Expand description
Represents a file path within a Zip archive, as a borrowed byte slice.
File paths in Zip archives are not guaranteed to be UTF-8, and may contain relative paths that try and escape the zip or absolute paths.
Implementations§
Source§impl<'a> ZipFilePath<'a>
impl<'a> ZipFilePath<'a>
Sourcepub fn as_bytes(&self) -> &'a [u8] ⓘ
pub fn as_bytes(&self) -> &'a [u8] ⓘ
Return the raw bytes of the Zip file path.
WARNING: this may contain be an absolute path or contain a file path
capable of zip slips. Prefer normalize
.
Sourcepub fn is_dir(&self) -> bool
pub fn is_dir(&self) -> bool
Returns true if the file path is a directory.
This is determined by the file path ending in a slash, but it’s a common convention as otherwise it would be an invalid file.
§Examples
let path = ZipFilePath::new(b"dir/");
assert!(path.is_dir());
let path = ZipFilePath::new(b"dir/file.txt");
assert!(!path.is_dir());
Sourcepub fn normalize(&self) -> Result<Cow<'_, str>, Error>
pub fn normalize(&self) -> Result<Cow<'_, str>, Error>
Normalizes the Zip file path.
The normalization process includes:
- Attempting to interpret the path as UTF-8.
- Replacing backslashes (
\
) with forward slashes (/
). - Removing redundant slashes (e.g.,
//
). - Resolving relative path components (
.
and..
). - Stripping leading slashes and parent directory traversals that would
escape the archive’s root (e.g.,
/foo
becomesfoo
,../foo
becomesfoo
).
In the case where no allocation needs to occur when normalizing, an original reference to the data is returned.
§Examples
Basic path normalization:
let path = ZipFilePath::new(b"dir/test.txt");
assert_eq!(path.normalize().unwrap(), "dir/test.txt");
// Converts backslashes to forward slashes
let path = ZipFilePath::new(b"dir\\test.txt");
assert_eq!(path.normalize().unwrap(), "dir/test.txt");
// Removes redundant slashes
let path = ZipFilePath::new(b"dir//test.txt");
assert_eq!(path.normalize().unwrap(), "dir/test.txt");
Handling relative and absolute paths:
// Removes leading slashes
let path = ZipFilePath::new(b"/test.txt");
assert_eq!(path.normalize().unwrap(), "test.txt");
// Resolves current directory references
let path = ZipFilePath::new(b"./test.txt");
assert_eq!(path.normalize().unwrap(), "test.txt");
// Resolves parent directory references
let path = ZipFilePath::new(b"dir/../test.txt");
assert_eq!(path.normalize().unwrap(), "test.txt");
let path = ZipFilePath::new(b"a/b/c/d/../../test.txt");
assert_eq!(path.normalize().unwrap(), "a/b/test.txt");
let path = ZipFilePath::new(b"dir/");
assert_eq!(path.normalize().unwrap(), "dir/");
Invalid paths:
// Invalid UTF-8 sequences result in an error
let path = ZipFilePath::new(&[0xFF]);
assert!(path.normalize().is_err());
let path = ZipFilePath::new(&[b't', b'e', b's', b't', 0xFF]);
assert!(path.normalize().is_err());
§Errors
- if the file path is not valid UTF-8.
Trait Implementations§
Source§impl<'a> Clone for ZipFilePath<'a>
impl<'a> Clone for ZipFilePath<'a>
Source§fn clone(&self) -> ZipFilePath<'a>
fn clone(&self) -> ZipFilePath<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more