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