Skip to content

Commit

Permalink
fix: zip needs unix paths
Browse files Browse the repository at this point in the history
ZIP expects unix-style paths here, and passing Windows paths will cause
incorrect and broken files to be added to the directory.

The `zip` crate's `start_file_from_path` and `add_directory_from_path` normalize
these, but they were deprecated without replacement so we need to do the work
ourselves.

Fixes axodotdev/cargo-dist#873.
  • Loading branch information
mistydemeo committed Mar 26, 2024
1 parent 4a3723f commit dd14197
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/compression.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Compression-related methods, all used in `axoasset::Local`

use camino::Utf8Path;
use camino::{Utf8Path, Utf8PathBuf};

/// Internal tar-file compression algorithms
#[cfg(feature = "compression-tar")]
Expand Down Expand Up @@ -279,10 +279,19 @@ pub(crate) fn zip_dir(
name.to_owned()
};

// ZIP files always need Unix-style file separators; we need to
// convert any Windows file names to use Unix separators before
// passing them to any of the other functions.
let unix_name = Utf8PathBuf::from(&name)
.components()
.map(|c| c.as_str())
.collect::<Vec<&str>>()
.join("/");

// Write file or directory explicitly
// Some unzip tools unzip files with directory paths correctly, some do not!
if path.is_file() {
zip.start_file(name, options)?;
zip.start_file(&unix_name, options)?;
let mut f = File::open(path)?;

f.read_to_end(&mut buffer)?;
Expand All @@ -291,7 +300,7 @@ pub(crate) fn zip_dir(
} else if !name.as_str().is_empty() {
// Only if not root! Avoids path spec / warning
// and mapname conversion failed error on unzip
zip.add_directory(name, options)?;
zip.add_directory(&unix_name, options)?;
}
}
zip.finish()?;
Expand Down

0 comments on commit dd14197

Please sign in to comment.