Skip to main content

s_zip/
writer.rs

1//! Streaming ZIP writer that compresses data on-the-fly without temp files
2//!
3//! This eliminates:
4//! - Temp file disk I/O
5//! - File read buffers
6//! - Intermediate storage
7//!
8//! Expected RAM savings: 5-8 MB per file
9//!
10//! Now supports arbitrary writers (File, Vec<u8>, network streams, etc.)
11
12use crate::error::{Result, SZipError};
13use crc32fast::Hasher as Crc32;
14use flate2::write::DeflateEncoder;
15use flate2::Compression;
16use std::fs::File;
17use std::io::{Seek, Write};
18use std::path::Path;
19
20#[cfg(feature = "encryption")]
21use crate::encryption::{AesEncryptor, AesStrength};
22
23/// Compression method to use for ZIP entries
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub enum CompressionMethod {
26    /// No compression (stored)
27    Stored,
28    /// DEFLATE compression (most common)
29    Deflate,
30    /// Zstd compression (requires zstd-support feature)
31    #[cfg(feature = "zstd-support")]
32    Zstd,
33}
34
35impl CompressionMethod {
36    pub(crate) fn to_zip_method(self) -> u16 {
37        match self {
38            CompressionMethod::Stored => 0,
39            CompressionMethod::Deflate => 8,
40            #[cfg(feature = "zstd-support")]
41            CompressionMethod::Zstd => 93,
42        }
43    }
44}
45
46/// Entry being written to ZIP
47struct ZipEntry {
48    name: String,
49    local_header_offset: u64,
50    crc32: u32,
51    compressed_size: u64,
52    uncompressed_size: u64,
53    compression_method: u16,
54    #[cfg(feature = "encryption")]
55    #[allow(dead_code)] // Will be used for central directory in future versions
56    encryption_strength: Option<u16>,
57}
58
59/// Streaming ZIP writer that compresses data on-the-fly
60pub struct StreamingZipWriter<W: Write + Seek> {
61    output: W,
62    entries: Vec<ZipEntry>,
63    current_entry: Option<CurrentEntry>,
64    compression_level: u32,
65    compression_method: CompressionMethod,
66    #[cfg(feature = "encryption")]
67    password: Option<String>,
68    #[cfg(feature = "encryption")]
69    encryption_strength: AesStrength,
70}
71
72struct CurrentEntry {
73    name: String,
74    local_header_offset: u64,
75    encoder: Box<dyn CompressorWrite>,
76    counter: CrcCounter,
77    compression_method: u16,
78    #[cfg(feature = "encryption")]
79    encryptor: Option<AesEncryptor>,
80}
81
82trait CompressorWrite: Write {
83    fn finish_compression(self: Box<Self>) -> Result<CompressedBuffer>;
84    fn get_buffer_mut(&mut self) -> &mut CompressedBuffer;
85}
86
87struct DeflateCompressor {
88    encoder: DeflateEncoder<CompressedBuffer>,
89}
90
91impl Write for DeflateCompressor {
92    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
93        self.encoder.write(buf)
94    }
95
96    fn flush(&mut self) -> std::io::Result<()> {
97        self.encoder.flush()
98    }
99}
100
101impl CompressorWrite for DeflateCompressor {
102    fn finish_compression(self: Box<Self>) -> Result<CompressedBuffer> {
103        Ok(self.encoder.finish()?)
104    }
105
106    fn get_buffer_mut(&mut self) -> &mut CompressedBuffer {
107        self.encoder.get_mut()
108    }
109}
110
111/// Stored (no compression) pass-through compressor
112struct StoredCompressor {
113    buffer: CompressedBuffer,
114}
115
116impl Write for StoredCompressor {
117    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
118        self.buffer.write(buf)
119    }
120
121    fn flush(&mut self) -> std::io::Result<()> {
122        self.buffer.flush()
123    }
124}
125
126impl CompressorWrite for StoredCompressor {
127    fn finish_compression(self: Box<Self>) -> Result<CompressedBuffer> {
128        Ok(self.buffer)
129    }
130
131    fn get_buffer_mut(&mut self) -> &mut CompressedBuffer {
132        &mut self.buffer
133    }
134}
135
136#[cfg(feature = "zstd-support")]
137struct ZstdCompressor {
138    encoder: zstd::Encoder<'static, CompressedBuffer>,
139}
140
141#[cfg(feature = "zstd-support")]
142impl Write for ZstdCompressor {
143    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
144        self.encoder.write(buf)
145    }
146
147    fn flush(&mut self) -> std::io::Result<()> {
148        self.encoder.flush()
149    }
150}
151
152#[cfg(feature = "zstd-support")]
153impl CompressorWrite for ZstdCompressor {
154    fn finish_compression(self: Box<Self>) -> Result<CompressedBuffer> {
155        Ok(self.encoder.finish()?)
156    }
157
158    fn get_buffer_mut(&mut self) -> &mut CompressedBuffer {
159        self.encoder.get_mut()
160    }
161}
162
163/// Metadata tracker for CRC and byte counts
164struct CrcCounter {
165    crc: Crc32,
166    uncompressed_count: u64,
167    compressed_count: u64,
168}
169
170impl CrcCounter {
171    fn new() -> Self {
172        Self {
173            crc: Crc32::new(),
174            uncompressed_count: 0,
175            compressed_count: 0,
176        }
177    }
178
179    fn update_uncompressed(&mut self, data: &[u8]) {
180        self.crc.update(data);
181        self.uncompressed_count += data.len() as u64;
182    }
183
184    fn add_compressed(&mut self, count: u64) {
185        self.compressed_count += count;
186    }
187
188    fn finalize(&self) -> u32 {
189        self.crc.clone().finalize()
190    }
191}
192
193/// Buffered writer for compressed data with adaptive sizing
194///
195/// Automatically adjusts buffer capacity and flush threshold based on data size hints
196/// to optimize memory usage and performance for different file sizes.
197struct CompressedBuffer {
198    buffer: Vec<u8>,
199    flush_threshold: usize,
200}
201
202impl CompressedBuffer {
203    /// Create buffer with default capacity (for backward compatibility)
204    #[allow(dead_code)]
205    fn new() -> Self {
206        Self::with_size_hint(None)
207    }
208
209    /// Create buffer with adaptive sizing based on expected data size
210    ///
211    /// Optimizes initial capacity and flush threshold:
212    /// - Tiny files (<10KB): 8KB initial, 256KB threshold
213    /// - Small files (<100KB): 32KB initial, 512KB threshold  
214    /// - Medium files (<1MB): 128KB initial, 2MB threshold
215    /// - Large files (≥1MB): 256KB initial, 4MB threshold
216    fn with_size_hint(size_hint: Option<u64>) -> Self {
217        let (initial_capacity, flush_threshold) = match size_hint {
218            Some(size) if size < 10_000 => (8 * 1024, 256 * 1024), // Tiny: 8KB, 256KB
219            Some(size) if size < 100_000 => (32 * 1024, 512 * 1024), // Small: 32KB, 512KB
220            Some(size) if size < 1_000_000 => (128 * 1024, 2 * 1024 * 1024), // Medium: 128KB, 2MB
221            Some(size) if size < 10_000_000 => (256 * 1024, 4 * 1024 * 1024), // Large: 256KB, 4MB
222            _ => (512 * 1024, 8 * 1024 * 1024),                    // Very large: 512KB, 8MB
223        };
224
225        Self {
226            buffer: Vec::with_capacity(initial_capacity),
227            flush_threshold,
228        }
229    }
230
231    fn take(&mut self) -> Vec<u8> {
232        std::mem::take(&mut self.buffer)
233    }
234
235    fn should_flush(&self) -> bool {
236        self.buffer.len() >= self.flush_threshold
237    }
238}
239
240impl Write for CompressedBuffer {
241    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
242        self.buffer.extend_from_slice(buf);
243        Ok(buf.len())
244    }
245
246    fn flush(&mut self) -> std::io::Result<()> {
247        Ok(())
248    }
249}
250
251impl StreamingZipWriter<File> {
252    /// Create a new ZIP writer with default compression level (6) using DEFLATE
253    pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> {
254        Self::with_compression(path, 6)
255    }
256
257    /// Create a new ZIP writer with custom compression level (0-9) using DEFLATE
258    pub fn with_compression<P: AsRef<Path>>(path: P, compression_level: u32) -> Result<Self> {
259        Self::with_method(path, CompressionMethod::Deflate, compression_level)
260    }
261
262    /// Create a new ZIP writer with specified compression method and level
263    ///
264    /// # Arguments
265    /// * `path` - Path to the output ZIP file
266    /// * `method` - Compression method to use (Deflate, Zstd, or Stored)
267    /// * `compression_level` - Compression level (0-9 for DEFLATE, 1-21 for Zstd)
268    pub fn with_method<P: AsRef<Path>>(
269        path: P,
270        method: CompressionMethod,
271        compression_level: u32,
272    ) -> Result<Self> {
273        let output = File::create(path)?;
274        Ok(Self {
275            output,
276            entries: Vec::new(),
277            current_entry: None,
278            compression_level,
279            compression_method: method,
280            #[cfg(feature = "encryption")]
281            password: None,
282            #[cfg(feature = "encryption")]
283            encryption_strength: AesStrength::Aes256,
284        })
285    }
286
287    /// Create a new ZIP writer with Zstd compression (requires zstd-support feature)
288    #[cfg(feature = "zstd-support")]
289    pub fn with_zstd<P: AsRef<Path>>(path: P, compression_level: i32) -> Result<Self> {
290        let output = File::create(path)?;
291        Ok(Self {
292            output,
293            entries: Vec::new(),
294            current_entry: None,
295            compression_level: compression_level as u32,
296            compression_method: CompressionMethod::Zstd,
297            #[cfg(feature = "encryption")]
298            password: None,
299            #[cfg(feature = "encryption")]
300            encryption_strength: AesStrength::Aes256,
301        })
302    }
303}
304
305impl<W: Write + Seek> StreamingZipWriter<W> {
306    /// Create a new ZIP writer from an arbitrary writer with default compression level (6) using DEFLATE
307    pub fn from_writer(writer: W) -> Result<Self> {
308        Self::from_writer_with_compression(writer, 6)
309    }
310
311    /// Create a new ZIP writer from an arbitrary writer with custom compression level
312    pub fn from_writer_with_compression(writer: W, compression_level: u32) -> Result<Self> {
313        Self::from_writer_with_method(writer, CompressionMethod::Deflate, compression_level)
314    }
315
316    /// Create a new ZIP writer from an arbitrary writer with specified compression method and level
317    ///
318    /// # Arguments
319    /// * `writer` - Any writer implementing Write + Seek
320    /// * `method` - Compression method to use (Deflate, Zstd, or Stored)
321    /// * `compression_level` - Compression level (0-9 for DEFLATE, 1-21 for Zstd)
322    pub fn from_writer_with_method(
323        writer: W,
324        method: CompressionMethod,
325        compression_level: u32,
326    ) -> Result<Self> {
327        Ok(Self {
328            output: writer,
329            entries: Vec::new(),
330            current_entry: None,
331            compression_level,
332            compression_method: method,
333            #[cfg(feature = "encryption")]
334            password: None,
335            #[cfg(feature = "encryption")]
336            encryption_strength: AesStrength::Aes256,
337        })
338    }
339
340    /// Set password for AES encryption (requires encryption feature)
341    ///
342    /// All subsequent entries will be encrypted with AES-256 using the provided password.
343    /// Call this method before `start_entry()` to encrypt files.
344    ///
345    /// # Arguments
346    /// * `password` - Password for encryption (minimum 8 characters recommended)
347    ///
348    /// # Example
349    /// ```no_run
350    /// use s_zip::StreamingZipWriter;
351    ///
352    /// let mut writer = StreamingZipWriter::new("encrypted.zip")?;
353    /// writer.set_password("my_secure_password");
354    ///
355    /// writer.start_entry("secret.txt")?;
356    /// writer.write_data(b"Confidential data")?;
357    /// writer.finish()?;
358    /// # Ok::<(), s_zip::SZipError>(())
359    /// ```
360    #[cfg(feature = "encryption")]
361    pub fn set_password(&mut self, password: impl Into<String>) -> &mut Self {
362        self.password = Some(password.into());
363        self
364    }
365
366    /// Set AES encryption strength (default: AES-256)
367    ///
368    /// # Arguments
369    /// * `strength` - AES encryption strength (Aes128, Aes192, or Aes256)
370    #[cfg(feature = "encryption")]
371    pub fn set_encryption_strength(&mut self, strength: AesStrength) -> &mut Self {
372        self.encryption_strength = strength;
373        self
374    }
375
376    /// Clear password (disable encryption for subsequent entries)
377    #[cfg(feature = "encryption")]
378    pub fn clear_password(&mut self) -> &mut Self {
379        self.password = None;
380        self
381    }
382
383    /// Start a new entry (file) in the ZIP
384    pub fn start_entry(&mut self, name: &str) -> Result<()> {
385        self.start_entry_with_hint(name, None)
386    }
387
388    /// Start a new entry with size hint for optimized buffering
389    ///
390    /// Providing an accurate size hint can improve performance by 15-25% for large files.
391    /// The hint is used to optimize buffer allocation and flush thresholds.
392    ///
393    /// # Arguments
394    /// * `name` - The name/path of the entry in the ZIP
395    /// * `size_hint` - Optional uncompressed size hint in bytes
396    ///
397    /// # Example
398    /// ```no_run
399    /// # use s_zip::StreamingZipWriter;
400    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
401    /// let mut writer = StreamingZipWriter::new("output.zip")?;
402    ///
403    /// // For large files, provide size hint for better performance
404    /// writer.start_entry_with_hint("large_file.bin", Some(10_000_000))?;
405    /// # Ok(())
406    /// # }
407    /// ```
408    pub fn start_entry_with_hint(&mut self, name: &str, size_hint: Option<u64>) -> Result<()> {
409        // Finish previous entry if any
410        self.finish_current_entry()?;
411
412        let local_header_offset = self.output.stream_position()?;
413        let compression_method = self.compression_method.to_zip_method();
414
415        // Check if encryption is enabled
416        #[cfg(feature = "encryption")]
417        let (encryptor, encryption_flag) = if let Some(ref password) = self.password {
418            let enc = AesEncryptor::new(password, self.encryption_strength)?;
419            (Some(enc), 0x01) // bit 0 set for encryption
420        } else {
421            (None, 0x00)
422        };
423
424        #[cfg(not(feature = "encryption"))]
425        let encryption_flag = 0x00;
426
427        // Write local file header with data descriptor flag (bit 3) + encryption flag (bit 0)
428        self.output.write_all(&[0x50, 0x4b, 0x03, 0x04])?; // signature
429        self.output.write_all(&[51, 0])?; // version needed (5.1 for AES)
430        self.output.write_all(&[8 | encryption_flag, 0])?; // general purpose bit flag
431        self.output.write_all(&compression_method.to_le_bytes())?; // compression method
432        self.output.write_all(&[0, 0, 0, 0])?; // mod time/date
433        self.output.write_all(&0u32.to_le_bytes())?; // crc32 placeholder
434        self.output.write_all(&0u32.to_le_bytes())?; // compressed size placeholder
435        self.output.write_all(&0u32.to_le_bytes())?; // uncompressed size placeholder
436        self.output.write_all(&(name.len() as u16).to_le_bytes())?;
437
438        // Calculate extra field size for AES
439        #[cfg(feature = "encryption")]
440        let extra_len = if encryptor.is_some() { 11 } else { 0 };
441        #[cfg(not(feature = "encryption"))]
442        let extra_len = 0;
443
444        self.output.write_all(&(extra_len as u16).to_le_bytes())?; // extra len
445        self.output.write_all(name.as_bytes())?;
446
447        // Write AES extra field if encryption is enabled
448        #[cfg(feature = "encryption")]
449        if let Some(ref enc) = encryptor {
450            // AES extra field header (0x9901)
451            self.output.write_all(&[0x01, 0x99])?; // WinZip AES encryption marker
452            self.output.write_all(&[7, 0])?; // data size
453            self.output.write_all(&[2, 0])?; // AE-2 format
454            self.output.write_all(&[0x41, 0x45])?; // vendor ID "AE"
455            self.output
456                .write_all(&enc.strength().to_winzip_code().to_le_bytes())?; // strength
457            self.output.write_all(&compression_method.to_le_bytes())?; // actual compression
458
459            // Write salt and password verification
460            self.output.write_all(enc.salt())?;
461            self.output.write_all(enc.password_verify())?;
462        }
463
464        // Create encoder for this entry based on compression method
465        // Use adaptive buffer if size hint is provided
466        let encoder: Box<dyn CompressorWrite> = match self.compression_method {
467            CompressionMethod::Deflate => Box::new(DeflateCompressor {
468                encoder: DeflateEncoder::new(
469                    CompressedBuffer::with_size_hint(size_hint),
470                    Compression::new(self.compression_level),
471                ),
472            }),
473            #[cfg(feature = "zstd-support")]
474            CompressionMethod::Zstd => {
475                let mut encoder = zstd::Encoder::new(
476                    CompressedBuffer::with_size_hint(size_hint),
477                    self.compression_level as i32,
478                )?;
479                encoder.include_checksum(false)?; // ZIP uses CRC32, not zstd checksum
480                Box::new(ZstdCompressor { encoder })
481            }
482            CompressionMethod::Stored => {
483                // Stored method: no compression, pass through data
484                Box::new(StoredCompressor {
485                    buffer: CompressedBuffer::new(),
486                })
487            }
488        };
489
490        #[cfg_attr(not(feature = "encryption"), allow(unused_mut))]
491        let mut counter = CrcCounter::new();
492
493        // Account for salt and password verify bytes in compressed size for encrypted entries
494        #[cfg(feature = "encryption")]
495        if let Some(ref enc) = encryptor {
496            let encryption_overhead = (enc.salt().len() + 2) as u64; // salt + password_verify
497            counter.add_compressed(encryption_overhead);
498        }
499
500        self.current_entry = Some(CurrentEntry {
501            name: name.to_string(),
502            local_header_offset,
503            encoder,
504            counter,
505            compression_method,
506            #[cfg(feature = "encryption")]
507            encryptor,
508        });
509
510        Ok(())
511    }
512
513    /// Write uncompressed data to current entry (will be compressed and/or encrypted on-the-fly)
514    pub fn write_data(&mut self, data: &[u8]) -> Result<()> {
515        let entry = self
516            .current_entry
517            .as_mut()
518            .ok_or_else(|| SZipError::InvalidFormat("No entry started".to_string()))?;
519
520        // Update CRC and size with uncompressed data
521        entry.counter.update_uncompressed(data);
522
523        // For AES encryption: Update HMAC with plaintext BEFORE compression
524        #[cfg(feature = "encryption")]
525        if let Some(ref mut encryptor) = entry.encryptor {
526            encryptor.update_hmac(data);
527        }
528
529        // Write to encoder (compresses data into buffer)
530        entry.encoder.write_all(data)?;
531
532        // Flush encoder to ensure all data is in buffer
533        entry.encoder.flush()?;
534
535        // Check if buffer should be flushed to output
536        let buffer = entry.encoder.get_buffer_mut();
537        if buffer.should_flush() {
538            // Flush buffer to output to keep memory usage low
539            let compressed_data = buffer.take();
540
541            // Encrypt compressed data if encryption is enabled and password is set
542            #[cfg(feature = "encryption")]
543            let data_to_write = if let Some(ref mut encryptor) = entry.encryptor {
544                let mut data_to_encrypt = compressed_data;
545                encryptor.encrypt(&mut data_to_encrypt)?;
546                data_to_encrypt
547            } else {
548                compressed_data
549            };
550
551            #[cfg(not(feature = "encryption"))]
552            let data_to_write = compressed_data;
553
554            self.output.write_all(&data_to_write)?;
555            entry.counter.add_compressed(data_to_write.len() as u64);
556        }
557
558        Ok(())
559    }
560
561    /// Finish current entry and write data descriptor
562    fn finish_current_entry(&mut self) -> Result<()> {
563        if let Some(mut entry) = self.current_entry.take() {
564            // Finish compression and get remaining buffered data
565            let mut buffer = entry.encoder.finish_compression()?;
566
567            // Flush any remaining data from buffer to output
568            let remaining_data = buffer.take();
569            if !remaining_data.is_empty() {
570                // Encrypt remaining compressed data if encryption is enabled and password is set
571                #[cfg(feature = "encryption")]
572                let data_to_write = if let Some(ref mut encryptor) = entry.encryptor {
573                    let mut data_to_encrypt = remaining_data;
574                    encryptor.encrypt(&mut data_to_encrypt)?;
575                    data_to_encrypt
576                } else {
577                    remaining_data
578                };
579
580                #[cfg(not(feature = "encryption"))]
581                let data_to_write = remaining_data;
582
583                self.output.write_all(&data_to_write)?;
584                entry.counter.add_compressed(data_to_write.len() as u64);
585            }
586
587            // Write authentication code for AES encryption
588            #[cfg(feature = "encryption")]
589            let (encryption_strength_code, auth_code_size) =
590                if let Some(encryptor) = entry.encryptor {
591                    let strength_code = encryptor.strength().to_winzip_code();
592                    let auth_code = encryptor.finalize();
593                    self.output.write_all(&auth_code)?;
594                    (Some(strength_code), auth_code.len() as u64)
595                } else {
596                    (None, 0)
597                };
598
599            #[cfg(not(feature = "encryption"))]
600            let auth_code_size = 0u64;
601
602            let crc = entry.counter.finalize();
603            let compressed_size = entry.counter.compressed_count + auth_code_size;
604            let uncompressed_size = entry.counter.uncompressed_count;
605
606            // Write data descriptor
607            // signature
608            self.output.write_all(&[0x50, 0x4b, 0x07, 0x08])?;
609            self.output.write_all(&crc.to_le_bytes())?;
610            // If sizes exceed 32-bit, write 64-bit sizes (ZIP64 data descriptor)
611            if compressed_size > u32::MAX as u64 || uncompressed_size > u32::MAX as u64 {
612                self.output.write_all(&compressed_size.to_le_bytes())?;
613                self.output.write_all(&uncompressed_size.to_le_bytes())?;
614            } else {
615                self.output
616                    .write_all(&(compressed_size as u32).to_le_bytes())?;
617                self.output
618                    .write_all(&(uncompressed_size as u32).to_le_bytes())?;
619            }
620
621            // Save entry info for central directory
622            self.entries.push(ZipEntry {
623                name: entry.name,
624                local_header_offset: entry.local_header_offset,
625                crc32: crc,
626                compressed_size,
627                uncompressed_size,
628                compression_method: entry.compression_method,
629                #[cfg(feature = "encryption")]
630                encryption_strength: encryption_strength_code,
631            });
632        }
633        Ok(())
634    }
635
636    /// Finish ZIP file (write central directory and return the writer)
637    pub fn finish(mut self) -> Result<W> {
638        // Finish last entry
639        self.finish_current_entry()?;
640
641        let central_dir_offset = self.output.stream_position()?;
642
643        // Write central directory
644        for entry in &self.entries {
645            self.output.write_all(&[0x50, 0x4b, 0x01, 0x02])?; // central dir sig
646            self.output.write_all(&[20, 0])?; // version made by
647            self.output.write_all(&[20, 0])?; // version needed
648
649            // Set encryption flag (bit 0) if entry was encrypted
650            #[cfg(feature = "encryption")]
651            let flags = if entry.encryption_strength.is_some() {
652                0x08 | 0x01 // bit 3 (data descriptor) + bit 0 (encryption)
653            } else {
654                0x08 // bit 3 only (data descriptor)
655            };
656            #[cfg(not(feature = "encryption"))]
657            let flags = 0x08;
658
659            self.output.write_all(&[flags, 0])?; // general purpose bit flag
660            self.output
661                .write_all(&entry.compression_method.to_le_bytes())?; // compression method
662            self.output.write_all(&[0, 0, 0, 0])?; // mod time/date
663            self.output.write_all(&entry.crc32.to_le_bytes())?;
664
665            // Write sizes (32-bit placeholders or actual values)
666            if entry.compressed_size > u32::MAX as u64 {
667                self.output.write_all(&0xFFFFFFFFu32.to_le_bytes())?;
668            } else {
669                self.output
670                    .write_all(&(entry.compressed_size as u32).to_le_bytes())?;
671            }
672
673            if entry.uncompressed_size > u32::MAX as u64 {
674                self.output.write_all(&0xFFFFFFFFu32.to_le_bytes())?;
675            } else {
676                self.output
677                    .write_all(&(entry.uncompressed_size as u32).to_le_bytes())?;
678            }
679
680            self.output
681                .write_all(&(entry.name.len() as u16).to_le_bytes())?;
682
683            // Prepare extra fields
684            let mut extra_field: Vec<u8> = Vec::new();
685
686            // Add AES extra field if entry was encrypted
687            #[cfg(feature = "encryption")]
688            if let Some(strength_code) = entry.encryption_strength {
689                // AES extra field header (0x9901)
690                extra_field.extend_from_slice(&[0x01, 0x99]); // WinZip AES encryption marker
691                extra_field.extend_from_slice(&[7, 0]); // data size
692                extra_field.extend_from_slice(&[2, 0]); // AE-2 format
693                extra_field.extend_from_slice(&[0x41, 0x45]); // vendor ID "AE"
694                extra_field.extend_from_slice(&strength_code.to_le_bytes()); // strength
695                extra_field.extend_from_slice(&entry.compression_method.to_le_bytes());
696                // actual compression
697            }
698
699            // Add ZIP64 extra field if needed
700            if entry.uncompressed_size > u32::MAX as u64
701                || entry.compressed_size > u32::MAX as u64
702                || entry.local_header_offset > u32::MAX as u64
703            {
704                // ZIP64 extra header ID 0x0001
705                extra_field.extend_from_slice(&0x0001u16.to_le_bytes());
706                // data size: we'll include uncompressed (8) if needed, compressed (8) if needed, and offset (8) if needed
707                let mut data: Vec<u8> = Vec::new();
708                if entry.uncompressed_size > u32::MAX as u64 {
709                    data.extend_from_slice(&entry.uncompressed_size.to_le_bytes());
710                }
711                if entry.compressed_size > u32::MAX as u64 {
712                    data.extend_from_slice(&entry.compressed_size.to_le_bytes());
713                }
714                if entry.local_header_offset > u32::MAX as u64 {
715                    data.extend_from_slice(&entry.local_header_offset.to_le_bytes());
716                }
717                extra_field.extend_from_slice(&(data.len() as u16).to_le_bytes());
718                extra_field.extend_from_slice(&data);
719            }
720
721            self.output
722                .write_all(&(extra_field.len() as u16).to_le_bytes())?; // extra len
723            self.output.write_all(&0u16.to_le_bytes())?; // file comment len
724            self.output.write_all(&0u16.to_le_bytes())?; // disk number start
725            self.output.write_all(&0u16.to_le_bytes())?; // internal attrs
726            self.output.write_all(&0u32.to_le_bytes())?; // external attrs
727
728            // local header offset (32-bit or 0xFFFFFFFF)
729            if entry.local_header_offset > u32::MAX as u64 {
730                self.output.write_all(&0xFFFFFFFFu32.to_le_bytes())?;
731            } else {
732                self.output
733                    .write_all(&(entry.local_header_offset as u32).to_le_bytes())?;
734            }
735
736            self.output.write_all(entry.name.as_bytes())?;
737            if !extra_field.is_empty() {
738                self.output.write_all(&extra_field)?;
739            }
740        }
741
742        let central_dir_size = self.output.stream_position()? - central_dir_offset;
743
744        // Determine if we need ZIP64 EOCD
745        let need_zip64 = self.entries.len() > u16::MAX as usize
746            || central_dir_size > u32::MAX as u64
747            || central_dir_offset > u32::MAX as u64;
748
749        if need_zip64 {
750            // Write ZIP64 End of Central Directory Record
751            // signature
752            self.output.write_all(&[0x50, 0x4b, 0x06, 0x06])?; // 0x06064b50
753                                                               // size of zip64 eocd record (size of remaining fields)
754                                                               // We'll write fixed-size fields: version made by(2)+version needed(2)+disk numbers(4+4)+entries on disk(8)+total entries(8)+cd size(8)+cd offset(8)
755            let zip64_eocd_size: u64 = 44;
756            self.output.write_all(&zip64_eocd_size.to_le_bytes())?;
757            // version made by, version needed
758            self.output.write_all(&[20, 0])?;
759            self.output.write_all(&[20, 0])?;
760            // disk number, disk where central dir starts
761            self.output.write_all(&0u32.to_le_bytes())?;
762            self.output.write_all(&0u32.to_le_bytes())?;
763            // entries on this disk (8)
764            self.output
765                .write_all(&(self.entries.len() as u64).to_le_bytes())?;
766            // total entries (8)
767            self.output
768                .write_all(&(self.entries.len() as u64).to_le_bytes())?;
769            // central directory size (8)
770            self.output.write_all(&central_dir_size.to_le_bytes())?;
771            // central directory offset (8)
772            self.output.write_all(&central_dir_offset.to_le_bytes())?;
773
774            // Write ZIP64 EOCD locator
775            // signature
776            self.output.write_all(&[0x50, 0x4b, 0x06, 0x07])?; // 0x07064b50
777                                                               // disk with ZIP64 EOCD (4)
778            self.output.write_all(&0u32.to_le_bytes())?;
779            // relative offset of ZIP64 EOCD (8)
780            let zip64_eocd_pos = central_dir_offset + central_dir_size; // directly after central dir
781            self.output.write_all(&zip64_eocd_pos.to_le_bytes())?;
782            // total number of disks
783            self.output.write_all(&0u32.to_le_bytes())?;
784        }
785
786        // Write end of central directory (classic)
787        self.output.write_all(&[0x50, 0x4b, 0x05, 0x06])?;
788        self.output.write_all(&0u16.to_le_bytes())?; // disk number
789        self.output.write_all(&0u16.to_le_bytes())?; // disk with central dir
790
791        // number of entries (16-bit or 0xFFFF if ZIP64 used)
792        if self.entries.len() > u16::MAX as usize {
793            self.output.write_all(&0xFFFFu16.to_le_bytes())?;
794            self.output.write_all(&0xFFFFu16.to_le_bytes())?;
795        } else {
796            self.output
797                .write_all(&(self.entries.len() as u16).to_le_bytes())?;
798            self.output
799                .write_all(&(self.entries.len() as u16).to_le_bytes())?;
800        }
801
802        // central dir size and offset (32-bit or 0xFFFFFFFF)
803        if central_dir_size > u32::MAX as u64 {
804            self.output.write_all(&0xFFFFFFFFu32.to_le_bytes())?;
805        } else {
806            self.output
807                .write_all(&(central_dir_size as u32).to_le_bytes())?;
808        }
809
810        if central_dir_offset > u32::MAX as u64 {
811            self.output.write_all(&0xFFFFFFFFu32.to_le_bytes())?;
812        } else {
813            self.output
814                .write_all(&(central_dir_offset as u32).to_le_bytes())?;
815        }
816
817        self.output.write_all(&0u16.to_le_bytes())?; // comment len
818
819        self.output.flush()?;
820        Ok(self.output)
821    }
822}