diff --git a/crates/zip/src/archive.rs b/crates/zip/src/archive.rs
index b9b3b3d80..0f26267c1 100644
--- a/crates/zip/src/archive.rs
+++ b/crates/zip/src/archive.rs
@@ -108,6 +108,15 @@ impl<'a> Archive<'a> {
Ok(Some(file.is_dir()))
}
+ /// Whether the entry at `index` is a symbolic link according to its Unix mode bits.
+ pub fn entry_is_symlink(&mut self, index: usize) -> Result, Error> {
+ if index >= self.zip.len() {
+ return Ok(None);
+ }
+ let file = self.zip.by_index_raw(index).map_err(|error| Error::NotZip(error.to_string()))?;
+ Ok(Some(file.unix_mode().is_some_and(|mode| mode & 0o170000 == 0o120000)))
+ }
+
/// Declared decoded and compressed sizes at `index`, without name lookup.
pub fn entry_sizes(&mut self, index: usize) -> Result , Error> {
if index >= self.zip.len() {
@@ -274,4 +283,30 @@ mod ownership_tests {
drop(clone);
assert!(weak.upgrade().is_none());
}
+
+ #[test]
+ fn entry_is_symlink_reports_unix_symlink_entries() {
+ let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new()));
+ zip.add_symlink("link", "/outside", zip::write::SimpleFileOptions::default()).unwrap();
+ zip.start_file("file.txt", zip::write::SimpleFileOptions::default()).unwrap();
+ std::io::Write::write_all(&mut zip, b"ok").unwrap();
+ let bytes = zip.finish().unwrap().into_inner();
+ let mut archive = Archive::open(&bytes, Limits { max_entries: 4, max_total_bytes: 16 }).unwrap();
+ assert_eq!(archive.entry_is_symlink(0).unwrap(), Some(true));
+ assert_eq!(archive.entry_is_symlink(1).unwrap(), Some(false));
+ assert_eq!(archive.entry_is_symlink(2).unwrap(), None);
+ }
+
+ #[test]
+ fn streaming_writer_supports_directories_and_custom_destinations() {
+ let mut writer = crate::Writer::to_writer(Cursor::new(Vec::new()));
+ writer.add_directory("resources/").unwrap();
+ writer.add("resources/data.bin", b"1234", crate::Compression::Stored).unwrap();
+ let bytes = writer.finish_into().unwrap().into_inner();
+ let mut archive = Archive::open(&bytes, Limits { max_entries: 4, max_total_bytes: 16 }).unwrap();
+ assert_eq!(archive.len(), 2);
+ assert_eq!(archive.entry_is_dir(0).unwrap(), Some(true));
+ assert_eq!(archive.entry_is_dir(1).unwrap(), Some(false));
+ assert_eq!(archive.read("resources/data.bin", 16).unwrap().unwrap(), b"1234");
+ }
}
diff --git a/crates/zip/src/write.rs b/crates/zip/src/write.rs
index 451729759..76bf753a6 100644
--- a/crates/zip/src/write.rs
+++ b/crates/zip/src/write.rs
@@ -1,6 +1,6 @@
//! A ZIP archive being written.
-use std::io::{Cursor, Write};
+use std::io::{Cursor, Seek, Write};
use crate::Error;
@@ -16,21 +16,21 @@ pub enum Compression {
Level(u8),
}
-/// A ZIP archive being written into memory. The output is deterministic: the same (1980)
+/// A ZIP archive being written. The output is deterministic: the same (1980)
/// timestamp on every entry. Entries are written in the order they are added.
-pub struct Writer {
- zip: zip::ZipWriter>>,
+pub struct Writer>> {
+ zip: zip::ZipWriter,
legacy_utf8: bool,
}
-impl Default for Writer {
- fn default() -> Writer {
+impl Default for Writer>> {
+ fn default() -> Writer>> {
Writer { zip: zip::ZipWriter::new(Cursor::new(Vec::new())), legacy_utf8: false }
}
}
-impl Writer {
- pub fn new() -> Writer {
+impl Writer>> {
+ pub fn new() -> Writer>> {
Writer::default()
}
@@ -38,10 +38,33 @@ impl Writer {
///
/// This keeps stored archives byte-for-byte stable for existing users while still using the
/// shared writer for entry encoding and validation.
- pub fn legacy_utf8() -> Writer {
+ pub fn legacy_utf8() -> Writer>> {
Writer { legacy_utf8: true, ..Writer::default() }
}
+ /// The finished archive.
+ pub fn finish(self) -> Result, Error> {
+ let mut bytes = self.zip.finish().map(Cursor::into_inner).map_err(|e| Error::Write(e.to_string()))?;
+ if self.legacy_utf8 {
+ patch_legacy_utf8_headers(&mut bytes)?;
+ }
+ Ok(bytes)
+ }
+}
+
+impl Writer {
+ /// Starts writing an archive directly to a seekable destination without buffering the whole
+ /// archive in memory.
+ pub fn to_writer(destination: W) -> Self {
+ Self { zip: zip::ZipWriter::new(destination), legacy_utf8: false }
+ }
+
+ /// Adds a directory entry with the deterministic 1980 timestamp.
+ pub fn add_directory(&mut self, name: &str) -> Result<(), Error> {
+ let options = zip::write::SimpleFileOptions::default().last_modified_time(zip::DateTime::default());
+ self.zip.add_directory(name, options).map_err(|e| Error::Write(format!("{name}: {e}")))
+ }
+
/// Adds an entry from a byte slice. A name the archive already has is an error.
pub fn add(&mut self, name: &str, data: &[u8], compression: Compression) -> Result<(), Error> {
self.add_entry(name, data, compression, 1, data.len() as u64 >= u64::from(u32::MAX) / 2)
@@ -98,13 +121,18 @@ impl Writer {
write(&mut self.zip).map_err(|e| fail(&e))
}
- /// The finished archive.
- pub fn finish(self) -> Result, Error> {
- let mut bytes = self.zip.finish().map(Cursor::into_inner).map_err(|e| Error::Write(e.to_string()))?;
+ /// Finishes writing and returns the underlying destination.
+ ///
+ /// Legacy UTF-8 mode requires [`Writer::finish`] to apply its in-memory header patch.
+ /// Calling this method in legacy mode consumes the writer and returns [`Error::Write`];
+ /// use `finish` instead when constructing a writer with [`Writer::legacy_utf8`].
+ pub fn finish_into(self) -> Result {
+ // A generic destination need not support reads or header patching. Never silently
+ // return an archive that omits the byte-compatibility contract of legacy mode.
if self.legacy_utf8 {
- patch_legacy_utf8_headers(&mut bytes)?;
+ return Err(Error::Write("legacy UTF-8 output requires Writer::finish".into()));
}
- Ok(bytes)
+ self.zip.finish().map_err(|e| Error::Write(e.to_string()))
}
}
@@ -184,3 +212,27 @@ fn patch_legacy_utf8_headers(bytes: &mut [u8]) -> Result<(), Error> {
}
Ok(())
}
+
+#[cfg(test)]
+mod finish_tests {
+ use super::*;
+
+ #[test]
+ fn legacy_ascii_output_requires_the_header_patching_finish_path() {
+ let mut legacy = Writer::legacy_utf8();
+ legacy.add("a.txt", b"ordinary text", Compression::Stored).unwrap();
+ assert!(matches!(legacy.finish_into(), Err(Error::Write(message))
+ if message == "legacy UTF-8 output requires Writer::finish"));
+
+ let mut legacy = Writer::legacy_utf8();
+ legacy.add("a.txt", b"ordinary text", Compression::Stored).unwrap();
+ let bytes = legacy.finish().unwrap();
+ assert_eq!(get_u16(&bytes, 6).unwrap() & 0x0800, 0x0800);
+
+ let mut ordinary = Writer::to_writer(Cursor::new(Vec::new()));
+ ordinary.add("a.txt", b"ordinary text", Compression::Stored).unwrap();
+ let bytes = ordinary.finish_into().unwrap().into_inner();
+ let mut archive = crate::Archive::open(&bytes, crate::Limits { max_entries: 1, max_total_bytes: 32 }).unwrap();
+ assert_eq!(archive.read("a.txt", 32).unwrap().unwrap(), b"ordinary text");
+ }
+}