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

Merged
merged 9 commits into from
Jun 18, 2025
Merged
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
100 changes: 96 additions & 4 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use std::default::Default;
use std::fmt::{Debug, Formatter};
use std::io;
use std::io::prelude::*;
use std::io::Cursor;
use std::io::{BufReader, SeekFrom};
use std::io::{Cursor, ErrorKind};
use std::marker::PhantomData;
use std::mem;
use std::str::{from_utf8, Utf8Error};
Expand Down 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 @@ -625,7 +626,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 @@ -637,6 +637,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 @@ -794,6 +795,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 @@ -980,6 +982,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 @@ -1088,8 +1091,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 @@ -1617,6 +1624,25 @@ impl<W: Write + Seek> ZipWriter<W> {
}
}

impl<W: Write> ZipWriter<StreamWriter<W>> {
/// Creates a writer that doesn't require the inner writer to implement [Seek], but where
/// operations that would overwrite previously-written bytes or cause subsequent operations to
/// do so (such as `abort_file`) will always return an error.
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([]),
zip64_comment: None,
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 @@ -1911,6 +1937,25 @@ 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::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 @@ -1980,6 +2025,53 @@ fn update_local_zip64_extra_field<T: Write + Seek>(
Ok(())
}

/// Wrapper around a [Write] implementation that implements the [Seek] trait, but where seeking
/// returns an error unless it's a no-op.
pub struct StreamWriter<W: Write> {
inner: W,
bytes_written: u64,
}

impl<W: Write> StreamWriter<W> {
/// Creates an instance wrapping the provided inner writer.
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) => return Ok(self.bytes_written),
SeekFrom::Start(x) => {
if x == self.bytes_written {
return Ok(self.bytes_written);
}
}
_ => {}
}
Err(io::Error::new(
ErrorKind::Unsupported,
"seek is not supported",
))
}
}

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