exarch-core 0.2.9

Memory-safe archive extraction library with security validation
Documentation
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
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
//! ZIP archive format extraction.
//!
//! This module provides secure extraction of ZIP archives with comprehensive
//! security validation. Supported features:
//!
//! - **ZIP format** (PKZIP 2.0+)
//! - **Compression methods**: Stored, DEFLATE, DEFLATE64, BZIP2, ZSTD
//! - **Symlinks**: Via Unix extended file attributes
//! - **Central directory**: Random access to entries
//!
//! # Central Directory Structure
//!
//! Unlike TAR's linear stream, ZIP archives have a central directory at the
//! end:
//!
//! ```text
//! [File 1 Data] [File 2 Data] ... [Central Directory] [End Record]
//! ```
//!
//! This allows:
//! - Random access to any entry without scanning entire archive
//! - Metadata lookup before extraction
//! - Better zip bomb detection (know all sizes upfront)
//!
//! **Trade-off:** Requires seekable reader (`Read + Seek`).
//!
//! # Compression Support
//!
//! Each ZIP entry is independently compressed:
//!
//! | Method | Feature Flag | Typical Use |
//! |--------|--------------|-------------|
//! | Stored | (built-in) | No compression |
//! | DEFLATE | `deflate` | Standard compression (ZIP default) |
//! | DEFLATE64 | `deflate64` | Enhanced DEFLATE |
//! | BZIP2 | `bzip2` | Better compression ratio |
//! | ZSTD | `zstd` | Modern fast compression |
//!
//! Decompression is transparent during extraction.
//!
//! # Security Features
//!
//! All entries are validated through the security layer:
//!
//! - **Path traversal prevention** (rejects `../`, absolute paths)
//! - **Quota enforcement** (file size, count, total size)
//! - **Zip bomb detection** (per-entry and aggregate compression ratios)
//! - **Symlink escape detection** (symlinks must point within extraction
//!   directory)
//! - **Permission sanitization** (strips setuid/setgid bits)
//! - **Encryption rejection** (password-protected archives not supported)
//!
//! # Entry Type Support
//!
//! | Entry Type | Supported | Detection Method |
//! |------------|-----------|------------------|
//! | Regular files | ✅ Yes | Default entry type |
//! | Directories | ✅ Yes | Name ends with `/` or explicit flag |
//! | Symlinks | ✅ Yes | Unix external attributes (mode & 0o120000) |
//! | Hardlinks | ❌ No | Not part of ZIP spec |
//!
//! ## Symlink Handling
//!
//! ZIP symlinks are platform-specific:
//!
//! - **Unix**: Symlink target stored as file data, mode indicates symlink type
//! - **Windows**: No native symlink support in ZIP
//! - **Detection**: Check Unix external file attributes for `S_IFLNK` mode
//!
//! # Password-Protected Archives
//!
//! **Security Policy:** Password-protected ZIP archives are **rejected**.
//!
//! **Rationale:**
//! - No crypto dependencies (smaller attack surface)
//! - Clear security boundary (no decryption attempted)
//! - User must decrypt separately if needed
//!
//! Detection:
//! - Archive-level check in constructor
//! - Per-entry encryption flag check during extraction
//!
//! # Examples
//!
//! Basic extraction:
//!
//! ```no_run
//! use exarch_core::ExtractionOptions;
//! use exarch_core::SecurityConfig;
//! use exarch_core::formats::ZipArchive;
//! use exarch_core::formats::traits::ArchiveFormat;
//! use std::fs::File;
//! use std::path::Path;
//!
//! let file = File::open("archive.zip")?;
//! let mut archive = ZipArchive::new(file)?;
//! let report = archive.extract(
//!     Path::new("/output"),
//!     &SecurityConfig::default(),
//!     &ExtractionOptions::default(),
//! )?;
//! println!("Extracted {} files", report.files_extracted);
//! # Ok::<(), exarch_core::ExtractionError>(())
//! ```
//!
//! Custom security configuration:
//!
//! ```no_run
//! use exarch_core::ExtractionOptions;
//! use exarch_core::SecurityConfig;
//!
//! let mut config = SecurityConfig::default();
//! config.allowed.symlinks = true; // Allow symlinks
//! config.max_file_size = 100 * 1024 * 1024; // 100 MB per file
//! config.max_compression_ratio = 100.0; // Allow 100:1 compression
//! // ... extract with config
//! ```

use std::io::Read;
use std::io::Seek;
use std::path::Path;
use std::path::PathBuf;
use std::time::Instant;

use zip::ZipArchive as ZipReader;

use crate::ExtractionError;
use crate::ExtractionOptions;
use crate::ExtractionReport;
use crate::Result;
use crate::SecurityConfig;
use crate::copy::CopyBuffer;
use crate::security::EntryValidator;
use crate::security::validator::ValidatedEntryType;
use crate::types::DestDir;
use crate::types::EntryType;

use super::common;
use super::traits::ArchiveFormat;

/// ZIP archive handler with random-access extraction.
///
/// Supports:
/// - ZIP format (PKZIP 2.0+)
/// - Compression methods: stored, deflate, deflate64, bzip2, zstd
/// - Unix symlinks via extended attributes
/// - Password-protected archive detection (rejected)
///
/// # Central Directory
///
/// ZIP archives have a central directory at the end containing metadata
/// for all entries. This allows random access but requires seekable reader.
///
/// # Compression
///
/// Unlike TAR, each ZIP entry is independently compressed. This allows:
/// - Selective decompression (only extract needed files)
/// - Parallel decompression (future optimization)
/// - Better compression ratio detection for zip bombs
///
/// # Examples
///
/// ```no_run
/// use exarch_core::ExtractionOptions;
/// use exarch_core::SecurityConfig;
/// use exarch_core::formats::ZipArchive;
/// use exarch_core::formats::traits::ArchiveFormat;
/// use std::fs::File;
/// use std::path::Path;
///
/// let file = File::open("archive.zip")?;
/// let mut archive = ZipArchive::new(file)?;
/// let report = archive.extract(
///     Path::new("/output"),
///     &SecurityConfig::default(),
///     &ExtractionOptions::default(),
/// )?;
/// println!("Extracted {} files", report.files_extracted);
/// # Ok::<(), exarch_core::ExtractionError>(())
/// ```
pub struct ZipArchive<R: Read + Seek> {
    inner: ZipReader<R>,
}

impl<R: Read + Seek> ZipArchive<R> {
    /// Creates a new ZIP archive handler from a seekable reader.
    ///
    /// The reader must support both `Read` and `Seek` because ZIP archives
    /// have a central directory at the end that must be parsed first.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - File is not a valid ZIP archive
    /// - Central directory is corrupted
    /// - Archive is password-protected (rejected for security)
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use exarch_core::formats::ZipArchive;
    /// use std::fs::File;
    ///
    /// let file = File::open("archive.zip")?;
    /// let archive = ZipArchive::new(file)?;
    /// # Ok::<(), exarch_core::ExtractionError>(())
    /// ```
    pub fn new(reader: R) -> Result<Self> {
        let mut inner = ZipReader::new(reader).map_err(|e| {
            ExtractionError::InvalidArchive(format!("failed to open ZIP archive: {e}"))
        })?;

        // Detect password protection early (CRIT-003: robust check with entry limit)
        if Self::is_password_protected(&mut inner)? {
            return Err(ExtractionError::SecurityViolation {
                reason: "password-protected ZIP archives are not supported".into(),
            });
        }

        Ok(Self { inner })
    }

    /// Checks if any entry in the archive is encrypted.
    ///
    /// OPT-H003: Sampling strategy checks first 100 + middle 100 + last 100
    /// entries for large archives, providing comprehensive coverage with
    /// reduced overhead.
    fn is_password_protected(archive: &mut ZipReader<R>) -> Result<bool> {
        const SAMPLE_SIZE: usize = 100;
        let total_entries = archive.len();

        if total_entries <= SAMPLE_SIZE * 3 {
            for i in 0..total_entries {
                if Self::check_entry_encrypted(archive, i)? {
                    return Ok(true);
                }
            }
            return Ok(false);
        }

        // First 100 entries
        for i in 0..SAMPLE_SIZE {
            if Self::check_entry_encrypted(archive, i)? {
                return Ok(true);
            }
        }

        // Middle 100 entries
        let middle_start = (total_entries / 2).saturating_sub(SAMPLE_SIZE / 2);
        let middle_end = middle_start + SAMPLE_SIZE;
        for i in middle_start..middle_end.min(total_entries) {
            if Self::check_entry_encrypted(archive, i)? {
                return Ok(true);
            }
        }

        // Last 100 entries (MED-001: tail sampling catches encrypted files at end)
        let tail_start = total_entries.saturating_sub(SAMPLE_SIZE);
        if tail_start > middle_end {
            for i in tail_start..total_entries {
                if Self::check_entry_encrypted(archive, i)? {
                    return Ok(true);
                }
            }
        }

        Ok(false)
    }

    #[inline]
    fn check_entry_encrypted(archive: &mut ZipReader<R>, index: usize) -> Result<bool> {
        match archive.by_index(index) {
            Ok(file) => Ok(file.encrypted()),
            Err(e) if e.to_string().contains("Password required to decrypt file") => Ok(true),
            Err(e) => Err(ExtractionError::InvalidArchive(format!(
                "failed to check entry {index} for encryption: {e}"
            ))),
        }
    }

    /// Processes a single ZIP entry with a single `by_index()` call.
    ///
    /// Branches on entry type (directory/symlink/file) within the same
    /// borrow scope. For directories and symlinks, the zip file is
    /// explicitly dropped before calling extraction helpers. For files,
    /// the zip file remains alive through validation and is reused for
    /// data extraction.
    #[allow(clippy::too_many_arguments)]
    fn process_entry(
        &mut self,
        index: usize,
        validator: &mut EntryValidator,
        dest: &DestDir,
        report: &mut ExtractionReport,
        copy_buffer: &mut CopyBuffer,
        dir_cache: &mut common::DirCache,
        skip_duplicates: bool,
    ) -> Result<()> {
        let mut zip_file = self.inner.by_index(index).map_err(|e| {
            if e.to_string().contains("Password required to decrypt file") {
                return ExtractionError::SecurityViolation {
                    reason: "archive is password-protected.\n  Password-protected ZIP archives are not supported. Decrypt the archive externally and try again.".into(),
                };
            }
            ExtractionError::InvalidArchive(format!("failed to read entry {index}: {e}"))
        })?;

        if zip_file.encrypted() {
            return Err(ExtractionError::SecurityViolation {
                reason: format!("encrypted entry detected: {}", zip_file.name()),
            });
        }

        let path = PathBuf::from(zip_file.name());
        let (uncompressed_size, compressed_size) = ZipEntryAdapter::get_sizes(&zip_file);
        let mode = zip_file.unix_mode();

        let compression = ZipEntryAdapter::get_compression_method(&zip_file);
        if matches!(compression, CompressionMethod::Unsupported) {
            return Err(ExtractionError::SecurityViolation {
                reason: format!(
                    "unsupported compression method: {:?}",
                    zip_file.compression()
                ),
            });
        }

        if zip_file.is_dir() {
            drop(zip_file);
            let validated = validator.validate_entry(
                &path,
                &EntryType::Directory,
                uncompressed_size,
                Some(compressed_size),
                mode,
                Some(dir_cache),
            )?;
            common::create_directory(&validated, dest, report, dir_cache)?;
        } else if ZipEntryAdapter::is_symlink_from_mode(mode) {
            let target = ZipEntryAdapter::read_symlink_target(&mut zip_file)?;
            drop(zip_file);
            let entry_type = EntryType::Symlink { target };
            let validated = validator.validate_entry(
                &path,
                &entry_type,
                uncompressed_size,
                Some(compressed_size),
                mode,
                Some(dir_cache),
            )?;
            if let ValidatedEntryType::Symlink(safe_symlink) = validated.entry_type {
                common::create_symlink(&safe_symlink, dest, report, dir_cache, skip_duplicates)?;
            }
        } else {
            // File: validate BEFORE writing (security invariant preserved),
            // then extract with the same zip_file (stream still at position 0)
            let validated = validator.validate_entry(
                &path,
                &EntryType::File,
                uncompressed_size,
                Some(compressed_size),
                mode,
                Some(dir_cache),
            )?;
            Self::extract_file(
                &mut zip_file,
                &validated,
                dest,
                report,
                uncompressed_size,
                copy_buffer,
                dir_cache,
                skip_duplicates,
            )?;
        }

        Ok(())
    }

    /// Extracts a regular file to disk.
    #[allow(clippy::too_many_arguments)]
    fn extract_file(
        zip_file: &mut zip::read::ZipFile<'_, R>,
        validated: &crate::security::validator::ValidatedEntry,
        dest: &DestDir,
        report: &mut ExtractionReport,
        file_size: u64,
        copy_buffer: &mut CopyBuffer,
        dir_cache: &mut common::DirCache,
        skip_duplicates: bool,
    ) -> Result<()> {
        common::extract_file_generic(
            zip_file,
            validated,
            dest,
            report,
            Some(file_size),
            copy_buffer,
            dir_cache,
            skip_duplicates,
        )
    }
}

impl<R: Read + Seek> ArchiveFormat for ZipArchive<R> {
    fn extract(
        &mut self,
        output_dir: &Path,
        config: &SecurityConfig,
        options: &ExtractionOptions,
    ) -> Result<ExtractionReport> {
        let start = Instant::now();
        let skip_duplicates = options.skip_duplicates;

        let dest = DestDir::new_or_create(output_dir.to_path_buf())?;

        // OPT-H004: Pass references to avoid cloning
        let mut validator = EntryValidator::new(config, &dest);

        let mut report = ExtractionReport::new();

        // OPT-C002: Single copy buffer per archive instead of per-file allocation
        let mut copy_buffer = CopyBuffer::new();

        let mut dir_cache = common::DirCache::new();

        let entry_count = self.inner.len();

        for i in 0..entry_count {
            if let Err(e) = self.process_entry(
                i,
                &mut validator,
                &dest,
                &mut report,
                &mut copy_buffer,
                &mut dir_cache,
                skip_duplicates,
            ) {
                return Err(if report.total_items() > 0 {
                    ExtractionError::PartialExtraction {
                        source: Box::new(e),
                        report: std::mem::take(&mut report),
                    }
                } else {
                    e
                });
            }
        }

        report.duration = start.elapsed();

        Ok(report)
    }

    fn format_name(&self) -> &'static str {
        "zip"
    }
}

/// Adapter to convert `zip::ZipFile` metadata to internal types.
struct ZipEntryAdapter;

impl ZipEntryAdapter {
    /// Checks if an entry is a symbolic link by examining Unix mode bits.
    fn is_symlink_from_mode(mode: Option<u32>) -> bool {
        mode.is_some_and(|m| {
            const S_IFMT: u32 = 0o170_000;
            const S_IFLNK: u32 = 0o120_000;
            (m & S_IFMT) == S_IFLNK
        })
    }

    /// Reads symlink target from ZIP entry data (stored as file content).
    fn read_symlink_target<R: Read>(zip_file: &mut zip::read::ZipFile<'_, R>) -> Result<PathBuf> {
        // SECURITY: Limit to PATH_MAX (4096) to prevent unbounded allocation
        const MAX_SYMLINK_TARGET_SIZE: u64 = 4096;

        let size = zip_file.size();
        if size > MAX_SYMLINK_TARGET_SIZE {
            return Err(ExtractionError::SecurityViolation {
                reason: format!(
                    "symlink target too large: {size} bytes (max {MAX_SYMLINK_TARGET_SIZE})"
                ),
            });
        }

        // SAFETY: size has already been validated to be <= MAX_SYMLINK_TARGET_SIZE
        // (4096) which is well within usize range on all platforms
        #[allow(clippy::cast_possible_truncation)]
        let mut target_bytes = Vec::with_capacity(size as usize);
        zip_file
            .take(MAX_SYMLINK_TARGET_SIZE)
            .read_to_end(&mut target_bytes)
            .map_err(|e| {
                ExtractionError::InvalidArchive(format!("failed to read symlink target: {e}"))
            })?;

        let target_str = std::str::from_utf8(&target_bytes).map_err(|_| {
            ExtractionError::InvalidArchive("symlink target is not valid UTF-8".into())
        })?;

        Ok(PathBuf::from(target_str))
    }

    /// Gets compression method for the entry.
    fn get_compression_method<R: Read>(zip_file: &zip::read::ZipFile<'_, R>) -> CompressionMethod {
        match zip_file.compression() {
            zip::CompressionMethod::Stored => CompressionMethod::Stored,
            zip::CompressionMethod::Deflated => CompressionMethod::Deflate,
            zip::CompressionMethod::Bzip2 => CompressionMethod::Bzip2,
            zip::CompressionMethod::Zstd => CompressionMethod::Zstd,
            _ => CompressionMethod::Unsupported,
        }
    }

    /// Gets uncompressed and compressed sizes.
    fn get_sizes<R: Read>(zip_file: &zip::read::ZipFile<'_, R>) -> (u64, u64) {
        (zip_file.size(), zip_file.compressed_size())
    }
}

/// Compression methods supported by ZIP.
#[derive(Debug, Clone, Copy)]
enum CompressionMethod {
    Stored,
    Deflate,
    Bzip2,
    Zstd,
    Unsupported,
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::items_after_statements,
    clippy::uninlined_format_args,
    clippy::field_reassign_with_default
)]
mod tests {
    use super::*;
    use crate::test_utils::create_test_zip;
    use std::io::Cursor;
    use std::io::Write;
    use tempfile::TempDir;
    use zip::write::SimpleFileOptions;
    use zip::write::ZipWriter;

    #[test]
    fn test_zip_archive_new() {
        let zip_data = create_test_zip(vec![]);
        let cursor = Cursor::new(zip_data);
        let archive = ZipArchive::new(cursor).unwrap();
        assert_eq!(archive.format_name(), "zip");
    }

    #[test]
    fn test_extract_empty_archive() {
        let zip_data = create_test_zip(vec![]);
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert_eq!(report.files_extracted, 0);
        assert_eq!(report.directories_created, 0);
    }

    #[test]
    fn test_extract_simple_file() {
        let zip_data = create_test_zip(vec![("file.txt", b"hello world")]);
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert_eq!(report.files_extracted, 1);
        assert!(temp.path().join("file.txt").exists());

        let content = std::fs::read_to_string(temp.path().join("file.txt")).unwrap();
        assert_eq!(content, "hello world");
    }

    #[test]
    fn test_extract_multiple_files() {
        let zip_data = create_test_zip(vec![
            ("file1.txt", b"content1"),
            ("file2.txt", b"content2"),
            ("file3.txt", b"content3"),
        ]);
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert_eq!(report.files_extracted, 3);
    }

    #[test]
    fn test_extract_nested_structure() {
        let zip_data = create_test_zip(vec![("dir1/dir2/file.txt", b"nested")]);
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert_eq!(report.files_extracted, 1);
        assert!(temp.path().join("dir1/dir2/file.txt").exists());
    }

    #[test]
    fn test_extract_with_deflate_compression() {
        let buffer = Vec::new();
        let mut zip = ZipWriter::new(Cursor::new(buffer));

        let options =
            SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated);

        zip.start_file("compressed.txt", options).unwrap();
        zip.write_all(b"This text will be compressed with DEFLATE")
            .unwrap();

        let zip_data = zip.finish().unwrap().into_inner();
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert_eq!(report.files_extracted, 1);

        let content = std::fs::read_to_string(temp.path().join("compressed.txt")).unwrap();
        assert_eq!(content, "This text will be compressed with DEFLATE");
    }

    #[test]
    fn test_extract_with_bzip2_compression() {
        let buffer = Vec::new();
        let mut zip = ZipWriter::new(Cursor::new(buffer));

        let options =
            SimpleFileOptions::default().compression_method(zip::CompressionMethod::Bzip2);

        zip.start_file("bzip2.txt", options).unwrap();
        zip.write_all(b"This text will be compressed with BZIP2")
            .unwrap();

        let zip_data = zip.finish().unwrap().into_inner();
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert_eq!(report.files_extracted, 1);
    }

    #[test]
    fn test_extract_with_zstd_compression() {
        let buffer = Vec::new();
        let mut zip = ZipWriter::new(Cursor::new(buffer));

        let options = SimpleFileOptions::default().compression_method(zip::CompressionMethod::Zstd);

        zip.start_file("zstd.txt", options).unwrap();
        zip.write_all(b"This text will be compressed with ZSTD")
            .unwrap();

        let zip_data = zip.finish().unwrap().into_inner();
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert_eq!(report.files_extracted, 1);
    }

    #[test]
    fn test_extract_directory_entry() {
        let buffer = Vec::new();
        let mut zip = ZipWriter::new(Cursor::new(buffer));

        // ZIP directories end with '/'
        let options = SimpleFileOptions::default();
        zip.add_directory("mydir/", options).unwrap();

        let zip_data = zip.finish().unwrap().into_inner();
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert_eq!(report.directories_created, 1);
        assert!(temp.path().join("mydir").is_dir());
    }

    #[test]
    fn test_extract_empty_file() {
        let zip_data = create_test_zip(vec![("empty.txt", b"")]);
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert_eq!(report.files_extracted, 1);
        assert!(temp.path().join("empty.txt").exists());

        let metadata = std::fs::metadata(temp.path().join("empty.txt")).unwrap();
        assert_eq!(metadata.len(), 0);
    }

    #[test]
    fn test_quota_file_size_exceeded() {
        let zip_data = create_test_zip(vec![("large.bin", &vec![0u8; 1000])]);
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let mut config = SecurityConfig::default();
        config.max_file_size = 100; // Only allow 100 bytes

        let result = archive.extract(temp.path(), &config, &ExtractionOptions::default());

        assert!(result.is_err());
    }

    #[test]
    fn test_quota_file_count_exceeded() {
        let zip_data = create_test_zip(vec![
            ("file1.txt", b"data"),
            ("file2.txt", b"data"),
            ("file3.txt", b"data"),
        ]);
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let mut config = SecurityConfig::default();
        config.max_file_count = 2; // Only allow 2 files

        let result = archive.extract(temp.path(), &config, &ExtractionOptions::default());

        assert!(result.is_err());
    }

    #[test]
    fn test_path_traversal_rejected() {
        let zip_data = create_test_zip(vec![("../etc/passwd", b"malicious")]);
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let result = archive.extract(temp.path(), &config, &ExtractionOptions::default());

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            ExtractionError::PathTraversal { .. }
        ));
    }

    #[test]
    fn test_absolute_path_rejected() {
        let zip_data = create_test_zip(vec![("/etc/shadow", b"malicious")]);
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let result = archive.extract(temp.path(), &config, &ExtractionOptions::default());

        assert!(result.is_err());
    }

    #[test]
    fn test_zip_bomb_detection() {
        // Create a highly compressed file
        let buffer = Vec::new();
        let mut zip = ZipWriter::new(Cursor::new(buffer));

        let options =
            SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated);

        zip.start_file("bomb.txt", options).unwrap();
        // Write highly compressible data
        zip.write_all(&vec![0u8; 100_000]).unwrap();

        let zip_data = zip.finish().unwrap().into_inner();
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let mut config = SecurityConfig::default();
        config.max_compression_ratio = 10.0; // Low threshold for testing

        let result = archive.extract(temp.path(), &config, &ExtractionOptions::default());

        // Should fail with ZipBomb error
        assert!(result.is_err());
    }

    #[test]
    #[cfg(unix)]
    fn test_file_permissions_preserved() {
        use std::os::unix::fs::PermissionsExt;

        let buffer = Vec::new();
        let mut zip = ZipWriter::new(Cursor::new(buffer));

        let options = SimpleFileOptions::default().unix_permissions(0o755);
        zip.start_file("script.sh", options).unwrap();
        zip.write_all(b"#!/bin/sh\n").unwrap();

        let zip_data = zip.finish().unwrap().into_inner();
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert_eq!(report.files_extracted, 1);

        let metadata = std::fs::metadata(temp.path().join("script.sh")).unwrap();
        let permissions = metadata.permissions();
        assert_eq!(permissions.mode() & 0o777, 0o755);
    }

    #[test]
    #[cfg(unix)]
    fn test_permissions_sanitized_setuid_removed() {
        use std::os::unix::fs::PermissionsExt;

        let buffer = Vec::new();
        let mut zip = ZipWriter::new(Cursor::new(buffer));

        let options = SimpleFileOptions::default().unix_permissions(0o4755); // setuid
        zip.start_file("binary", options).unwrap();
        zip.write_all(b"data").unwrap();

        let zip_data = zip.finish().unwrap().into_inner();
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let _report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        let metadata = std::fs::metadata(temp.path().join("binary")).unwrap();
        let permissions = metadata.permissions();
        // setuid bit should be stripped
        assert_eq!(permissions.mode() & 0o7777, 0o755);
    }

    #[test]
    #[cfg(unix)]
    fn test_permissions_sanitized_setgid_removed() {
        use std::os::unix::fs::PermissionsExt;

        // MED-003: Test setgid bit removal
        let buffer = Vec::new();
        let mut zip = ZipWriter::new(Cursor::new(buffer));

        let options = SimpleFileOptions::default().unix_permissions(0o2755); // setgid
        zip.start_file("binary", options).unwrap();
        zip.write_all(b"data").unwrap();

        let zip_data = zip.finish().unwrap().into_inner();
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let _report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        let metadata = std::fs::metadata(temp.path().join("binary")).unwrap();
        let permissions = metadata.permissions();
        // setgid bit should be stripped
        assert_eq!(permissions.mode() & 0o7777, 0o755);
    }

    #[test]
    #[cfg(unix)]
    fn test_permissions_sanitized_setuid_setgid_removed() {
        use std::os::unix::fs::PermissionsExt;

        // MED-003: Test both setuid and setgid bit removal
        let buffer = Vec::new();
        let mut zip = ZipWriter::new(Cursor::new(buffer));

        let options = SimpleFileOptions::default().unix_permissions(0o6755); // setuid + setgid
        zip.start_file("binary", options).unwrap();
        zip.write_all(b"data").unwrap();

        let zip_data = zip.finish().unwrap().into_inner();
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let _report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        let metadata = std::fs::metadata(temp.path().join("binary")).unwrap();
        let permissions = metadata.permissions();
        // Both setuid and setgid bits should be stripped
        assert_eq!(permissions.mode() & 0o7777, 0o755);
    }

    // CRIT-007/CRIT-008: Symlink test requires proper ZIP creation
    // The zip crate's unix_permissions() method does not preserve file type bits
    // when writing to ZIP archives. It stores mode 0o120777 as 0o100777.
    // This is a limitation of the zip crate's API, not our extraction logic.
    // Our symlink detection code is correct and will work with real ZIP files
    // created by standard tools (like Info-ZIP, 7-Zip, etc.)
    //
    // TODO: Find proper way to create symlink entries with zip crate or use
    // a different library for testing
    #[test]
    #[cfg(unix)]
    #[ignore = "zip crate does not preserve file type bits in unix_permissions()"]
    fn test_extract_symlink_via_unix_attributes() {
        let buffer = Vec::new();
        let mut zip = ZipWriter::new(Cursor::new(buffer));

        // Create target file
        let options = SimpleFileOptions::default().unix_permissions(0o644);
        zip.start_file("target.txt", options).unwrap();
        zip.write_all(b"data").unwrap();

        // CRIT-007/CRIT-008 FIX: Create symlink entry with proper Unix mode
        // Symlink: mode = 0o120777 (S_IFLNK | 0o777)
        // The zip crate stores unix_permissions in the external file attributes
        const S_IFLNK: u32 = 0o120_000; // Symlink file type
        let symlink_mode = S_IFLNK | 0o777; // Full rwx permissions for symlink

        let options = SimpleFileOptions::default().unix_permissions(symlink_mode);
        zip.start_file("link.txt", options).unwrap();
        zip.write_all(b"target.txt").unwrap(); // Target stored as content

        let zip_data = zip.finish().unwrap().into_inner();
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let mut config = SecurityConfig::default();
        config.allowed.symlinks = true;

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert_eq!(report.files_extracted, 1, "should have 1 regular file");
        assert_eq!(report.symlinks_created, 1, "should have 1 symlink");

        // Verify symlink exists
        let link_path = temp.path().join("link.txt");
        assert!(link_path.exists(), "symlink should exist");

        // Verify it's actually a symlink
        let metadata = std::fs::symlink_metadata(&link_path).unwrap();
        assert!(metadata.is_symlink(), "link.txt should be a symlink");
    }

    // CRIT-007: See comment above - same issue with zip crate
    #[test]
    #[cfg(unix)]
    #[ignore = "zip crate does not preserve file type bits in unix_permissions()"]
    fn test_symlink_disabled_by_default() {
        let buffer = Vec::new();
        let mut zip = ZipWriter::new(Cursor::new(buffer));

        // CRIT-007 FIX: Create symlink entry with proper Unix mode
        const S_IFLNK: u32 = 0o120_000;
        let symlink_mode = S_IFLNK | 0o777;

        let options = SimpleFileOptions::default().unix_permissions(symlink_mode);
        zip.start_file("link.txt", options).unwrap();
        zip.write_all(b"target.txt").unwrap();

        let zip_data = zip.finish().unwrap().into_inner();
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default(); // symlinks disabled by default

        let result = archive.extract(temp.path(), &config, &ExtractionOptions::default());

        // Should fail because symlinks are not allowed
        assert!(
            result.is_err(),
            "extraction should fail when symlinks are disabled"
        );

        // Verify it's a SecurityViolation error
        match result {
            Err(ExtractionError::SecurityViolation { reason }) => {
                assert!(
                    reason.contains("symlinks not allowed") || reason.contains("symlink"),
                    "error should mention symlinks: {reason}"
                );
            }
            Err(other) => panic!("expected SecurityViolation, got: {other:?}"),
            Ok(_) => panic!("expected error, got success"),
        }
    }

    // Debug test showing zip crate limitation
    #[test]
    #[cfg(unix)]
    #[ignore = "debug test showing zip crate limitation"]
    fn test_debug_zip_unix_mode() {
        // Debug test to understand how unix_permissions() works
        let buffer = Vec::new();
        let mut zip = ZipWriter::new(Cursor::new(buffer));

        const S_IFLNK: u32 = 0o120_000;
        let symlink_mode = S_IFLNK | 0o777;

        let options = SimpleFileOptions::default().unix_permissions(symlink_mode);
        zip.start_file("link.txt", options).unwrap();
        zip.write_all(b"target.txt").unwrap();

        let zip_data = zip.finish().unwrap().into_inner();

        // Read it back
        let mut reader = zip::ZipArchive::new(Cursor::new(zip_data)).unwrap();
        let file = reader.by_index(0).unwrap();

        if let Some(mode) = file.unix_mode() {
            eprintln!("Mode retrieved: {:o} (decimal: {})", mode, mode);
            eprintln!("Expected symlink mode: {:o}", symlink_mode);

            const S_IFMT: u32 = 0o170_000;
            const S_IFLNK_CHECK: u32 = 0o120_000;
            eprintln!("File type bits: {:o}", mode & S_IFMT);
            eprintln!("Is symlink: {}", (mode & S_IFMT) == S_IFLNK_CHECK);
        } else {
            panic!("No Unix mode set!");
        }
    }

    #[test]
    fn test_hardlink_rejected() {
        // HIGH-011: ZIP doesn't have native hardlink support
        // This test verifies that hardlink entries are rejected at the format level

        // ZIP format doesn't support hardlinks in the spec
        // If an entry has the hardlink type in ValidatedEntryType, it should be
        // rejected

        // Create a minimal test to verify the hardlink rejection path exists
        let zip_data = create_test_zip(vec![("file.txt", b"content")]);
        let cursor = Cursor::new(zip_data);
        let archive = ZipArchive::new(cursor).unwrap();

        // Verify the format is ZIP
        assert_eq!(archive.format_name(), "zip");

        // ZIP format does not support hardlinks - any hardlink entry
        // would be rejected in process_entry() ValidatedEntryType::Hardlink
        // branch The rejection path is tested implicitly by the type
        // system (ZIP entries can only be File, Directory, or Symlink,
        // never Hardlink)
    }

    #[test]
    fn test_compression_method_detection() {
        // Test that different compression methods are detected correctly
        let buffer = Vec::new();
        let mut zip = ZipWriter::new(Cursor::new(buffer));

        let stored =
            SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
        zip.start_file("stored.txt", stored).unwrap();
        zip.write_all(b"stored").unwrap();

        let deflated =
            SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated);
        zip.start_file("deflated.txt", deflated).unwrap();
        zip.write_all(b"deflated").unwrap();

        let zip_data = zip.finish().unwrap().into_inner();
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert_eq!(report.files_extracted, 2);
    }

    #[test]
    fn test_bytes_written_tracking() {
        let zip_data = create_test_zip(vec![
            ("file1.txt", b"hello"),    // 5 bytes
            ("file2.txt", b"world!!!"), // 8 bytes
        ]);
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert_eq!(report.bytes_written, 13);
    }

    #[test]
    fn test_duration_tracking() {
        let zip_data = create_test_zip(vec![("file.txt", b"data")]);
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        // Duration should be non-zero
        assert!(report.duration.as_nanos() > 0);
    }

    #[test]
    fn test_invalid_zip_archive() {
        let invalid_data = b"not a zip file";
        let cursor = Cursor::new(invalid_data);
        let result = ZipArchive::new(cursor);

        assert!(result.is_err());
    }

    #[test]
    fn test_entry_type_detection_file() {
        let zip_data = create_test_zip(vec![("regular.txt", b"content")]);
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert_eq!(report.files_extracted, 1);
        assert_eq!(report.directories_created, 0);
        assert_eq!(report.symlinks_created, 0);
    }

    #[test]
    fn test_entry_type_detection_directory() {
        let buffer = Vec::new();
        let mut zip = ZipWriter::new(Cursor::new(buffer));

        let options = SimpleFileOptions::default();
        zip.add_directory("testdir/", options).unwrap();

        let zip_data = zip.finish().unwrap().into_inner();
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert_eq!(report.files_extracted, 0);
        assert_eq!(report.directories_created, 1);
    }

    #[test]
    fn test_nested_directories_created_automatically() {
        // ZIP might not have explicit directory entries
        // Parent dirs should be created automatically
        let zip_data = create_test_zip(vec![("a/b/c/file.txt", b"nested")]);
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let _report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert!(temp.path().join("a/b/c/file.txt").exists());
        assert!(temp.path().join("a").is_dir());
        assert!(temp.path().join("a/b").is_dir());
        assert!(temp.path().join("a/b/c").is_dir());
    }

    #[test]
    fn test_large_file_extraction() {
        // Test with a 1MB file
        let large_data = vec![0xAB; 1024 * 1024];
        let zip_data = create_test_zip(vec![("large.bin", &large_data)]);
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert_eq!(report.files_extracted, 1);

        let extracted = std::fs::read(temp.path().join("large.bin")).unwrap();
        assert_eq!(extracted.len(), 1024 * 1024);
    }

    #[test]
    fn test_many_files_extraction() {
        // Test with 100 files
        let entries: Vec<_> = (0..100)
            .map(|i| (format!("file{i}.txt"), format!("content{i}").into_bytes()))
            .collect();

        let buffer = Vec::new();
        let mut zip = ZipWriter::new(Cursor::new(buffer));

        for (name, data) in &entries {
            let options = SimpleFileOptions::default();
            zip.start_file(name, options).unwrap();
            zip.write_all(data).unwrap();
        }

        let zip_data = zip.finish().unwrap().into_inner();
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert_eq!(report.files_extracted, 100);
    }

    #[test]
    fn test_quota_total_size_exceeded() {
        let zip_data = create_test_zip(vec![
            ("file1.txt", &vec![0u8; 600]),
            ("file2.txt", &vec![0u8; 600]),
        ]);
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let mut config = SecurityConfig::default();
        config.max_total_size = 1000; // Total limit 1000 bytes

        let result = archive.extract(temp.path(), &config, &ExtractionOptions::default());

        assert!(result.is_err());
    }

    #[test]
    fn test_special_characters_in_filename() {
        let zip_data = create_test_zip(vec![
            ("file with spaces.txt", b"content"),
            ("file-with-dashes.txt", b"content"),
            ("file_with_underscores.txt", b"content"),
        ]);
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();

        let report = archive
            .extract(temp.path(), &config, &ExtractionOptions::default())
            .unwrap();

        assert_eq!(report.files_extracted, 3);
        assert!(temp.path().join("file with spaces.txt").exists());
    }

    #[test]
    fn test_is_symlink_from_mode() {
        assert!(ZipEntryAdapter::is_symlink_from_mode(Some(0o120_777)));
        assert!(ZipEntryAdapter::is_symlink_from_mode(Some(0o120_755)));
        assert!(!ZipEntryAdapter::is_symlink_from_mode(Some(0o100_644)));
        assert!(!ZipEntryAdapter::is_symlink_from_mode(Some(0o040_755)));
        assert!(!ZipEntryAdapter::is_symlink_from_mode(Some(0o755)));
        assert!(!ZipEntryAdapter::is_symlink_from_mode(None));
    }

    /// Builds a single-entry ZIP in memory with a custom compression method
    /// field, flags, unix mode, and content. CRC32 must be correct for
    /// non-empty content when using Stored method (method=0); pass 0 for
    /// empty content.
    #[allow(clippy::cast_possible_truncation)]
    fn raw_zip_with_custom_entry(
        filename: &str,
        content: &[u8],
        compression_method: u16,
        flags: u16,
        unix_mode: u32,
    ) -> Vec<u8> {
        let crc = crc32_ieee(content);
        let external_attributes = unix_mode << 16;
        let name_bytes = filename.as_bytes();
        let name_len = name_bytes.len() as u16;
        let content_len = content.len() as u32;

        let mut buf: Vec<u8> = Vec::new();

        let local_offset = buf.len() as u32;
        buf.extend_from_slice(b"PK\x03\x04");
        buf.extend_from_slice(&20u16.to_le_bytes()); // version needed
        buf.extend_from_slice(&flags.to_le_bytes());
        buf.extend_from_slice(&compression_method.to_le_bytes());
        buf.extend_from_slice(&0u16.to_le_bytes()); // mod time
        buf.extend_from_slice(&0u16.to_le_bytes()); // mod date
        buf.extend_from_slice(&crc.to_le_bytes());
        buf.extend_from_slice(&content_len.to_le_bytes()); // compressed size
        buf.extend_from_slice(&content_len.to_le_bytes()); // uncompressed size
        buf.extend_from_slice(&name_len.to_le_bytes());
        buf.extend_from_slice(&0u16.to_le_bytes()); // extra field length
        buf.extend_from_slice(name_bytes);
        buf.extend_from_slice(content);

        let central_offset = buf.len() as u32;
        buf.extend_from_slice(b"PK\x01\x02");
        buf.extend_from_slice(&0x031eu16.to_le_bytes()); // version made by: Unix
        buf.extend_from_slice(&20u16.to_le_bytes()); // version needed
        buf.extend_from_slice(&flags.to_le_bytes());
        buf.extend_from_slice(&compression_method.to_le_bytes());
        buf.extend_from_slice(&0u16.to_le_bytes()); // mod time
        buf.extend_from_slice(&0u16.to_le_bytes()); // mod date
        buf.extend_from_slice(&crc.to_le_bytes());
        buf.extend_from_slice(&content_len.to_le_bytes()); // compressed size
        buf.extend_from_slice(&content_len.to_le_bytes()); // uncompressed size
        buf.extend_from_slice(&name_len.to_le_bytes());
        buf.extend_from_slice(&0u16.to_le_bytes()); // extra length
        buf.extend_from_slice(&0u16.to_le_bytes()); // comment length
        buf.extend_from_slice(&0u16.to_le_bytes()); // disk number start
        buf.extend_from_slice(&0u16.to_le_bytes()); // internal attributes
        buf.extend_from_slice(&external_attributes.to_le_bytes());
        buf.extend_from_slice(&local_offset.to_le_bytes());
        buf.extend_from_slice(name_bytes);

        let central_size = (buf.len() as u32) - central_offset;
        buf.extend_from_slice(b"PK\x05\x06");
        buf.extend_from_slice(&0u16.to_le_bytes()); // disk number
        buf.extend_from_slice(&0u16.to_le_bytes()); // disk with central dir
        buf.extend_from_slice(&1u16.to_le_bytes()); // entries on this disk
        buf.extend_from_slice(&1u16.to_le_bytes()); // total entries
        buf.extend_from_slice(&central_size.to_le_bytes());
        buf.extend_from_slice(&central_offset.to_le_bytes());
        buf.extend_from_slice(&0u16.to_le_bytes()); // comment length
        buf
    }

    /// CRC32 (IEEE 802.3 polynomial) implementation for test use.
    fn crc32_ieee(data: &[u8]) -> u32 {
        let mut crc: u32 = 0xFFFF_FFFF;
        for &byte in data {
            crc ^= u32::from(byte);
            for _ in 0..8 {
                if crc & 1 != 0 {
                    crc = (crc >> 1) ^ 0xEDB8_8320;
                } else {
                    crc >>= 1;
                }
            }
        }
        !crc
    }

    #[test]
    fn test_unsupported_compression_method_rejected() {
        // Build a ZIP with compression method=99 (unknown/unsupported)
        // The zip crate parses entries with unknown methods but fails on decompression.
        // Our code checks the compression method in process_entry before decompressing.
        let zip_bytes = raw_zip_with_custom_entry("file.txt", b"", 99, 0, 0o100_644);
        let cursor = Cursor::new(zip_bytes);
        let result = ZipArchive::new(cursor);
        if let Ok(mut archive) = result {
            let temp = TempDir::new().unwrap();
            let config = SecurityConfig::default();
            let err = archive
                .extract(temp.path(), &config, &ExtractionOptions::default())
                .unwrap_err();
            assert!(
                matches!(err, ExtractionError::SecurityViolation { .. }),
                "expected SecurityViolation for unsupported compression, got: {err:?}"
            );
        }
        // If zip crate rejects at parse time, that's also acceptable —
        // the archive never opens, so extraction is blocked either way.
    }

    #[test]
    fn test_symlink_target_too_large() {
        // Build a raw ZIP entry with symlink mode (0o120777) and >4096 bytes content.
        // The size field in the local header is what our code reads via
        // zip_file.size(). We need actual content bytes so the zip crate
        // reports the correct uncompressed size.
        let target = vec![b'a'; 4097];
        let zip_bytes = raw_zip_with_custom_entry("link", &target, 0, 0, 0o120_777);
        let cursor = Cursor::new(zip_bytes);
        // The archive itself should open fine.
        let result = ZipArchive::new(cursor);
        if let Ok(mut archive) = result {
            let temp = TempDir::new().unwrap();
            let mut config = SecurityConfig::default();
            config.allowed.symlinks = true;
            let err = archive
                .extract(temp.path(), &config, &ExtractionOptions::default())
                .unwrap_err();
            assert!(
                matches!(err, ExtractionError::SecurityViolation { ref reason } if reason.contains("symlink target too large")),
                "expected SecurityViolation(symlink target too large), got: {err:?}"
            );
        }
    }

    #[test]
    fn test_symlink_target_invalid_utf8() {
        // Build a raw ZIP entry with symlink mode and non-UTF-8 content.
        let invalid_utf8 = vec![0xFF, 0xFE, 0x00];
        let zip_bytes = raw_zip_with_custom_entry("link", &invalid_utf8, 0, 0, 0o120_777);
        let cursor = Cursor::new(zip_bytes);
        let result = ZipArchive::new(cursor);
        if let Ok(mut archive) = result {
            let temp = TempDir::new().unwrap();
            let mut config = SecurityConfig::default();
            config.allowed.symlinks = true;
            let err = archive
                .extract(temp.path(), &config, &ExtractionOptions::default())
                .unwrap_err();
            assert!(
                matches!(err, ExtractionError::InvalidArchive(ref msg) if msg.contains("UTF-8")),
                "expected InvalidArchive(UTF-8), got: {err:?}"
            );
        }
    }

    /// Creates a 400-entry ZIP archive in memory. The entry at
    /// `encrypted_index` is encrypted using deprecated `ZipCrypto`. All other
    /// entries are unencrypted with Stored compression and 1-byte content,
    /// to keep construction fast.
    fn create_large_archive_with_encrypted_entry(encrypted_index: usize) -> Vec<u8> {
        use zip::unstable::write::FileOptionsExt;

        let total = 400usize;
        let buffer = Vec::new();
        let mut zip = ZipWriter::new(Cursor::new(buffer));

        for i in 0..total {
            let options = if i == encrypted_index {
                SimpleFileOptions::default()
                    .compression_method(zip::CompressionMethod::Stored)
                    .with_deprecated_encryption(b"pass")
                    .unwrap()
            } else {
                SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored)
            };
            zip.start_file(format!("file{i}.txt"), options).unwrap();
            zip.write_all(b"x").unwrap();
        }

        zip.finish().unwrap().into_inner()
    }

    #[test]
    fn test_password_protected_large_archive_first_entry() {
        // Encrypted entry at index 0 — caught by first-100 sampling.
        let zip_data = create_large_archive_with_encrypted_entry(0);
        let cursor = Cursor::new(zip_data);
        let result = ZipArchive::new(cursor);
        assert!(
            matches!(result, Err(ExtractionError::SecurityViolation { .. })),
            "expected SecurityViolation for encrypted entry in first batch"
        );
    }

    #[test]
    fn test_password_protected_large_archive_middle_entry() {
        // For 400 entries: middle_start = 200 - 50 = 150, middle_end = 250.
        // An encrypted entry at index 200 is within 150..250 — caught by middle
        // sampling.
        let zip_data = create_large_archive_with_encrypted_entry(200);
        let cursor = Cursor::new(zip_data);
        let result = ZipArchive::new(cursor);
        assert!(
            matches!(result, Err(ExtractionError::SecurityViolation { .. })),
            "expected SecurityViolation for encrypted entry in middle batch"
        );
    }

    #[test]
    fn test_password_protected_large_archive_last_entry() {
        // Encrypted entry at index 399 — caught by last-100 sampling
        // (tail_start=300..400).
        let zip_data = create_large_archive_with_encrypted_entry(399);
        let cursor = Cursor::new(zip_data);
        let result = ZipArchive::new(cursor);
        assert!(
            matches!(result, Err(ExtractionError::SecurityViolation { .. })),
            "expected SecurityViolation for encrypted entry in last batch"
        );
    }

    #[test]
    fn test_large_archive_no_encryption_passes_constructor() {
        // 400-entry unencrypted archive — constructor should succeed.
        let buffer = Vec::new();
        let mut zip = ZipWriter::new(Cursor::new(buffer));
        let options =
            SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
        for i in 0..400usize {
            zip.start_file(format!("file{i}.txt"), options).unwrap();
            zip.write_all(b"x").unwrap();
        }
        let zip_data = zip.finish().unwrap().into_inner();
        let cursor = Cursor::new(zip_data);
        let archive = ZipArchive::new(cursor);
        assert!(
            archive.is_ok(),
            "unencrypted 400-entry archive should open fine"
        );
    }

    #[test]
    fn test_per_entry_encrypted_check_catches_missed_by_sampling() {
        // For 400 entries: first=0..100, middle=150..250, last=300..400.
        // An encrypted entry at index 125 is in the gap (100..150) and is missed
        // by is_password_protected sampling, but caught by the per-entry check
        // in process_entry.
        // Entries 0..125 are unencrypted and extracted successfully before the
        // encrypted entry is hit, so the error is wrapped in PartialExtraction.
        let zip_data = create_large_archive_with_encrypted_entry(125);
        let cursor = Cursor::new(zip_data);
        let result = ZipArchive::new(cursor);
        // The constructor MAY or MAY NOT catch it depending on sampling bounds.
        // If it opens, extraction must catch it.
        match result {
            Err(ExtractionError::SecurityViolation { .. }) => {
                // Caught by constructor sampling — acceptable
            }
            Ok(mut archive) => {
                // Missed by sampling — must be caught by per-entry check during extraction.
                // Since entries 0..125 were written first, the error is wrapped in
                // PartialExtraction. Unwrap one level to check the underlying cause.
                let temp = TempDir::new().unwrap();
                let config = SecurityConfig::default();
                let err = archive
                    .extract(temp.path(), &config, &ExtractionOptions::default())
                    .unwrap_err();
                let source = match err {
                    ExtractionError::PartialExtraction { source, .. } => *source,
                    other => other,
                };
                assert!(
                    matches!(source, ExtractionError::SecurityViolation { .. }),
                    "per-entry check must catch encrypted entry missed by sampling, got: {source:?}"
                );
            }
            Err(other) => panic!("unexpected error: {other:?}"),
        }
    }

    /// Build a raw ZIP with two local entries sharing the same path.
    ///
    /// The `zip` crate's writer rejects duplicate filenames, so we craft bytes
    /// manually. Both local file records and both central directory entries are
    /// included so the archive is spec-valid.
    #[allow(clippy::cast_possible_truncation)]
    fn create_raw_duplicate_zip(path: &str, content1: &[u8], content2: &[u8]) -> Vec<u8> {
        let name_bytes = path.as_bytes();
        let name_len = name_bytes.len() as u16;
        let mut buf: Vec<u8> = Vec::new();

        let write_local = |buf: &mut Vec<u8>, content: &[u8]| {
            let crc = crc32_ieee(content);
            let size = content.len() as u32;
            buf.extend_from_slice(b"PK\x03\x04");
            buf.extend_from_slice(&20u16.to_le_bytes());
            buf.extend_from_slice(&0u16.to_le_bytes()); // flags
            buf.extend_from_slice(&0u16.to_le_bytes()); // stored
            buf.extend_from_slice(&0u16.to_le_bytes()); // mod time
            buf.extend_from_slice(&0u16.to_le_bytes()); // mod date
            buf.extend_from_slice(&crc.to_le_bytes());
            buf.extend_from_slice(&size.to_le_bytes());
            buf.extend_from_slice(&size.to_le_bytes());
            buf.extend_from_slice(&name_len.to_le_bytes());
            buf.extend_from_slice(&0u16.to_le_bytes()); // extra
            buf.extend_from_slice(name_bytes);
            buf.extend_from_slice(content);
        };

        let offset1 = buf.len() as u32;
        write_local(&mut buf, content1);
        let offset2 = buf.len() as u32;
        write_local(&mut buf, content2);

        let write_central = |buf: &mut Vec<u8>, content: &[u8], offset: u32| {
            let crc = crc32_ieee(content);
            let size = content.len() as u32;
            buf.extend_from_slice(b"PK\x01\x02");
            buf.extend_from_slice(&0x031eu16.to_le_bytes()); // version made: Unix
            buf.extend_from_slice(&20u16.to_le_bytes());
            buf.extend_from_slice(&0u16.to_le_bytes()); // flags
            buf.extend_from_slice(&0u16.to_le_bytes()); // stored
            buf.extend_from_slice(&0u16.to_le_bytes()); // mod time
            buf.extend_from_slice(&0u16.to_le_bytes()); // mod date
            buf.extend_from_slice(&crc.to_le_bytes());
            buf.extend_from_slice(&size.to_le_bytes());
            buf.extend_from_slice(&size.to_le_bytes());
            buf.extend_from_slice(&name_len.to_le_bytes());
            buf.extend_from_slice(&0u16.to_le_bytes()); // extra
            buf.extend_from_slice(&0u16.to_le_bytes()); // comment
            buf.extend_from_slice(&0u16.to_le_bytes()); // disk start
            buf.extend_from_slice(&0u16.to_le_bytes()); // int attrs
            buf.extend_from_slice(&(0o100_644u32 << 16).to_le_bytes()); // ext attrs
            buf.extend_from_slice(&offset.to_le_bytes());
            buf.extend_from_slice(name_bytes);
        };

        let central_start = buf.len() as u32;
        write_central(&mut buf, content1, offset1);
        write_central(&mut buf, content2, offset2);
        let central_size = (buf.len() as u32) - central_start;

        buf.extend_from_slice(b"PK\x05\x06");
        buf.extend_from_slice(&0u16.to_le_bytes()); // disk
        buf.extend_from_slice(&0u16.to_le_bytes()); // disk w/ cd
        buf.extend_from_slice(&2u16.to_le_bytes()); // entries on disk
        buf.extend_from_slice(&2u16.to_le_bytes()); // total entries
        buf.extend_from_slice(&central_size.to_le_bytes());
        buf.extend_from_slice(&central_start.to_le_bytes());
        buf.extend_from_slice(&0u16.to_le_bytes()); // comment len
        buf
    }

    #[test]
    fn test_duplicate_entry_skip_default() {
        let zip_data = create_raw_duplicate_zip("legit.txt", b"first", b"second");
        let cursor = Cursor::new(zip_data);
        let mut archive = ZipArchive::new(cursor).unwrap();

        let temp = TempDir::new().unwrap();
        let config = SecurityConfig::default();
        let options = ExtractionOptions::default(); // skip_duplicates = true

        let report = archive.extract(temp.path(), &config, &options).unwrap();

        // zip crate 8.x deduplicates entries at ZipArchive::new(), so the raw
        // archive with two identical filenames appears as a single entry.
        // The skip logic is verified by the TAR tests; this test confirms the
        // ZIP extractor still succeeds without panicking on such archives.
        assert_eq!(report.files_extracted, 1);
        assert!(temp.path().join("legit.txt").exists());
    }

    #[test]
    fn test_encrypted_zip_rejected_with_security_violation() {
        use zip::unstable::write::FileOptionsExt;

        let buffer = Vec::new();
        let mut writer = ZipWriter::new(Cursor::new(buffer));
        let options = SimpleFileOptions::default()
            .with_deprecated_encryption(b"password123")
            .unwrap();
        writer.start_file("secret.txt", options).unwrap();
        writer.write_all(b"secret data").unwrap();
        let zip_data = writer.finish().unwrap().into_inner();

        let cursor = Cursor::new(zip_data);
        let result = ZipArchive::new(cursor);
        let Err(err) = result else {
            panic!("expected error for encrypted ZIP, got Ok");
        };
        match err {
            ExtractionError::SecurityViolation { reason } => {
                assert!(
                    reason.contains("password") || reason.contains("encrypted"),
                    "expected password/encryption mention in reason: {reason}"
                );
            }
            other => panic!("expected SecurityViolation, got: {other}"),
        }
    }
}