@@ -13,7 +13,7 @@ use crate::types::{
1313 ZipRawValues , MIN_VERSION ,
1414} ;
1515use crate :: write:: ffi:: S_IFLNK ;
16- #[ cfg( any ( feature = "_deflate-any" , feature = "bzip2" , feature = "zstd" , ) ) ]
16+ #[ cfg( feature = "deflate-zopfli" ) ]
1717use core:: num:: NonZeroU64 ;
1818use crc32fast:: Hasher ;
1919use indexmap:: IndexMap ;
@@ -754,6 +754,7 @@ impl<A: Read + Write + Seek> ZipWriter<A> {
754754 /// [`Self::finish()`].
755755 ///
756756 ///```
757+ /// # #[cfg(all(feature = "deflate-zopfli", not(feature = "deflate-flate2")))] {
757758 /// # fn main() -> Result<(), zip::result::ZipError> {
758759 /// use std::io::{Cursor, prelude::*};
759760 /// use zip::{ZipArchive, ZipWriter, write::SimpleFileOptions};
@@ -770,6 +771,7 @@ impl<A: Read + Write + Seek> ZipWriter<A> {
770771 /// assert_eq!(s, "hello\n");
771772 /// # Ok(())
772773 /// # }
774+ /// # }
773775 ///```
774776 pub fn finish_into_readable ( mut self ) -> ZipResult < ZipArchive < A > > {
775777 let central_start = self . finalize ( ) ?;
@@ -1191,6 +1193,7 @@ impl<W: Write + Seek> ZipWriter<W> {
11911193 /// calling [`Self::raw_copy_file()`] for each entry from the `source` archive in sequence.
11921194 ///
11931195 ///```
1196+ /// # #[cfg(all(feature = "deflate-zopfli", not(feature = "deflate-flate2")))] {
11941197 /// # fn main() -> Result<(), zip::result::ZipError> {
11951198 /// use std::io::{Cursor, prelude::*};
11961199 /// use zip::{ZipArchive, ZipWriter, write::SimpleFileOptions};
@@ -1208,6 +1211,7 @@ impl<W: Write + Seek> ZipWriter<W> {
12081211 /// let src2 = ZipArchive::new(zip.finish()?)?;
12091212 ///
12101213 /// let buf = Cursor::new(Vec::new());
1214+ ///
12111215 /// let mut zip = ZipWriter::new(buf);
12121216 /// zip.merge_archive(src)?;
12131217 /// zip.merge_archive(src2)?;
@@ -1221,6 +1225,7 @@ impl<W: Write + Seek> ZipWriter<W> {
12211225 /// assert_eq!(s, "hey\n");
12221226 /// # Ok(())
12231227 /// # }
1228+ /// # }
12241229 ///```
12251230 pub fn merge_archive < R > ( & mut self , mut source : ZipArchive < R > ) -> ZipResult < ( ) >
12261231 where
@@ -1660,14 +1665,11 @@ impl<W: Write + Seek> GenericZipWriter<W> {
16601665 }
16611666 #[ cfg( feature = "_deflate-any" ) ]
16621667 CompressionMethod :: Deflated => {
1663- let default = if cfg ! ( all(
1664- feature = "deflate-zopfli" ,
1665- not( feature = "deflate-flate2" )
1666- ) ) {
1667- 24
1668- } else {
1669- Compression :: default ( ) . level ( ) as i64
1670- } ;
1668+ #[ cfg( feature = "deflate-flate2" ) ]
1669+ let default = Compression :: default ( ) . level ( ) as i64 ;
1670+
1671+ #[ cfg( all( feature = "deflate-zopfli" , not( feature = "deflate-flate2" ) ) ) ]
1672+ let default = 24 ;
16711673
16721674 let level = clamp_opt (
16731675 compression_level. unwrap_or ( default) ,
@@ -1677,12 +1679,11 @@ impl<W: Write + Seek> GenericZipWriter<W> {
16771679 as u32 ;
16781680
16791681 #[ cfg( feature = "deflate-zopfli" ) ]
1680- {
1681- let best_non_zopfli = Compression :: best ( ) . level ( ) ;
1682- if level > best_non_zopfli {
1682+ macro_rules! deflate_zopfli_and_return {
1683+ ( $bare: expr, $best_non_zopfli: expr) => {
16831684 let options = Options {
16841685 iteration_count: NonZeroU64 :: try_from(
1685- ( level - best_non_zopfli) as u64 ,
1686+ ( level - $ best_non_zopfli) as u64 ,
16861687 )
16871688 . unwrap( ) ,
16881689 ..Default :: default ( )
@@ -1702,9 +1703,23 @@ impl<W: Write + Seek> GenericZipWriter<W> {
17021703 zopfli:: DeflateEncoder :: new( options, Default :: default ( ) , bare) ,
17031704 ) ,
17041705 } ) ) ;
1706+ } ;
1707+ }
1708+
1709+ #[ cfg( all( feature = "deflate-zopfli" , feature = "deflate-flate2" ) ) ]
1710+ {
1711+ let best_non_zopfli = Compression :: best ( ) . level ( ) ;
1712+ if level > best_non_zopfli {
1713+ deflate_zopfli_and_return ! ( bare, best_non_zopfli) ;
17051714 }
17061715 }
17071716
1717+ #[ cfg( all( feature = "deflate-zopfli" , not( feature = "deflate-flate2" ) ) ) ]
1718+ {
1719+ let best_non_zopfli = 9 ;
1720+ deflate_zopfli_and_return ! ( bare, best_non_zopfli) ;
1721+ }
1722+
17081723 #[ cfg( feature = "deflate-flate2" ) ]
17091724 {
17101725 Ok ( Box :: new ( move |bare| {
@@ -1838,18 +1853,15 @@ impl<W: Write + Seek> GenericZipWriter<W> {
18381853
18391854#[ cfg( feature = "_deflate-any" ) ]
18401855fn deflate_compression_level_range ( ) -> std:: ops:: RangeInclusive < i64 > {
1841- let min = if cfg ! ( feature = "deflate-flate2" ) {
1842- Compression :: fast ( ) . level ( ) as i64
1843- } else {
1844- Compression :: best ( ) . level ( ) as i64 + 1
1845- } ;
1856+ #[ cfg( feature = "deflate-flate2" ) ]
1857+ let min = Compression :: fast ( ) . level ( ) as i64 ;
1858+ #[ cfg( all( feature = "deflate-zopfli" , not( feature = "deflate-flate2" ) ) ) ]
1859+ let min = 1 ;
18461860
1847- let max = Compression :: best ( ) . level ( ) as i64
1848- + if cfg ! ( feature = "deflate-zopfli" ) {
1849- u8:: MAX as i64
1850- } else {
1851- 0
1852- } ;
1861+ #[ cfg( feature = "deflate-zopfli" ) ]
1862+ let max = 264 ;
1863+ #[ cfg( all( feature = "deflate-flate2" , not( feature = "deflate-zopfli" ) ) ) ]
1864+ let max = Compression :: best ( ) . level ( ) as i64 ;
18531865
18541866 min..=max
18551867}
@@ -2001,7 +2013,9 @@ mod test {
20012013 use crate :: zipcrypto:: ZipCryptoKeys ;
20022014 use crate :: CompressionMethod :: Stored ;
20032015 use crate :: ZipArchive ;
2004- use std:: io:: { Cursor , Read , Write } ;
2016+ #[ cfg( feature = "deflate-flate2" ) ]
2017+ use std:: io:: Read ;
2018+ use std:: io:: { Cursor , Write } ;
20052019 use std:: marker:: PhantomData ;
20062020 use std:: path:: PathBuf ;
20072021
@@ -2156,14 +2170,18 @@ mod test {
21562170 assert_eq ! ( result. get_ref( ) , & v) ;
21572171 }
21582172
2173+ #[ cfg( feature = "deflate-flate2" ) ]
21592174 const RT_TEST_TEXT : & str = "And I can't stop thinking about the moments that I lost to you\
21602175 And I can't stop thinking of things I used to do\
21612176 And I can't stop making bad decisions\
21622177 And I can't stop eating stuff you make me chew\
21632178 I put on a smile like you wanna see\
21642179 Another day goes by that I long to be like you";
2180+ #[ cfg( feature = "deflate-flate2" ) ]
21652181 const RT_TEST_FILENAME : & str = "subfolder/sub-subfolder/can't_stop.txt" ;
2182+ #[ cfg( feature = "deflate-flate2" ) ]
21662183 const SECOND_FILENAME : & str = "different_name.xyz" ;
2184+ #[ cfg( feature = "deflate-flate2" ) ]
21672185 const THIRD_FILENAME : & str = "third_name.xyz" ;
21682186
21692187 #[ test]
@@ -2219,6 +2237,7 @@ mod test {
22192237 }
22202238
22212239 #[ test]
2240+ #[ cfg( feature = "deflate-flate2" ) ]
22222241 fn test_shallow_copy ( ) {
22232242 let mut writer = ZipWriter :: new ( Cursor :: new ( Vec :: new ( ) ) ) ;
22242243 let options = FileOptions {
@@ -2269,6 +2288,7 @@ mod test {
22692288 }
22702289
22712290 #[ test]
2291+ #[ cfg( feature = "deflate-flate2" ) ]
22722292 fn test_deep_copy ( ) {
22732293 let mut writer = ZipWriter :: new ( Cursor :: new ( Vec :: new ( ) ) ) ;
22742294 let options = FileOptions {
@@ -2440,6 +2460,7 @@ mod test {
24402460 }
24412461
24422462 #[ test]
2463+ #[ cfg( feature = "deflate-flate2" ) ]
24432464 fn remove_shallow_copy_keeps_original ( ) -> ZipResult < ( ) > {
24442465 let mut writer = ZipWriter :: new ( Cursor :: new ( Vec :: new ( ) ) ) ;
24452466 writer
0 commit comments