Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
fix: allow non-utf8 comments in ZipWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
Plecra committed Apr 19, 2021
1 parent 32b2f5b commit cfa3b11
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub struct ZipWriter<W: Write + io::Seek> {
files: Vec<ZipFileData>,
stats: ZipWriterStats,
writing_to_file: bool,
comment: String,
comment: Vec<u8>,
writing_raw: bool,
}

Expand Down Expand Up @@ -225,7 +225,7 @@ impl<A: Read + Write + io::Seek> ZipWriter<A> {
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
})
}
Expand All @@ -241,7 +241,7 @@ impl<W: Write + io::Seek> ZipWriter<W> {
files: Vec::new(),
stats: Default::default(),
writing_to_file: false,
comment: String::new(),
comment: Vec::new(),
writing_raw: false,
}
}
Expand All @@ -251,7 +251,18 @@ impl<W: Write + io::Seek> ZipWriter<W> {
where
S: Into<String>,
{
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<S>(&mut self, comment: Vec<u8>)
where
S: Into<String>,
{
self.comment = comment;
}

/// Start a new file for with the requested options.
Expand Down Expand Up @@ -522,7 +533,7 @@ impl<W: Write + io::Seek> ZipWriter<W> {
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)?;
Expand Down

0 comments on commit cfa3b11

Please sign in to comment.