Skip to content

feat: Write ZIP file to stream #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,9 +849,16 @@ impl ZipFileData {
} else {
0
};

let using_data_descriptor_bit = if self.using_data_descriptor {
1u16 << 3
} else {
0
};

let encrypted_bit: u16 = if self.encrypted { 1u16 << 0 } else { 0 };

utf8_bit | encrypted_bit
utf8_bit | using_data_descriptor_bit | encrypted_bit
}

fn clamp_size_field(&self, field: u64) -> u32 {
Expand All @@ -863,8 +870,14 @@ impl ZipFileData {
}

pub(crate) fn local_block(&self) -> ZipResult<ZipLocalEntryBlock> {
let compressed_size: u32 = self.clamp_size_field(self.compressed_size);
let uncompressed_size: u32 = self.clamp_size_field(self.uncompressed_size);
let (compressed_size, uncompressed_size) = if self.using_data_descriptor {
(0, 0)
} else {
(
self.clamp_size_field(self.compressed_size),
self.clamp_size_field(self.uncompressed_size),
)
};
let extra_field_length: u16 = self
.extra_field_len()
.try_into()
Expand Down
83 changes: 80 additions & 3 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ pub(crate) mod zip_writer {
pub(super) comment: Box<[u8]>,
pub(super) zip64_comment: Option<Box<[u8]>>,
pub(super) flush_on_finish_file: bool,
pub(super) seek_possible: bool,
}

impl<W: Write + Seek> Debug for ZipWriter<W> {
Expand Down Expand Up @@ -634,7 +635,6 @@ impl<A: Read + Write + Seek> ZipWriter<A> {
/// This uses the given read configuration to initially read the archive.
pub fn new_append_with_config(config: Config, mut readwriter: A) -> ZipResult<ZipWriter<A>> {
readwriter.seek(SeekFrom::Start(0))?;

let shared = ZipArchive::get_metadata(config, &mut readwriter)?;

Ok(ZipWriter {
Expand All @@ -646,6 +646,7 @@ impl<A: Read + Write + Seek> ZipWriter<A> {
zip64_comment: shared.zip64_comment,
writing_raw: true, // avoid recomputing the last file's header
flush_on_finish_file: false,
seek_possible: true,
})
}

Expand Down Expand Up @@ -800,6 +801,7 @@ impl<W: Write + Seek> ZipWriter<W> {
comment: Box::new([]),
zip64_comment: None,
flush_on_finish_file: false,
seek_possible: true,
}
}

Expand Down Expand Up @@ -989,6 +991,7 @@ impl<W: Write + Seek> ZipWriter<W> {
aes_mode,
&extra_data,
);
file.using_data_descriptor = !self.seek_possible;
file.version_made_by = file.version_made_by.max(file.version_needed() as u8);
file.extra_data_start = Some(header_end);
let index = self.insert_file_data(file)?;
Expand Down Expand Up @@ -1097,8 +1100,12 @@ impl<W: Write + Seek> ZipWriter<W> {
0
};
update_aes_extra_data(writer, file)?;
update_local_file_header(writer, file)?;
writer.seek(SeekFrom::Start(file_end))?;
if file.using_data_descriptor {
write_data_descriptor(writer, file)?;
} else {
update_local_file_header(writer, file)?;
writer.seek(SeekFrom::Start(file_end))?;
}
}
if self.flush_on_finish_file {
let result = writer.flush();
Expand Down Expand Up @@ -1622,6 +1629,21 @@ impl<W: Write + Seek> ZipWriter<W> {
}
}

impl<W: Write> ZipWriter<StreamWriter<W>> {
pub fn new_stream(inner: W) -> ZipWriter<StreamWriter<W>> {
ZipWriter {
inner: Storer(MaybeEncrypted::Unencrypted(StreamWriter::new(inner))),
files: IndexMap::new(),
stats: Default::default(),
writing_to_file: false,
writing_raw: false,
comment: Box::new([]),
flush_on_finish_file: false,
seek_possible: false,
}
}
}

impl<W: Write + Seek> Drop for ZipWriter<W> {
fn drop(&mut self) {
if !self.inner.is_closed() {
Expand Down Expand Up @@ -1909,6 +1931,26 @@ fn update_aes_extra_data<W: Write + Seek>(writer: &mut W, file: &mut ZipFileData
Ok(())
}

fn write_data_descriptor<T: Write>(writer: &mut T, file: &ZipFileData) -> ZipResult<()> {
writer.write_u32_le(file.crc32)?;
if file.large_file {
writer.write_u64_le(file.compressed_size)?;
writer.write_u64_le(file.uncompressed_size)?;
} else {
// check compressed size as well as it can also be slightly larger than uncompressed size
if file.compressed_size > spec::ZIP64_BYTES_THR {
return Err(ZipError::Io(io::Error::new(
io::ErrorKind::Other,
"Large file option has not been set",
)));
}

writer.write_u32_le(file.compressed_size as u32)?;
writer.write_u32_le(file.uncompressed_size as u32)?;
}
Ok(())
}

fn update_local_file_header<T: Write + Seek>(
writer: &mut T,
file: &mut ZipFileData,
Expand Down Expand Up @@ -1979,6 +2021,41 @@ fn update_local_zip64_extra_field<T: Write + Seek>(
Ok(())
}

pub struct StreamWriter<W: Write> {
inner: W,
bytes_written: u64,
}

impl<W: Write> StreamWriter<W> {
pub fn new(inner: W) -> StreamWriter<W> {
Self {
inner,
bytes_written: 0,
}
}
}

impl<W: Write> Write for StreamWriter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let bytes_written = self.inner.write(buf)?;
self.bytes_written += bytes_written as u64;
Ok(bytes_written)
}

fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
}

impl<W: Write> Seek for StreamWriter<W> {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
match pos {
SeekFrom::Current(0) | SeekFrom::End(0) => Ok(self.bytes_written),
_ => panic!("Seek is not supported, trying to seek to {:?}", pos),
}
}
}

#[cfg(not(feature = "unreserved"))]
const EXTRA_FIELD_MAPPING: [u16; 43] = [
0x0007, 0x0008, 0x0009, 0x000a, 0x000c, 0x000d, 0x000e, 0x000f, 0x0014, 0x0015, 0x0016, 0x0017,
Expand Down