From cfa3b1151c7226ddfcbaf40c88c9485fbfb5426c Mon Sep 17 00:00:00 2001 From: Marli Frost Date: Mon, 19 Apr 2021 11:58:45 +0100 Subject: [PATCH] fix: allow non-utf8 comments in ZipWriter --- src/write.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/write.rs b/src/write.rs index 68ae11472..3d538daf7 100644 --- a/src/write.rs +++ b/src/write.rs @@ -68,7 +68,7 @@ pub struct ZipWriter { files: Vec, stats: ZipWriterStats, writing_to_file: bool, - comment: String, + comment: Vec, writing_raw: bool, } @@ -225,7 +225,7 @@ impl ZipWriter { files, stats: Default::default(), writing_to_file: false, - comment: String::new(), + comment: footer.zip_file_comment, writing_raw: true, // avoid recomputing the last file's header }) } @@ -241,7 +241,7 @@ impl ZipWriter { files: Vec::new(), stats: Default::default(), writing_to_file: false, - comment: String::new(), + comment: Vec::new(), writing_raw: false, } } @@ -251,7 +251,18 @@ impl ZipWriter { where S: Into, { - self.comment = comment.into(); + self.set_raw_comment(comment.into().into()) + } + + /// Set ZIP archive comment. + /// + /// This sets the raw bytes of the comment. The comment + /// is typically expected to be encoded in UTF-8 + pub fn set_raw_comment(&mut self, comment: Vec) + where + S: Into, + { + self.comment = comment; } /// Start a new file for with the requested options. @@ -522,7 +533,7 @@ impl ZipWriter { number_of_files: self.files.len() as u16, central_directory_size: central_size as u32, central_directory_offset: central_start as u32, - zip_file_comment: self.comment.as_bytes().to_vec(), + zip_file_comment: self.comment.clone(), }; footer.write(writer)?;