Skip to content

Commit 4c492f2

Browse files
authored
Merge branch 'master' into fix-some-imports
2 parents 89057eb + 13005c5 commit 4c492f2

File tree

7 files changed

+89
-34
lines changed

7 files changed

+89
-34
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ jobs:
6060
runs-on: ubuntu-latest
6161
steps:
6262
- uses: actions/checkout@v5
63-
- run: sudo apt install gcc-s390x-linux-gnu
63+
- run: sudo apt-get update
64+
- run: sudo apt-get install crossbuild-essential-s390x
6465
- run: rustup toolchain add nightly-x86_64-unknown-linux-gnu
6566
- run: rustup toolchain add --force-non-host stable-s390x-unknown-linux-gnu
6667
- run: rustup target add s390x-unknown-linux-gnu --toolchain stable-s390x-unknown-linux-gnu

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ deflate-flate2 = ["_deflate-any", "dep:flate2"]
7777
deflate-flate2-zlib-rs = ["deflate-flate2", "flate2/zlib-rs"]
7878
# Pull in flate2 and the zlib backend; only use this if you need a dynamically linked system zlib
7979
deflate-flate2-zlib = ["deflate-flate2", "flate2/zlib"]
80+
# Pull in flate2 and the zlib-ng backend; a modern fork of zlib
81+
deflate-flate2-zlib-ng = ["deflate-flate2", "flate2/zlib-ng"]
82+
# Pull in flate2 and the zlib-ng backend with a zlib-compatible API
83+
deflate-flate2-zlib-ng-compat = ["deflate-flate2", "flate2/zlib-ng-compat"]
8084
# Pull in zopfli (write-only DEFLATE, slower than flate2 with better compression ratios)
8185
deflate-zopfli = ["dep:zopfli", "_deflate-any"]
8286
jiff-02 = ["dep:jiff"]

src/compression.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ impl CompressionMethod {
139139
pub const AES: Self = CompressionMethod::Aes;
140140
#[cfg(not(feature = "aes-crypto"))]
141141
pub const AES: Self = CompressionMethod::Unsupported(99);
142+
143+
#[cfg(feature = "_deflate-any")]
144+
pub const DEFAULT: Self = CompressionMethod::Deflated;
145+
146+
#[cfg(all(not(feature = "_deflate-any"), feature = "bzip2"))]
147+
pub const DEFAULT: Self = CompressionMethod::Bzip2;
148+
149+
#[cfg(all(not(feature = "_deflate-any"), not(feature = "bzip2")))]
150+
pub const DEFAULT: Self = CompressionMethod::Stored;
142151
}
143152
impl CompressionMethod {
144153
pub(crate) const fn parse_from_u16(val: u16) -> Self {
@@ -229,11 +238,7 @@ impl CompressionMethod {
229238

230239
impl Default for CompressionMethod {
231240
fn default() -> Self {
232-
#[cfg(feature = "_deflate-any")]
233-
return CompressionMethod::Deflated;
234-
235-
#[cfg(not(feature = "_deflate-any"))]
236-
return CompressionMethod::Stored;
241+
Self::DEFAULT
237242
}
238243
}
239244

src/read.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1656,7 +1656,7 @@ impl<'a, R: Read> ZipFile<'a, R> {
16561656
/// `foo/../bar` as `foo/bar` (instead of `bar`). Because of this,
16571657
/// [`ZipFile::enclosed_name`] is the better option in most scenarios.
16581658
///
1659-
/// [`ParentDir`]: `PathBuf::Component::ParentDir`
1659+
/// [`ParentDir`]: `Component::ParentDir`
16601660
pub fn mangled_name(&self) -> PathBuf {
16611661
self.get_metadata().file_name_sanitized()
16621662
}

src/spec.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl ExtraFieldMagic {
9191
/// The file size at which a ZIP64 record becomes necessary.
9292
///
9393
/// If a file larger than this threshold attempts to be written, compressed or uncompressed, and
94-
/// [`FileOptions::large_file()`](crate::write::FileOptions) was not true, then [`ZipWriter`] will
94+
/// [`FileOptions::large_file()`](crate::write::FileOptions::large_file) was not true, then [`crate::ZipWriter`] will
9595
/// raise an [`io::Error`] with [`io::ErrorKind::Other`].
9696
///
9797
/// If the zip file itself is larger than this value, then a zip64 central directory record will be
@@ -107,32 +107,32 @@ impl ExtraFieldMagic {
107107
///
108108
/// let mut zip = ZipWriter::new(Cursor::new(Vec::new()));
109109
/// // Writing an extremely large file for this test is faster without compression.
110-
/// let options = SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
111110
///
112111
/// let big_len: usize = (zip::ZIP64_BYTES_THR as usize) + 1;
113112
/// let big_buf = vec![0u8; big_len];
114113
/// {
115-
/// zip.start_file("zero.dat", options)?;
116-
/// // This is too big!
117-
/// let res = zip.write_all(&big_buf[..]).err().unwrap();
118-
/// assert_eq!(res.kind(), io::ErrorKind::Other);
119-
/// let description = format!("{}", &res);
120-
/// assert_eq!(description, "Large file option has not been set");
121-
/// }
122-
/// {
123-
/// // Attempting to write anything further to the same zip will still succeed, but the previous
124-
/// // failing entry has been removed.
125-
/// zip.start_file("one.dat", options)?;
126-
/// let zip = zip.finish_into_readable()?;
127-
/// let names: Vec<_> = zip.file_names().collect();
128-
/// assert_eq!(&names, &["one.dat"]);
114+
/// let options = SimpleFileOptions::default()
115+
/// .compression_method(zip::CompressionMethod::Stored);
116+
/// zip.start_file("zero.dat", options)?;
117+
/// // This is too big!
118+
/// let res = zip.write_all(&big_buf[..]).err().unwrap();
119+
/// assert_eq!(res.kind(), io::ErrorKind::Other);
120+
/// let description = format!("{}", &res);
121+
/// assert_eq!(description, "Large file option has not been set");
122+
/// // Attempting to write anything further to the same zip will still succeed, but the previous
123+
/// // failing entry has been removed.
124+
/// zip.start_file("one.dat", options)?;
125+
/// let zip = zip.finish_into_readable()?;
126+
/// let names: Vec<_> = zip.file_names().collect();
127+
/// assert_eq!(&names, &["one.dat"]);
129128
/// }
130129
///
131130
/// // Create a new zip output.
132131
/// let mut zip = ZipWriter::new(Cursor::new(Vec::new()));
133132
/// // This time, create a zip64 record for the file.
134133
/// let options = SimpleFileOptions::default()
135-
/// .compression_method(zip::CompressionMethod::Stored).large_file(true);
134+
/// .compression_method(zip::CompressionMethod::Stored)
135+
/// .large_file(true);
136136
/// zip.start_file("zero.dat", options)?;
137137
/// // This succeeds because we specified that it could be a large file.
138138
/// assert!(zip.write_all(&big_buf[..]).is_ok());
@@ -144,7 +144,7 @@ pub const ZIP64_BYTES_THR: u64 = u32::MAX as u64;
144144
/// The number of entries within a single zip necessary to allocate a zip64 central
145145
/// directory record.
146146
///
147-
/// If more than this number of entries is written to a [`ZipWriter`], then [`ZipWriter::finish()`]
147+
/// If more than this number of entries is written to a [`crate::ZipWriter`], then [`crate::ZipWriter::finish()`]
148148
/// will write out extra zip64 data to the end of the zip file.
149149
pub const ZIP64_ENTRY_THR: usize = u16::MAX as usize;
150150

src/types.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ impl PartialOrd for DateTime {
154154
}
155155

156156
impl DateTime {
157+
/// Constructs a default datetime of 1980-01-01 00:00:00.
158+
pub const DEFAULT: Self = DateTime {
159+
datepart: 0b0000000000100001,
160+
timepart: 0,
161+
};
162+
157163
/// Returns the current time if possible, otherwise the default of 1980-01-01.
158164
#[cfg(feature = "time")]
159165
pub fn default_for_write() -> Self {
@@ -274,10 +280,7 @@ impl From<DateTime> for (u16, u16) {
274280
impl Default for DateTime {
275281
/// Constructs an 'default' datetime of 1980-01-01 00:00:00
276282
fn default() -> DateTime {
277-
DateTime {
278-
datepart: 0b0000000000100001,
279-
timepart: 0,
280-
}
283+
DateTime::DEFAULT
281284
}
282285
}
283286

@@ -977,10 +980,21 @@ impl ZipFileData {
977980
pub(crate) fn write_data_descriptor<W: std::io::Write>(
978981
&self,
979982
writer: &mut W,
983+
auto_large_file: bool,
980984
) -> Result<(), ZipError> {
981985
if self.large_file {
982986
return self.zip64_data_descriptor_block().write(writer);
983987
}
988+
if self.compressed_size > spec::ZIP64_BYTES_THR
989+
|| self.uncompressed_size > spec::ZIP64_BYTES_THR
990+
{
991+
if auto_large_file {
992+
return self.zip64_data_descriptor_block().write(writer);
993+
}
994+
return Err(ZipError::Io(std::io::Error::other(
995+
"Large file option has not been set - use .large_file(true) in options",
996+
)));
997+
}
984998
self.data_descriptor_block().write(writer)
985999
}
9861000

src/write.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ pub(crate) mod zip_writer {
182182
pub(super) zip64_comment: Option<Box<[u8]>>,
183183
pub(super) flush_on_finish_file: bool,
184184
pub(super) seek_possible: bool,
185+
pub(crate) auto_large_file: bool,
185186
}
186187

187188
impl<W: Write + Seek> Debug for ZipWriter<W> {
@@ -395,10 +396,9 @@ impl<T: FileOptionExtension> FileOptions<'_, T> {
395396

396397
/// Set the compression method for the new file
397398
///
398-
/// The default is `CompressionMethod::Deflated` if it is enabled. If not,
399-
/// `CompressionMethod::Bzip2` is the default if it is enabled. If neither `bzip2` nor `deflate`
400-
/// is enabled, `CompressionMethod::Zlib` is the default. If all else fails,
401-
/// `CompressionMethod::Stored` becomes the default and files are written uncompressed.
399+
/// The default is [`CompressionMethod::Deflated`] if it is enabled. If not,
400+
/// [`CompressionMethod::Bzip2`] is the default if it is enabled. If neither `bzip2` nor `deflate`
401+
/// is enabled, [`CompressionMethod::Stored`] becomes the default and files are written uncompressed.
402402
#[must_use]
403403
pub const fn compression_method(mut self, method: CompressionMethod) -> Self {
404404
self.compression_method = method;
@@ -522,6 +522,28 @@ impl FileOptions<'_, ExtendedFileOptions> {
522522
self
523523
}
524524
}
525+
impl FileOptions<'static, ()> {
526+
/// Constructs a const FileOptions object.
527+
///
528+
/// Note: This value is different than the return value of [`FileOptions::default()`]:
529+
///
530+
/// - The `last_modified_time` is [`DateTime::DEFAULT`]. This corresponds to 1980-01-01 00:00:00
531+
pub const DEFAULT: Self = Self {
532+
compression_method: CompressionMethod::DEFAULT,
533+
compression_level: None,
534+
last_modified_time: DateTime::DEFAULT,
535+
large_file: false,
536+
permissions: None,
537+
encrypt_with: None,
538+
extended_options: (),
539+
alignment: 1,
540+
#[cfg(feature = "deflate-zopfli")]
541+
zopfli_buffer_size: Some(1 << 15),
542+
#[cfg(feature = "aes-crypto")]
543+
aes_mode: None,
544+
};
545+
}
546+
525547
impl<T: FileOptionExtension> Default for FileOptions<'_, T> {
526548
/// Construct a new FileOptions object
527549
fn default() -> Self {
@@ -614,6 +636,7 @@ impl<A: Read + Write + Seek> ZipWriter<A> {
614636
writing_raw: true, // avoid recomputing the last file's header
615637
flush_on_finish_file: false,
616638
seek_possible: true,
639+
auto_large_file: false,
617640
})
618641
}
619642

@@ -772,9 +795,16 @@ impl<W: Write + Seek> ZipWriter<W> {
772795
zip64_comment: None,
773796
flush_on_finish_file: false,
774797
seek_possible: true,
798+
auto_large_file: false,
775799
}
776800
}
777801

802+
/// Set automatically large file to true if needed
803+
pub fn set_auto_large_file(mut self) -> Self {
804+
self.auto_large_file = true;
805+
self
806+
}
807+
778808
/// Returns true if a file is currently open for writing.
779809
pub const fn is_writing_file(&self) -> bool {
780810
self.writing_to_file && !self.inner.is_closed()
@@ -1075,7 +1105,7 @@ impl<W: Write + Seek> ZipWriter<W> {
10751105
};
10761106
update_aes_extra_data(writer, file)?;
10771107
if file.using_data_descriptor {
1078-
file.write_data_descriptor(writer)?;
1108+
file.write_data_descriptor(writer, self.auto_large_file)?;
10791109
} else {
10801110
update_local_file_header(writer, file)?;
10811111
writer.seek(SeekFrom::Start(file_end))?;
@@ -1622,6 +1652,7 @@ impl<W: Write> ZipWriter<StreamWriter<W>> {
16221652
zip64_comment: None,
16231653
flush_on_finish_file: false,
16241654
seek_possible: false,
1655+
auto_large_file: false,
16251656
}
16261657
}
16271658
}

0 commit comments

Comments
 (0)