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            // Format per WinZip AE-2 spec:
452            //   ID(2) + Length(2) + Version(2) + Vendor(2) + Strength(1) + ActualCompression(2) = 7 bytes data
453            self.output.write_all(&[0x01, 0x99])?; // WinZip AES encryption marker
454            self.output.write_all(&[7, 0])?; // data size (7 bytes)
455            self.output.write_all(&[2, 0])?; // AE-2 format version
456            self.output.write_all(&[0x41, 0x45])?; // vendor ID "AE"
457            self.output
458                .write_all(&[enc.strength().to_winzip_code() as u8])?; // strength (1 byte!)
459            self.output.write_all(&compression_method.to_le_bytes())?; // actual compression (2 bytes)
460
461            // Write salt and password verification
462            self.output.write_all(enc.salt())?;
463            self.output.write_all(enc.password_verify())?;
464        }
465
466        // Create encoder for this entry based on compression method
467        // Use adaptive buffer if size hint is provided
468        let encoder: Box<dyn CompressorWrite> = match self.compression_method {
469            CompressionMethod::Deflate => Box::new(DeflateCompressor {
470                encoder: DeflateEncoder::new(
471                    CompressedBuffer::with_size_hint(size_hint),
472                    Compression::new(self.compression_level),
473                ),
474            }),
475            #[cfg(feature = "zstd-support")]
476            CompressionMethod::Zstd => {
477                let mut encoder = zstd::Encoder::new(
478                    CompressedBuffer::with_size_hint(size_hint),
479                    self.compression_level as i32,
480                )?;
481                encoder.include_checksum(false)?; // ZIP uses CRC32, not zstd checksum
482                Box::new(ZstdCompressor { encoder })
483            }
484            CompressionMethod::Stored => {
485                // Stored method: no compression, pass through data
486                Box::new(StoredCompressor {
487                    buffer: CompressedBuffer::new(),
488                })
489            }
490        };
491
492        #[cfg_attr(not(feature = "encryption"), allow(unused_mut))]
493        let mut counter = CrcCounter::new();
494
495        // Account for salt and password verify bytes in compressed size for encrypted entries
496        #[cfg(feature = "encryption")]
497        if let Some(ref enc) = encryptor {
498            let encryption_overhead = (enc.salt().len() + 2) as u64; // salt + password_verify
499            counter.add_compressed(encryption_overhead);
500        }
501
502        self.current_entry = Some(CurrentEntry {
503            name: name.to_string(),
504            local_header_offset,
505            encoder,
506            counter,
507            compression_method,
508            #[cfg(feature = "encryption")]
509            encryptor,
510        });
511
512        Ok(())
513    }
514
515    /// Write uncompressed data to current entry (will be compressed and/or encrypted on-the-fly)
516    pub fn write_data(&mut self, data: &[u8]) -> Result<()> {
517        let entry = self
518            .current_entry
519            .as_mut()
520            .ok_or_else(|| SZipError::InvalidFormat("No entry started".to_string()))?;
521
522        // Update CRC and size with uncompressed data
523        entry.counter.update_uncompressed(data);
524
525        // For AES encryption: Update HMAC with plaintext BEFORE compression
526        #[cfg(feature = "encryption")]
527        if let Some(ref mut encryptor) = entry.encryptor {
528            encryptor.update_hmac(data);
529        }
530
531        // Write to encoder (compresses data into buffer)
532        entry.encoder.write_all(data)?;
533
534        // Flush encoder to ensure all data is in buffer
535        entry.encoder.flush()?;
536
537        // Check if buffer should be flushed to output
538        let buffer = entry.encoder.get_buffer_mut();
539        if buffer.should_flush() {
540            // Flush buffer to output to keep memory usage low
541            let compressed_data = buffer.take();
542
543            // Encrypt compressed data if encryption is enabled and password is set
544            #[cfg(feature = "encryption")]
545            let data_to_write = if let Some(ref mut encryptor) = entry.encryptor {
546                let mut data_to_encrypt = compressed_data;
547                encryptor.encrypt(&mut data_to_encrypt)?;
548                data_to_encrypt
549            } else {
550                compressed_data
551            };
552
553            #[cfg(not(feature = "encryption"))]
554            let data_to_write = compressed_data;
555
556            self.output.write_all(&data_to_write)?;
557            entry.counter.add_compressed(data_to_write.len() as u64);
558        }
559
560        Ok(())
561    }
562
563    /// Finish current entry and write data descriptor
564    fn finish_current_entry(&mut self) -> Result<()> {
565        if let Some(mut entry) = self.current_entry.take() {
566            // Finish compression and get remaining buffered data
567            let mut buffer = entry.encoder.finish_compression()?;
568
569            // Flush any remaining data from buffer to output
570            let remaining_data = buffer.take();
571            if !remaining_data.is_empty() {
572                // Encrypt remaining compressed data if encryption is enabled and password is set
573                #[cfg(feature = "encryption")]
574                let data_to_write = if let Some(ref mut encryptor) = entry.encryptor {
575                    let mut data_to_encrypt = remaining_data;
576                    encryptor.encrypt(&mut data_to_encrypt)?;
577                    data_to_encrypt
578                } else {
579                    remaining_data
580                };
581
582                #[cfg(not(feature = "encryption"))]
583                let data_to_write = remaining_data;
584
585                self.output.write_all(&data_to_write)?;
586                entry.counter.add_compressed(data_to_write.len() as u64);
587            }
588
589            // Write authentication code for AES encryption
590            #[cfg(feature = "encryption")]
591            let (encryption_strength_code, auth_code_size) =
592                if let Some(encryptor) = entry.encryptor {
593                    let strength_code = encryptor.strength().to_winzip_code();
594                    let auth_code = encryptor.finalize();
595                    self.output.write_all(&auth_code)?;
596                    (Some(strength_code), auth_code.len() as u64)
597                } else {
598                    (None, 0)
599                };
600
601            #[cfg(not(feature = "encryption"))]
602            let auth_code_size = 0u64;
603
604            let crc = entry.counter.finalize();
605            let compressed_size = entry.counter.compressed_count + auth_code_size;
606            let uncompressed_size = entry.counter.uncompressed_count;
607
608            // Write data descriptor
609            // signature
610            self.output.write_all(&[0x50, 0x4b, 0x07, 0x08])?;
611            self.output.write_all(&crc.to_le_bytes())?;
612            // If sizes exceed 32-bit, write 64-bit sizes (ZIP64 data descriptor)
613            if compressed_size > u32::MAX as u64 || uncompressed_size > u32::MAX as u64 {
614                self.output.write_all(&compressed_size.to_le_bytes())?;
615                self.output.write_all(&uncompressed_size.to_le_bytes())?;
616            } else {
617                self.output
618                    .write_all(&(compressed_size as u32).to_le_bytes())?;
619                self.output
620                    .write_all(&(uncompressed_size as u32).to_le_bytes())?;
621            }
622
623            // Save entry info for central directory
624            self.entries.push(ZipEntry {
625                name: entry.name,
626                local_header_offset: entry.local_header_offset,
627                crc32: crc,
628                compressed_size,
629                uncompressed_size,
630                compression_method: entry.compression_method,
631                #[cfg(feature = "encryption")]
632                encryption_strength: encryption_strength_code,
633            });
634        }
635        Ok(())
636    }
637
638    /// Finish ZIP file (write central directory and return the writer)
639    pub fn finish(mut self) -> Result<W> {
640        // Finish last entry
641        self.finish_current_entry()?;
642
643        let central_dir_offset = self.output.stream_position()?;
644
645        // Write central directory
646        for entry in &self.entries {
647            self.output.write_all(&[0x50, 0x4b, 0x01, 0x02])?; // central dir sig
648            self.output.write_all(&[20, 0])?; // version made by
649            self.output.write_all(&[20, 0])?; // version needed
650
651            // Set encryption flag (bit 0) if entry was encrypted
652            #[cfg(feature = "encryption")]
653            let flags = if entry.encryption_strength.is_some() {
654                0x08 | 0x01 // bit 3 (data descriptor) + bit 0 (encryption)
655            } else {
656                0x08 // bit 3 only (data descriptor)
657            };
658            #[cfg(not(feature = "encryption"))]
659            let flags = 0x08;
660
661            self.output.write_all(&[flags, 0])?; // general purpose bit flag
662            self.output
663                .write_all(&entry.compression_method.to_le_bytes())?; // compression method
664            self.output.write_all(&[0, 0, 0, 0])?; // mod time/date
665            self.output.write_all(&entry.crc32.to_le_bytes())?;
666
667            // Write sizes (32-bit placeholders or actual values)
668            if entry.compressed_size > u32::MAX as u64 {
669                self.output.write_all(&0xFFFFFFFFu32.to_le_bytes())?;
670            } else {
671                self.output
672                    .write_all(&(entry.compressed_size as u32).to_le_bytes())?;
673            }
674
675            if entry.uncompressed_size > u32::MAX as u64 {
676                self.output.write_all(&0xFFFFFFFFu32.to_le_bytes())?;
677            } else {
678                self.output
679                    .write_all(&(entry.uncompressed_size as u32).to_le_bytes())?;
680            }
681
682            self.output
683                .write_all(&(entry.name.len() as u16).to_le_bytes())?;
684
685            // Prepare extra fields
686            let mut extra_field: Vec<u8> = Vec::new();
687
688            // Add AES extra field if entry was encrypted
689            #[cfg(feature = "encryption")]
690            if let Some(strength_code) = entry.encryption_strength {
691                // AES extra field header (0x9901)
692                extra_field.extend_from_slice(&[0x01, 0x99]); // WinZip AES encryption marker
693                extra_field.extend_from_slice(&[7, 0]); // data size
694                extra_field.extend_from_slice(&[2, 0]); // AE-2 format
695                extra_field.extend_from_slice(&[0x41, 0x45]); // vendor ID "AE"
696                extra_field.extend_from_slice(&strength_code.to_le_bytes()); // strength
697                extra_field.extend_from_slice(&entry.compression_method.to_le_bytes());
698                // actual compression
699            }
700
701            // Add ZIP64 extra field if needed
702            if entry.uncompressed_size > u32::MAX as u64
703                || entry.compressed_size > u32::MAX as u64
704                || entry.local_header_offset > u32::MAX as u64
705            {
706                // ZIP64 extra header ID 0x0001
707                extra_field.extend_from_slice(&0x0001u16.to_le_bytes());
708                // data size: we'll include uncompressed (8) if needed, compressed (8) if needed, and offset (8) if needed
709                let mut data: Vec<u8> = Vec::new();
710                if entry.uncompressed_size > u32::MAX as u64 {
711                    data.extend_from_slice(&entry.uncompressed_size.to_le_bytes());
712                }
713                if entry.compressed_size > u32::MAX as u64 {
714                    data.extend_from_slice(&entry.compressed_size.to_le_bytes());
715                }
716                if entry.local_header_offset > u32::MAX as u64 {
717                    data.extend_from_slice(&entry.local_header_offset.to_le_bytes());
718                }
719                extra_field.extend_from_slice(&(data.len() as u16).to_le_bytes());
720                extra_field.extend_from_slice(&data);
721            }
722
723            self.output
724                .write_all(&(extra_field.len() as u16).to_le_bytes())?; // extra len
725            self.output.write_all(&0u16.to_le_bytes())?; // file comment len
726            self.output.write_all(&0u16.to_le_bytes())?; // disk number start
727            self.output.write_all(&0u16.to_le_bytes())?; // internal attrs
728            self.output.write_all(&0u32.to_le_bytes())?; // external attrs
729
730            // local header offset (32-bit or 0xFFFFFFFF)
731            if entry.local_header_offset > u32::MAX as u64 {
732                self.output.write_all(&0xFFFFFFFFu32.to_le_bytes())?;
733            } else {
734                self.output
735                    .write_all(&(entry.local_header_offset as u32).to_le_bytes())?;
736            }
737
738            self.output.write_all(entry.name.as_bytes())?;
739            if !extra_field.is_empty() {
740                self.output.write_all(&extra_field)?;
741            }
742        }
743
744        let central_dir_size = self.output.stream_position()? - central_dir_offset;
745
746        // Determine if we need ZIP64 EOCD
747        let need_zip64 = self.entries.len() > u16::MAX as usize
748            || central_dir_size > u32::MAX as u64
749            || central_dir_offset > u32::MAX as u64;
750
751        if need_zip64 {
752            // Write ZIP64 End of Central Directory Record
753            // signature
754            self.output.write_all(&[0x50, 0x4b, 0x06, 0x06])?; // 0x06064b50
755                                                               // size of zip64 eocd record (size of remaining fields)
756                                                               // 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)
757            let zip64_eocd_size: u64 = 44;
758            self.output.write_all(&zip64_eocd_size.to_le_bytes())?;
759            // version made by, version needed
760            self.output.write_all(&[20, 0])?;
761            self.output.write_all(&[20, 0])?;
762            // disk number, disk where central dir starts
763            self.output.write_all(&0u32.to_le_bytes())?;
764            self.output.write_all(&0u32.to_le_bytes())?;
765            // entries on this disk (8)
766            self.output
767                .write_all(&(self.entries.len() as u64).to_le_bytes())?;
768            // total entries (8)
769            self.output
770                .write_all(&(self.entries.len() as u64).to_le_bytes())?;
771            // central directory size (8)
772            self.output.write_all(&central_dir_size.to_le_bytes())?;
773            // central directory offset (8)
774            self.output.write_all(&central_dir_offset.to_le_bytes())?;
775
776            // Write ZIP64 EOCD locator
777            // signature
778            self.output.write_all(&[0x50, 0x4b, 0x06, 0x07])?; // 0x07064b50
779                                                               // disk with ZIP64 EOCD (4)
780            self.output.write_all(&0u32.to_le_bytes())?;
781            // relative offset of ZIP64 EOCD (8)
782            let zip64_eocd_pos = central_dir_offset + central_dir_size; // directly after central dir
783            self.output.write_all(&zip64_eocd_pos.to_le_bytes())?;
784            // total number of disks
785            self.output.write_all(&0u32.to_le_bytes())?;
786        }
787
788        // Write end of central directory (classic)
789        self.output.write_all(&[0x50, 0x4b, 0x05, 0x06])?;
790        self.output.write_all(&0u16.to_le_bytes())?; // disk number
791        self.output.write_all(&0u16.to_le_bytes())?; // disk with central dir
792
793        // number of entries (16-bit or 0xFFFF if ZIP64 used)
794        if self.entries.len() > u16::MAX as usize {
795            self.output.write_all(&0xFFFFu16.to_le_bytes())?;
796            self.output.write_all(&0xFFFFu16.to_le_bytes())?;
797        } else {
798            self.output
799                .write_all(&(self.entries.len() as u16).to_le_bytes())?;
800            self.output
801                .write_all(&(self.entries.len() as u16).to_le_bytes())?;
802        }
803
804        // central dir size and offset (32-bit or 0xFFFFFFFF)
805        if central_dir_size > u32::MAX as u64 {
806            self.output.write_all(&0xFFFFFFFFu32.to_le_bytes())?;
807        } else {
808            self.output
809                .write_all(&(central_dir_size as u32).to_le_bytes())?;
810        }
811
812        if central_dir_offset > u32::MAX as u64 {
813            self.output.write_all(&0xFFFFFFFFu32.to_le_bytes())?;
814        } else {
815            self.output
816                .write_all(&(central_dir_offset as u32).to_le_bytes())?;
817        }
818
819        self.output.write_all(&0u16.to_le_bytes())?; // comment len
820
821        self.output.flush()?;
822        Ok(self.output)
823    }
824}