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 2 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 @@ -762,9 +762,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 @@ -776,8 +783,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
82 changes: 80 additions & 2 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
pub(super) writing_raw: bool,
pub(super) comment: 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 @@ -637,6 +638,7 @@
comment: footer.zip_file_comment,
writing_raw: true, // avoid recomputing the last file's header
flush_on_finish_file: false,
seek_possible: true,
})
} else {
Err(InvalidArchive("No central-directory end header found"))
Expand Down Expand Up @@ -795,6 +797,7 @@
writing_raw: false,
comment: Box::new([]),
flush_on_finish_file: false,
seek_possible: true,
}
}

Expand Down Expand Up @@ -955,6 +958,7 @@
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 @@ -1063,8 +1067,12 @@
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 @@ -1550,6 +1558,21 @@
}
}

impl<W: Write> ZipWriter<StreamWriter<W>> {
pub fn new_stream(inner: W) -> ZipWriter<StreamWriter<W>> {

Check failure on line 1562 in src/write.rs

View workflow job for this annotation

GitHub Actions / Build and test --no-default-features: macOS-latest, msrv

missing documentation for an associated function

Check failure on line 1562 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

missing documentation for an associated function
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 @@ -1828,6 +1851,26 @@
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: &ZipFileData) -> ZipResult<()> {
const CRC32_OFFSET: u64 = 14;
writer.seek(SeekFrom::Start(file.header_start + CRC32_OFFSET))?;
Expand Down Expand Up @@ -1885,6 +1928,41 @@
Ok(())
}

pub struct StreamWriter<W: Write> {

Check failure on line 1931 in src/write.rs

View workflow job for this annotation

GitHub Actions / Build and test --no-default-features: macOS-latest, msrv

missing documentation for a struct

Check failure on line 1931 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

missing documentation for a struct
inner: W,
bytes_written: u64,
}

impl<W: Write> StreamWriter<W> {
pub fn new(inner: W) -> StreamWriter<W> {

Check failure on line 1937 in src/write.rs

View workflow job for this annotation

GitHub Actions / Build and test --no-default-features: macOS-latest, msrv

missing documentation for an associated function

Check failure on line 1937 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

missing documentation for an associated function
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
Loading