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
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
//! Unified Archive API
//!
//! This module provides a unified interface for working with various archive formats.
//! It abstracts away the differences between ARC, ARJ, ZOO, SQ, SQZ, Z, HYP and other
//! archive formats, providing a simple "open → list → extract" workflow.
//!
//! # Example
//!
//! ```no_run
//! use unarc_rs::unified::ArchiveFormat;
//!
//! // Open archive directly from path
//! let mut archive = ArchiveFormat::open_path("archive.arj").unwrap();
//!
//! // Iterate over entries using the iterator
//! for entry in archive.entries_iter() {
//! let entry = entry.unwrap();
//! println!("File: {} ({} bytes)", entry.name(), entry.original_size());
//! }
//! ```
//!
//! # Opening with Options
//!
//! ```no_run
//! use unarc_rs::unified::{ArchiveFormat, ArchiveOptions};
//!
//! // Open a password-protected archive with CRC verification
//! let options = ArchiveOptions::new()
//! .with_password("secret")
//! .with_verify_crc(true);
//!
//! let mut archive = ArchiveFormat::open_path_with_options("encrypted.zip", options).unwrap();
//! ```
use std::fs::File;
use std::io::{BufReader, Read, Seek, Write};
use std::path::Path;
use std::sync::Arc;
use crate::ace::{AceArchive, FileHeader as AceFileHeader};
use crate::arc::arc_archive::ArcArchive;
use crate::arc::local_file_header::LocalFileHeader as ArcHeader;
use crate::arj::arj_archive::ArjArchive;
use crate::arj::local_file_header::LocalFileHeader as ArjHeader;
use crate::bz2::Bz2Archive;
use crate::date_time::DosDateTime;
use crate::encryption::{EncryptionMethod, RarEncryption, ZipEncryption};
use crate::error::{ArchiveError, Result};
use crate::gz::GzArchive;
use crate::ha::ha_archive::HaArchive;
use crate::ha::header::FileHeader as HaHeader;
use crate::hyp::header::Header as HypHeader;
use crate::hyp::hyp_archive::HypArchive;
use crate::ice::IceArchive;
use crate::lha::lha_archive::{LhaArchiveSeekable, LhaFileHeader};
use crate::packice::PackIceArchive;
use crate::rar::rar_archive::{RarArchive, RarFileHeader};
use crate::sevenz::sevenz_archive::{SevenZArchive, SevenZFileHeader};
use crate::sq::header::Header as SqHeader;
use crate::sq::sq_archive::SqArchive;
use crate::sqz::file_header::FileHeader as SqzFileHeader;
use crate::sqz::sqz_archive::SqzArchive;
use crate::tar::{TarArchive, TarFileHeader};
use crate::tarz::TarZArchive;
use crate::tbz::TbzArchive;
use crate::tgz::TgzArchive;
use crate::uc2::uc2_archive::{Uc2Archive, Uc2ArchiveEntry as Uc2Header};
use crate::z::ZArchive;
use crate::zip::multi_volume::MultiVolumeReader;
use crate::zip::zip_archive::{ZipArchive, ZipFileHeader};
use crate::zoo::dirent::DirectoryEntry as ZooEntry;
use crate::zoo::zoo_archive::ZooArchive;
/// Trait for providing additional volumes for multi-volume archives.
///
/// This trait is used to open subsequent volumes when extracting files
/// that span multiple archive volumes (e.g., ARJ split archives).
///
/// # Example
///
/// ```no_run
/// use std::fs::File;
/// use std::io::{BufReader, Cursor};
/// use std::path::PathBuf;
/// use unarc_rs::unified::{ArchiveOptions, VolumeProvider};
///
/// struct FileVolumeProvider {
/// base_path: PathBuf,
/// }
///
/// impl VolumeProvider for FileVolumeProvider {
/// fn open_volume(&self, volume_number: u32) -> Option<Box<dyn std::io::Read + Send>> {
/// // ARJ volumes: .arj, .a01, .a02, ...
/// let ext = if volume_number == 0 {
/// "arj".to_string()
/// } else {
/// format!("a{:02}", volume_number)
/// };
/// let path = self.base_path.with_extension(&ext);
/// println!("Opening volume: {}", path.display());
/// File::open(&path).ok().map(|f| Box::new(BufReader::new(f)) as Box<dyn std::io::Read + Send>)
/// }
/// }
/// ```
pub trait VolumeProvider: Send + Sync {
/// Opens the specified volume number and returns a reader.
///
/// Volume 0 is the first volume (the one being opened initially).
/// Returns `None` if the volume cannot be opened.
fn open_volume(&self, volume_number: u32) -> Option<Box<dyn Read + Send>>;
}
impl std::fmt::Debug for dyn VolumeProvider {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "VolumeProvider")
}
}
/// Options for opening and reading archives
///
/// `ArchiveOptions` provides a builder-style API for configuring archive operations,
/// such as setting passwords for encrypted archives or enabling CRC verification.
///
/// # Example
///
/// ```
/// use unarc_rs::unified::ArchiveOptions;
///
/// let options = ArchiveOptions::new()
/// .with_password("secret")
/// .with_verify_crc(true);
///
/// assert!(options.has_password());
/// assert!(options.verify_crc());
/// ```
#[derive(Debug, Clone, Default)]
pub struct ArchiveOptions {
/// Password for encrypted archives
password: Option<String>,
/// Whether to verify CRC checksums during extraction
verify_crc: bool,
/// Volume provider for multi-volume archives
volume_provider: Option<Arc<dyn VolumeProvider>>,
}
impl ArchiveOptions {
/// Create new default options
pub fn new() -> Self {
Self::default()
}
/// Set the password for encrypted archives
pub fn with_password<S: Into<String>>(mut self, password: S) -> Self {
self.password = Some(password.into());
self
}
/// Enable or disable CRC verification during extraction
pub fn with_verify_crc(mut self, verify: bool) -> Self {
self.verify_crc = verify;
self
}
/// Set the volume provider for multi-volume archives
pub fn with_volume_provider<V: VolumeProvider + 'static>(mut self, provider: V) -> Self {
self.volume_provider = Some(Arc::new(provider));
self
}
/// Set the volume provider using an Arc for shared ownership
pub fn with_volume_provider_arc(mut self, provider: Arc<dyn VolumeProvider>) -> Self {
self.volume_provider = Some(provider);
self
}
/// Returns the password if set
pub fn password(&self) -> Option<&str> {
self.password.as_deref()
}
/// Returns whether a password is set
pub fn has_password(&self) -> bool {
self.password.is_some()
}
/// Returns whether CRC verification is enabled
pub fn verify_crc(&self) -> bool {
self.verify_crc
}
/// Returns a reference to the volume provider if set
pub fn volume_provider(&self) -> Option<&Arc<dyn VolumeProvider>> {
self.volume_provider.as_ref()
}
}
/// Supported archive formats
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ArchiveFormat {
/// ACE archive format (.ace)
Ace,
/// ARC archive format (.arc)
Arc,
/// ARJ archive format (.arj)
Arj,
/// ZOO archive format (.zoo)
Zoo,
/// SQ/SQ2/QQQ squeezed file format (.sq, .sq2, .qqq, or ?q? pattern)
Sq,
/// SQZ (Squeeze It) archive format (.sqz)
Sqz,
/// Unix compress format (.Z)
Z,
/// Gzip single file format (.gz)
Gz,
/// Bzip2 single file format (.bz2)
Bz2,
/// ICE compressed file format (.ice) - Legacy DOS ICE
Ice,
/// Pack-Ice compressed format (.pi9, etc.) - Atari ST Pack-Ice
PackIce,
/// Hyper archive format (.hyp)
Hyp,
/// HA (Harri Archiver) format (.ha)
Ha,
/// LHA/LZH archive format (.lha, .lzh)
Lha,
/// ZIP archive format (.zip)
Zip,
/// RAR archive format (.rar) - RAR5 only
Rar,
/// 7z archive format (.7z)
SevenZ,
/// TAR archive format (.tar)
Tar,
/// TGZ (tar.gz) archive format (.tgz, .tar.gz)
Tgz,
/// TBZ (tar.bz2) archive format (.tbz, .tbz2, .tar.bz2)
Tbz,
/// TAR.Z (tar + Unix compress) archive format (.tar.Z)
TarZ,
//
Uc2,
}
impl ArchiveFormat {
/// All supported archive formats
pub const ALL: &'static [ArchiveFormat] = &[
ArchiveFormat::Ace,
ArchiveFormat::Arc,
ArchiveFormat::Arj,
ArchiveFormat::Zoo,
ArchiveFormat::Sq,
ArchiveFormat::Sqz,
ArchiveFormat::Z,
ArchiveFormat::Gz,
ArchiveFormat::Bz2,
ArchiveFormat::Ice,
ArchiveFormat::PackIce,
ArchiveFormat::Hyp,
ArchiveFormat::Ha,
ArchiveFormat::Uc2,
ArchiveFormat::Lha,
ArchiveFormat::Zip,
ArchiveFormat::Rar,
ArchiveFormat::SevenZ,
ArchiveFormat::Tar,
ArchiveFormat::Tgz,
ArchiveFormat::Tbz,
ArchiveFormat::TarZ,
];
/// Try to detect the archive format from a file extension (internal use only)
fn from_extension(ext: &str) -> Option<Self> {
let ext_lower = ext.to_lowercase();
match ext_lower.as_str() {
"ace" => Some(ArchiveFormat::Ace),
"arc" => Some(ArchiveFormat::Arc),
"arj" => Some(ArchiveFormat::Arj),
"zoo" => Some(ArchiveFormat::Zoo),
"sq" | "sq2" | "qqq" => Some(ArchiveFormat::Sq),
"sqz" => Some(ArchiveFormat::Sqz),
"z" => Some(ArchiveFormat::Z),
"gz" => Some(ArchiveFormat::Gz),
"bz2" => Some(ArchiveFormat::Bz2),
"ice" => Some(ArchiveFormat::Ice),
// Pack-Ice compressed pictures commonly use .PI9
"pi9" => Some(ArchiveFormat::PackIce),
// PAK uses same format as ARC with additional compression methods
"pak" => Some(ArchiveFormat::Arc),
"uc2" => Some(ArchiveFormat::Uc2),
"ue2" => Some(ArchiveFormat::Uc2),
"hyp" => Some(ArchiveFormat::Hyp),
"ha" => Some(ArchiveFormat::Ha),
"lha" | "lzh" => Some(ArchiveFormat::Lha),
"zip" => Some(ArchiveFormat::Zip),
"rar" => Some(ArchiveFormat::Rar),
"7z" => Some(ArchiveFormat::SevenZ),
"tar" => Some(ArchiveFormat::Tar),
"tgz" => Some(ArchiveFormat::Tgz),
"tbz" | "tbz2" => Some(ArchiveFormat::Tbz),
_ => {
// Check for ?Q? pattern (e.g., .BQK, .CQM, .DQC)
let bytes = ext_lower.as_bytes();
if bytes.len() == 3 && bytes[1] == b'q' {
Some(ArchiveFormat::Sq)
} else {
None
}
}
}
}
/// Try to detect the archive format from a file path
///
/// # Example
/// ```
/// use std::path::Path;
/// use unarc_rs::unified::ArchiveFormat;
///
/// assert_eq!(ArchiveFormat::from_path(Path::new("archive.arj")), Some(ArchiveFormat::Arj));
/// assert_eq!(ArchiveFormat::from_path(Path::new("/path/to/file.zoo")), Some(ArchiveFormat::Zoo));
/// ```
pub fn from_path(path: &Path) -> Option<Self> {
// Check for .tar.gz first (double extension)
let filename = path.file_name()?.to_str()?;
let filename_lower = filename.to_lowercase();
if filename_lower.ends_with(".tar.gz") {
return Some(ArchiveFormat::Tgz);
}
if filename_lower.ends_with(".tar.bz2") {
return Some(ArchiveFormat::Tbz);
}
if filename_lower.ends_with(".tar.z") {
return Some(ArchiveFormat::TarZ);
}
path.extension().and_then(|ext| ext.to_str()).and_then(Self::from_extension)
}
/// Returns the typical file extension for this format
pub fn extension(&self) -> &'static str {
match self {
ArchiveFormat::Ace => "ace",
ArchiveFormat::Arc => "arc",
ArchiveFormat::Arj => "arj",
ArchiveFormat::Zoo => "zoo",
ArchiveFormat::Sq => "sq",
ArchiveFormat::Sqz => "sqz",
ArchiveFormat::Z => "Z",
ArchiveFormat::Gz => "gz",
ArchiveFormat::Bz2 => "bz2",
ArchiveFormat::Ice => "ice",
ArchiveFormat::PackIce => "pi9",
ArchiveFormat::Hyp => "hyp",
ArchiveFormat::Ha => "ha",
ArchiveFormat::Uc2 => "uc2",
ArchiveFormat::Lha => "lha",
ArchiveFormat::Zip => "zip",
ArchiveFormat::Rar => "rar",
ArchiveFormat::SevenZ => "7z",
ArchiveFormat::Tar => "tar",
ArchiveFormat::Tgz => "tgz",
ArchiveFormat::Tbz => "tbz2",
ArchiveFormat::TarZ => "tar.Z",
}
}
/// Returns a human-readable name for this format
pub fn name(&self) -> &'static str {
match self {
ArchiveFormat::Ace => "ACE",
ArchiveFormat::Arc => "ARC",
ArchiveFormat::Arj => "ARJ",
ArchiveFormat::Zoo => "ZOO",
ArchiveFormat::Sq => "SQ (Squeezed)",
ArchiveFormat::Sqz => "SQZ (Squeeze It)",
ArchiveFormat::Z => "Z (Unix compress)",
ArchiveFormat::Gz => "GZ (gzip)",
ArchiveFormat::Bz2 => "BZ2 (bzip2)",
ArchiveFormat::Ice => "ICE (Legacy DOS)",
ArchiveFormat::PackIce => "Pack-Ice (Atari ST)",
ArchiveFormat::Hyp => "HYP (Hyper)",
ArchiveFormat::Ha => "HA (Harri Archiver)",
ArchiveFormat::Uc2 => "UC2 (Ultra Compressor II)",
ArchiveFormat::Lha => "LHA/LZH",
ArchiveFormat::Zip => "ZIP",
ArchiveFormat::Rar => "RAR",
ArchiveFormat::SevenZ => "7z",
ArchiveFormat::Tar => "TAR",
ArchiveFormat::Tgz => "TGZ (tar.gz)",
ArchiveFormat::Tbz => "TBZ (tar.bz2)",
ArchiveFormat::TarZ => "TAR.Z (tar + Unix compress)",
}
}
/// Returns all typical file extensions for this format
pub fn extensions(&self) -> &'static [&'static str] {
match self {
ArchiveFormat::Ace => &["ace"],
ArchiveFormat::Arc => &["arc"],
ArchiveFormat::Arj => &["arj"],
ArchiveFormat::Zoo => &["zoo"],
ArchiveFormat::Sq => &["sq", "sq2", "qqq"],
ArchiveFormat::Sqz => &["sqz"],
ArchiveFormat::Z => &["Z"],
ArchiveFormat::Gz => &["gz"],
ArchiveFormat::Bz2 => &["bz2"],
ArchiveFormat::Ice => &["ice"],
ArchiveFormat::PackIce => &["pi9"],
ArchiveFormat::Hyp => &["hyp"],
ArchiveFormat::Ha => &["ha"],
// UE2 is UltraCrypt-encrypted UC2 (detected, but not decrypted).
ArchiveFormat::Uc2 => &["uc2", "ue2"],
ArchiveFormat::Lha => &["lha", "lzh"],
ArchiveFormat::Zip => &["zip"],
ArchiveFormat::Rar => &["rar"],
ArchiveFormat::SevenZ => &["7z"],
ArchiveFormat::Tar => &["tar"],
ArchiveFormat::Tgz => &["tgz", "tar.gz"],
ArchiveFormat::Tbz => &["tbz", "tbz2", "tar.bz2"],
ArchiveFormat::TarZ => &["tar.Z"],
}
}
/// Returns the magic bytes (preamble) for this archive format, if available.
///
/// Some formats have magic bytes at a specific offset rather than at the start,
/// in which case this returns `None` and `detect_from_bytes` should be used instead.
///
/// # Example
/// ```
/// use unarc_rs::unified::ArchiveFormat;
///
/// assert_eq!(ArchiveFormat::Arj.preambles(), Some(&[&[0x60u8, 0xEA][..]][..]));
/// assert_eq!(ArchiveFormat::Zip.preambles(), Some(&[b"PK\x03\x04".as_slice(), b"PK\x05\x06".as_slice()][..]));
/// ```
pub fn preambles(&self) -> Option<&'static [&'static [u8]]> {
match self {
// ACE: "**ACE**" at offset 7
ArchiveFormat::Ace => Some(&[b"**ACE**"]),
// ARC: 0x1A followed by method byte (1-11)
ArchiveFormat::Arc => Some(&[&[0x1A]]),
// ARJ: 0x60 0xEA
ArchiveFormat::Arj => Some(&[&[0x60, 0xEA]]),
// ZOO: "ZOO " text header
ArchiveFormat::Zoo => Some(&[b"ZOO "]),
// SQ/SQ2: two possible signatures
ArchiveFormat::Sq => Some(&[&[0x76, 0xFF], &[0xFA, 0xFF]]),
// SQZ: "HLSQZ"
ArchiveFormat::Sqz => Some(&[b"HLSQZ"]),
// Unix compress: 0x1F 0x9D
ArchiveFormat::Z => Some(&[&[0x1F, 0x9D]]),
// Gzip: 0x1F 0x8B
ArchiveFormat::Gz => Some(&[&[0x1F, 0x8B]]),
// Bzip2: "BZh"
ArchiveFormat::Bz2 => Some(&[b"BZh"]),
// ICE: no fixed magic, starts with size (Legacy DOS format)
ArchiveFormat::Ice => None,
// Pack-Ice: "ICE!", "Ice!", "TMM!", "TSM!", "SHE!" at offset 0
ArchiveFormat::PackIce => Some(&[b"ICE!", b"Ice!", b"TMM!", b"TSM!", b"SHE!"]),
// HYP: "HP" (compressed) or "ST" (stored)
ArchiveFormat::Hyp => Some(&[b"HP", b"ST"]),
// HA: "HA"
ArchiveFormat::Ha => Some(&[b"HA"]),
// UC2: "UC2\x1A" (normal) or "UE2" (UltraCrypt-encrypted)
ArchiveFormat::Uc2 => Some(&[b"UC2\x1a", b"UE2"]),
// LHA: "-lh" or "-lz" at offset 2
ArchiveFormat::Lha => Some(&[b"-lh", b"-lz"]),
// ZIP: "PK\x03\x04" or "PK\x05\x06" (empty)
ArchiveFormat::Zip => Some(&[b"PK\x03\x04", b"PK\x05\x06"]),
// RAR: "Rar!\x1A\x07"
ArchiveFormat::Rar => Some(&[b"Rar!\x1a\x07"]),
// 7z: "7z\xBC\xAF\x27\x1C"
ArchiveFormat::SevenZ => Some(&[&[0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C]]),
// TAR: "ustar" at offset 257
ArchiveFormat::Tar => Some(&[b"ustar"]),
// TGZ: gzip magic (contains TAR inside)
ArchiveFormat::Tgz => Some(&[&[0x1F, 0x8B]]),
// TBZ: bzip2 magic (contains TAR inside)
ArchiveFormat::Tbz => Some(&[b"BZh"]),
// TAR.Z: Unix compress magic (contains TAR inside)
ArchiveFormat::TarZ => Some(&[&[0x1F, 0x9D]]),
}
}
/// Returns the offset where the magic bytes are located.
///
/// Most formats have magic at offset 0, but some (like LHA, ACE, TAR) have it elsewhere.
pub fn preamble_offset(&self) -> usize {
match self {
ArchiveFormat::Lha => 2, // "-lh" or "-lz" at offset 2
ArchiveFormat::Ace => 7, // "**ACE**" at offset 7
ArchiveFormat::Tar => 257, // "ustar" at offset 257
_ => 0,
}
}
/// Detect archive format from file content (magic bytes).
///
/// This function attempts to identify the archive format by examining
/// the first bytes of the file content. It checks for known magic bytes
/// and signatures at various offsets.
///
/// Returns `Some(format)` if a known format is detected, `None` otherwise.
///
/// # Example
/// ```
/// use unarc_rs::unified::ArchiveFormat;
///
/// let zip_data = b"PK\x03\x04rest of zip...";
/// assert_eq!(ArchiveFormat::detect_from_bytes(zip_data), Some(ArchiveFormat::Zip));
///
/// let unknown = b"random data";
/// assert_eq!(ArchiveFormat::detect_from_bytes(unknown), None);
/// ```
pub fn detect_from_bytes(data: &[u8]) -> Option<Self> {
if data.len() < 2 {
return None;
}
// Check simple prefix-based formats first (most specific to least)
// 7z: 6 bytes "7z\xBC\xAF\x27\x1C"
if data.len() >= 6 && data.starts_with(&[0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C]) {
return Some(ArchiveFormat::SevenZ);
}
// RAR: "Rar!\x1A\x07" (6 bytes for RAR4, 7 bytes for RAR5)
if data.len() >= 6 && data.starts_with(b"Rar!\x1a\x07") {
return Some(ArchiveFormat::Rar);
}
// SQZ: "HLSQZ" (5 bytes)
if data.len() >= 5 && data.starts_with(b"HLSQZ") {
return Some(ArchiveFormat::Sqz);
}
// ZIP: "PK\x03\x04" or "PK\x05\x06" (empty archive)
if data.len() >= 4 && (data.starts_with(b"PK\x03\x04") || data.starts_with(b"PK\x05\x06")) {
return Some(ArchiveFormat::Zip);
}
// Pack-Ice: several 4-byte IDs at offset 0
if data.len() >= 4
&& (data.starts_with(b"ICE!") || data.starts_with(b"Ice!") || data.starts_with(b"TMM!") || data.starts_with(b"TSM!") || data.starts_with(b"SHE!"))
{
return Some(ArchiveFormat::PackIce);
}
// UC2: "UC2\x1A"
if data.len() >= 4 && data.starts_with(b"UC2\x1a") {
return Some(ArchiveFormat::Uc2);
}
// UE2: UltraCrypt-encrypted UC2 wrapper
if data.len() >= 3 && data.starts_with(b"UE2") {
return Some(ArchiveFormat::Uc2);
}
// ZOO: "ZOO " (then version text)
if data.len() >= 4 && data.starts_with(b"ZOO ") {
return Some(ArchiveFormat::Zoo);
}
// BZ2: "BZh" (3 bytes)
if data.len() >= 3 && data.starts_with(b"BZh") {
// Could be TBZ if followed by TAR, but we can't know without decompressing
// Return Bz2 as default, let caller use extension for TBZ
return Some(ArchiveFormat::Bz2);
}
// Gzip: 0x1F 0x8B (2 bytes)
if data.len() >= 2 && data[0] == 0x1F && data[1] == 0x8B {
// Could be TGZ if contains TAR, but we can't know without decompressing
// Return Gz as default, let caller use extension for TGZ
return Some(ArchiveFormat::Gz);
}
// Unix compress: 0x1F 0x9D (2 bytes)
if data.len() >= 2 && data[0] == 0x1F && data[1] == 0x9D {
// Could be TAR.Z, but we can't know without decompressing
return Some(ArchiveFormat::Z);
}
// ARJ: 0x60 0xEA (2 bytes)
if data.len() >= 2 && data[0] == 0x60 && data[1] == 0xEA {
return Some(ArchiveFormat::Arj);
}
// HA: "HA" (2 bytes)
if data.len() >= 2 && data.starts_with(b"HA") {
return Some(ArchiveFormat::Ha);
}
// HYP: "HP" (compressed) or "ST" (stored)
if data.len() >= 2 && (data.starts_with(b"HP") || data.starts_with(b"ST")) {
return Some(ArchiveFormat::Hyp);
}
// SQ: 0x76 0xFF or 0xFA 0xFF
if data.len() >= 2 && data[1] == 0xFF && (data[0] == 0x76 || data[0] == 0xFA) {
return Some(ArchiveFormat::Sq);
}
// ARC: 0x1A followed by method byte (1-11)
if data.len() >= 2 && data[0] == 0x1A && data[1] >= 1 && data[1] <= 11 {
return Some(ArchiveFormat::Arc);
}
// ACE: "**ACE**" at offset 7
if data.len() >= 14 && &data[7..14] == b"**ACE**" {
return Some(ArchiveFormat::Ace);
}
// LHA/LZH: "-lh" or "-lz" at offset 2
if data.len() >= 5 && data[2] == b'-' && data[3] == b'l' && (data[4] == b'h' || data[4] == b'z') {
return Some(ArchiveFormat::Lha);
}
// TAR: "ustar" at offset 257 (POSIX format)
if data.len() >= 263 && &data[257..262] == b"ustar" {
return Some(ArchiveFormat::Tar);
}
// TAR: Alternative detection - check if it looks like a TAR header
// TAR headers have null-terminated filename at start, mode at 100, etc.
if data.len() >= 512 {
// Check for valid TAR: name ends with null, reasonable checksum area
let has_null_in_name = data[..100].contains(&0);
let checksum_area = &data[148..156];
let is_checksum_space_or_digit = checksum_area.iter().all(|&b| b == b' ' || b == b'0' || (b'1'..=b'7').contains(&b));
if has_null_in_name && is_checksum_space_or_digit {
// Could be TAR, but this is a weak heuristic
// Only return TAR if nothing else matched
return Some(ArchiveFormat::Tar);
}
}
None
}
/// Detect archive format from a reader.
///
/// Reads up to 512 bytes from the reader, then seeks back to the original position.
/// Returns `Some(format)` if detected, `None` otherwise.
pub fn detect_from_reader<R: Read + Seek>(reader: &mut R) -> std::io::Result<Option<Self>> {
let pos = reader.stream_position()?;
let mut buffer = [0u8; 512];
let bytes_read = reader.read(&mut buffer)?;
reader.seek(std::io::SeekFrom::Start(pos))?;
Ok(Self::detect_from_bytes(&buffer[..bytes_read]))
}
/// Detect archive format, trying preamble first, then falling back to extension.
///
/// This is the recommended method for detecting archive formats:
/// 1. First tries to detect from file content (magic bytes)
/// 2. Falls back to extension-based detection if content detection fails
///
/// # Example
/// ```no_run
/// use std::path::Path;
/// use std::fs::File;
/// use std::io::Read;
/// use unarc_rs::unified::ArchiveFormat;
///
/// let path = Path::new("archive.arj");
/// let mut file = File::open(path).unwrap();
/// let format = ArchiveFormat::detect(&mut file, Some(path)).unwrap();
/// ```
pub fn detect<R: Read + Seek>(reader: &mut R, path: Option<&Path>) -> std::io::Result<Option<Self>> {
// First try content-based detection
if let Some(format) = Self::detect_from_reader(reader)? {
return Ok(Some(format));
}
// Fall back to extension-based detection
if let Some(p) = path {
return Ok(Self::from_path(p));
}
Ok(None)
}
/// Open an archive with this format
///
/// # Example
/// ```no_run
/// use std::fs::File;
/// use unarc_rs::unified::ArchiveFormat;
///
/// let file = File::open("archive.arj").unwrap();
/// let mut archive = ArchiveFormat::Arj.open(file).unwrap();
///
/// while let Some(entry) = archive.next_entry().unwrap() {
/// println!("File: {}", entry.name());
/// }
/// ```
pub fn open<T: Read + Seek>(self, reader: T) -> Result<UnifiedArchive<T>> {
UnifiedArchive::open_with_format(reader, self)
}
/// Open an archive file directly from a path
///
/// This is a convenience method that opens the file, detects the format from
/// the extension, and returns a ready-to-use archive reader.
///
/// # Example
/// ```no_run
/// use unarc_rs::unified::ArchiveFormat;
///
/// let mut archive = ArchiveFormat::open_path("archive.arj").unwrap();
///
/// for entry in archive.entries_iter() {
/// let entry = entry.unwrap();
/// println!("File: {}", entry.name());
/// }
/// ```
pub fn open_path<P: AsRef<Path>>(path: P) -> Result<UnifiedArchive<BufReader<File>>> {
let path = path.as_ref();
let format = Self::from_path(path).ok_or_else(|| ArchiveError::UnsupportedFormat(format!("Unsupported archive format: {}", path.display())))?;
let file = File::open(path)?;
let reader = BufReader::new(file);
let mut archive = format.open(reader)?;
// For single-file formats (.Z, .gz, .bz2), derive the output filename from the archive name
if matches!(format, ArchiveFormat::Z | ArchiveFormat::Gz | ArchiveFormat::Bz2) {
if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
archive.set_single_file_name(stem.to_string());
}
}
Ok(archive)
}
/// Open an archive with this format and options
///
/// # Example
/// ```no_run
/// use std::fs::File;
/// use unarc_rs::unified::{ArchiveFormat, ArchiveOptions};
///
/// let file = File::open("encrypted.zip").unwrap();
/// let options = ArchiveOptions::new().with_password("secret");
/// let mut archive = ArchiveFormat::Zip.open_with_options(file, options).unwrap();
/// ```
pub fn open_with_options<T: Read + Seek>(self, reader: T, options: ArchiveOptions) -> Result<UnifiedArchive<T>> {
UnifiedArchive::open_with_format_and_options(reader, self, options)
}
/// Open an archive file directly from a path with options
///
/// # Example
/// ```no_run
/// use unarc_rs::unified::{ArchiveFormat, ArchiveOptions};
///
/// let options = ArchiveOptions::new()
/// .with_password("secret")
/// .with_verify_crc(true);
///
/// let mut archive = ArchiveFormat::open_path_with_options("encrypted.zip", options).unwrap();
/// ```
pub fn open_path_with_options<P: AsRef<Path>>(path: P, options: ArchiveOptions) -> Result<UnifiedArchive<BufReader<File>>> {
let path = path.as_ref();
let format = Self::from_path(path).ok_or_else(|| ArchiveError::UnsupportedFormat(format!("Unsupported archive format: {}", path.display())))?;
let file = File::open(path)?;
let reader = BufReader::new(file);
let mut archive = format.open_with_options(reader, options)?;
// For single-file formats (.Z, .gz, .bz2), derive the output filename from the archive name
if matches!(format, ArchiveFormat::Z | ArchiveFormat::Gz | ArchiveFormat::Bz2) {
if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
archive.set_single_file_name(stem.to_string());
}
}
Ok(archive)
}
/// Open a multi-volume (split) ZIP archive from multiple files
///
/// This function is for ZIP archives that have been split into multiple files,
/// typically named with sequential extensions like `.001`, `.002`, `.003`, etc.
///
/// # Arguments
/// * `paths` - Paths to the volume files in order (first volume first)
/// * `options` - Archive options (password, etc.)
///
/// # Example
/// ```no_run
/// use std::path::PathBuf;
/// use unarc_rs::unified::{ArchiveFormat, ArchiveOptions};
///
/// let volumes = vec![
/// PathBuf::from("archive.001"),
/// PathBuf::from("archive.002"),
/// PathBuf::from("archive.003"),
/// ];
///
/// let mut archive = ArchiveFormat::open_multi_volume_zip(&volumes, ArchiveOptions::new()).unwrap();
///
/// for entry in archive.entries_iter() {
/// let entry = entry.unwrap();
/// println!("File: {}", entry.name());
/// }
/// ```
pub fn open_multi_volume_zip<P: AsRef<Path>>(paths: &[P], options: ArchiveOptions) -> Result<UnifiedArchive<MultiVolumeReader>> {
if paths.is_empty() {
return Err(ArchiveError::io_error("No volume files provided for multi-volume ZIP archive"));
}
// Get sizes of all volumes
let mut volume_sizes = Vec::with_capacity(paths.len());
for path in paths {
let metadata = std::fs::metadata(path.as_ref())?;
volume_sizes.push(metadata.len());
}
// Create volume provider from file paths
let paths_owned: Vec<std::path::PathBuf> = paths.iter().map(|p| p.as_ref().to_path_buf()).collect();
let volume_provider = Arc::new(FileVolumeProvider { paths: paths_owned });
// Create the multi-volume reader
let reader = MultiVolumeReader::new(volume_provider.clone(), volume_sizes);
// Open ZIP archive with the multi-volume reader
let password = options.password().map(|s| s.to_string());
let mut archive = ZipArchive::new(reader)?;
if let Some(ref pwd) = password {
archive.set_password(pwd.as_bytes());
}
Ok(UnifiedArchive {
inner: ArchiveInner::Zip(archive),
format: ArchiveFormat::Zip,
single_file_name: None,
options,
})
}
/// Open a multi-volume (split) 7z archive from multiple files
///
/// This function is for 7z archives that have been split into multiple files,
/// typically named with sequential extensions like `.7z.001`, `.7z.002`, `.7z.003`, etc.
///
/// # Arguments
/// * `paths` - Paths to the volume files in order (first volume first)
/// * `options` - Archive options (password, etc.)
///
/// # Example
/// ```no_run
/// use std::path::PathBuf;
/// use unarc_rs::unified::{ArchiveFormat, ArchiveOptions};
///
/// let volumes = vec![
/// PathBuf::from("archive.7z.001"),
/// PathBuf::from("archive.7z.002"),
/// PathBuf::from("archive.7z.003"),
/// ];
///
/// let mut archive = ArchiveFormat::open_multi_volume_7z(&volumes, ArchiveOptions::new()).unwrap();
///
/// for entry in archive.entries_iter() {
/// let entry = entry.unwrap();
/// println!("File: {}", entry.name());
/// }
/// ```
pub fn open_multi_volume_7z<P: AsRef<Path>>(paths: &[P], options: ArchiveOptions) -> Result<UnifiedArchive<MultiVolumeReader>> {
if paths.is_empty() {
return Err(ArchiveError::io_error("No volume files provided for multi-volume 7z archive"));
}
// Get sizes of all volumes
let mut volume_sizes = Vec::with_capacity(paths.len());
for path in paths {
let metadata = std::fs::metadata(path.as_ref())?;
volume_sizes.push(metadata.len());
}
// Create volume provider from file paths
let paths_owned: Vec<std::path::PathBuf> = paths.iter().map(|p| p.as_ref().to_path_buf()).collect();
let volume_provider = Arc::new(FileVolumeProvider { paths: paths_owned });
// Create the multi-volume reader
let reader = MultiVolumeReader::new(volume_provider.clone(), volume_sizes);
// Open 7z archive with the multi-volume reader
let password = options.password().map(|s| s.to_string());
let archive = SevenZArchive::new_with_password(reader, password)?;
Ok(UnifiedArchive {
inner: ArchiveInner::SevenZ(archive),
format: ArchiveFormat::SevenZ,
single_file_name: None,
options,
})
}
}
/// File-based volume provider for multi-volume archives
struct FileVolumeProvider {
paths: Vec<std::path::PathBuf>,
}
impl VolumeProvider for FileVolumeProvider {
fn open_volume(&self, volume_number: u32) -> Option<Box<dyn Read + Send>> {
self.paths
.get(volume_number as usize)
.and_then(|path| File::open(path).ok().map(|f| Box::new(BufReader::new(f)) as Box<dyn Read + Send>))
}
}
/// Unified archive entry containing metadata about a file in an archive
#[derive(Debug, Clone)]
pub struct ArchiveEntry {
/// File name (may include path)
name: String,
/// Compressed size in bytes
compressed_size: u64,
/// Original (uncompressed) size in bytes
original_size: u64,
/// Compression method as a human-readable string
compression_method: String,
/// Modification date/time (if available)
modified_time: Option<DosDateTime>,
/// CRC checksum (CRC16 or CRC32, stored as u64)
crc: u64,
/// Encryption method (if any)
encryption: EncryptionMethod,
/// Internal index for reading the entry
index: EntryIndex,
}
/// Internal index to track which entry to read
#[derive(Debug, Clone)]
enum EntryIndex {
Ace(AceFileHeader),
Arc(ArcHeader),
Arj(ArjHeader),
Zoo(ZooEntry),
Sq(SqHeader),
Sqz(SqzFileHeader),
/// Z format has no header per file, just one file
Z,
/// GZ format has no header per file, just one file
Gz,
/// BZ2 format has no header per file, just one file
Bz2,
/// ICE format has no header per file, just one file (Legacy DOS)
Ice,
/// Pack-Ice format has no header per file, just one file (Atari ST)
PackIce,
Hyp(HypHeader),
Ha(HaHeader),
Uc2(Uc2Header),
Lha(LhaFileHeader),
Zip(ZipFileHeader),
Rar(RarFileHeader),
SevenZ(SevenZFileHeader),
Tar(TarFileHeader),
/// TGZ uses the same header as TAR
Tgz(TarFileHeader),
/// TBZ uses the same header as TAR
Tbz(TarFileHeader),
/// TAR.Z uses the same header as TAR
TarZ(TarFileHeader),
}
impl ArchiveEntry {
/// Returns the file name (may include path components)
pub fn name(&self) -> &str {
&self.name
}
/// Returns just the file name without any path components
pub fn file_name(&self) -> &str {
self.name.rsplit(['/', '\\']).next().unwrap_or(&self.name)
}
/// Returns the compressed size in bytes
pub fn compressed_size(&self) -> u64 {
self.compressed_size
}
/// Returns the original (uncompressed) size in bytes
pub fn original_size(&self) -> u64 {
self.original_size
}
/// Returns the compression method as a human-readable string
pub fn compression_method(&self) -> &str {
&self.compression_method
}
/// Returns the modification date/time if available
pub fn modified_time(&self) -> Option<DosDateTime> {
self.modified_time
}
/// Returns the CRC checksum (CRC16 or CRC32 depending on format)
pub fn crc(&self) -> u64 {
self.crc
}
/// Returns the compression ratio (0.0 - 1.0, where lower is better compression)
pub fn compression_ratio(&self) -> f64 {
if self.original_size == 0 {
1.0
} else {
self.compressed_size as f64 / self.original_size as f64
}
}
/// Returns true if this entry is stored without compression
pub fn is_stored(&self) -> bool {
self.compression_method.to_lowercase().contains("stored") || self.compression_method.to_lowercase().contains("unpacked")
}
/// Returns the encryption method used for this entry
pub fn encryption(&self) -> EncryptionMethod {
self.encryption
}
/// Returns true if this entry is encrypted
pub fn is_encrypted(&self) -> bool {
self.encryption.is_encrypted()
}
}
/// Iterator over archive entries
///
/// This iterator yields `Result<ArchiveEntry>` for each entry in the archive.
/// The iterator automatically skips entries after yielding them.
pub struct ArchiveEntryIter<'a, T: Read + Seek> {
archive: &'a mut UnifiedArchive<T>,
}
impl<T: Read + Seek> Iterator for ArchiveEntryIter<'_, T> {
type Item = Result<ArchiveEntry>;
fn next(&mut self) -> Option<Self::Item> {
match self.archive.next_entry() {
Ok(Some(entry)) => {
// Auto-skip after yielding the entry
if let Err(e) = self.archive.skip(&entry) {
return Some(Err(e));
}
Some(Ok(entry))
}
Ok(None) => None,
Err(e) => Some(Err(e)),
}
}
}
/// Internal archive wrapper enum
enum ArchiveInner<T: Read + Seek> {
Ace(AceArchive<T>),
Arc(ArcArchive<T>),
Arj(ArjArchive<T>),
Zoo(ZooArchive<T>),
Sq(SqArchive<T>),
Sqz(SqzArchive<T>),
Z(ZArchive<T>, bool), // bool = already read
Gz(GzArchive<T>, bool), // bool = already read
Bz2(Bz2Archive<T>, bool), // bool = already read
Ice(IceArchive, bool), // bool = already read (Legacy DOS)
PackIce(PackIceArchive, bool), // bool = already read (Atari ST)
Hyp(HypArchive<T>),
Ha(HaArchive<T>),
Uc2(Uc2Archive<T>),
Lha(LhaArchiveSeekable<T>),
Zip(ZipArchive<T>),
Rar(RarArchive<T>),
SevenZ(SevenZArchive<T>),
Tar(TarArchive<T>),
/// TGZ decompresses to memory, so it doesn't need the generic reader type
Tgz(TgzArchive),
/// TBZ decompresses to memory, so it doesn't need the generic reader type
Tbz(TbzArchive),
/// TAR.Z decompresses to memory, so it doesn't need the generic reader type
TarZ(TarZArchive),
}
/// Unified archive reader that provides a common interface for all supported formats
pub struct UnifiedArchive<T: Read + Seek> {
inner: ArchiveInner<T>,
format: ArchiveFormat,
/// For single-file formats (.Z, .gz, .bz2): store the original filename if known
single_file_name: Option<String>,
/// Options for archive operations (password, CRC verification, etc.)
options: ArchiveOptions,
}
impl<T: Read + Seek> UnifiedArchive<T> {
/// Open an archive with a specific format
///
/// # Example
/// ```no_run
/// use std::fs::File;
/// use unarc_rs::unified::{UnifiedArchive, ArchiveFormat};
///
/// let file = File::open("archive.arj").unwrap();
/// let archive = UnifiedArchive::open_with_format(file, ArchiveFormat::Arj).unwrap();
/// ```
pub fn open_with_format(reader: T, format: ArchiveFormat) -> Result<Self> {
let inner = match format {
ArchiveFormat::Ace => ArchiveInner::Ace(AceArchive::new(reader)?),
ArchiveFormat::Arc => ArchiveInner::Arc(ArcArchive::new(reader)?),
ArchiveFormat::Arj => ArchiveInner::Arj(ArjArchive::new(reader)?),
ArchiveFormat::Zoo => ArchiveInner::Zoo(ZooArchive::new(reader)?),
ArchiveFormat::Sq => ArchiveInner::Sq(SqArchive::new(reader)?),
ArchiveFormat::Sqz => ArchiveInner::Sqz(SqzArchive::new(reader)?),
ArchiveFormat::Z => ArchiveInner::Z(ZArchive::new(reader)?, false),
ArchiveFormat::Gz => ArchiveInner::Gz(GzArchive::new(reader)?, false),
ArchiveFormat::Bz2 => ArchiveInner::Bz2(Bz2Archive::new(reader)?, false),
ArchiveFormat::Ice => ArchiveInner::Ice(IceArchive::new(reader)?, false),
ArchiveFormat::PackIce => ArchiveInner::PackIce(PackIceArchive::from_reader(reader)?, false),
ArchiveFormat::Hyp => ArchiveInner::Hyp(HypArchive::new(reader)?),
ArchiveFormat::Ha => ArchiveInner::Ha(HaArchive::new(reader)?),
ArchiveFormat::Uc2 => ArchiveInner::Uc2(Uc2Archive::new(reader)?),
ArchiveFormat::Lha => ArchiveInner::Lha(LhaArchiveSeekable::new(reader)?),
ArchiveFormat::Zip => ArchiveInner::Zip(ZipArchive::new(reader)?),
ArchiveFormat::Rar => ArchiveInner::Rar(RarArchive::new(reader)?),
ArchiveFormat::SevenZ => ArchiveInner::SevenZ(SevenZArchive::new(reader)?),
ArchiveFormat::Tar => ArchiveInner::Tar(TarArchive::new(reader)?),
ArchiveFormat::Tgz => ArchiveInner::Tgz(TgzArchive::new(reader)?),
ArchiveFormat::Tbz => ArchiveInner::Tbz(TbzArchive::new(reader)?),
ArchiveFormat::TarZ => ArchiveInner::TarZ(TarZArchive::new(reader)?),
};
Ok(Self {
inner,
format,
single_file_name: None,
options: ArchiveOptions::default(),
})
}
/// Open an archive with a specific format and options
///
/// # Example
/// ```no_run
/// use std::fs::File;
/// use unarc_rs::unified::{UnifiedArchive, ArchiveFormat, ArchiveOptions};
///
/// let file = File::open("encrypted.zip").unwrap();
/// let options = ArchiveOptions::new().with_password("secret");
/// let archive = UnifiedArchive::open_with_format_and_options(file, ArchiveFormat::Zip, options).unwrap();
/// ```
pub fn open_with_format_and_options(reader: T, format: ArchiveFormat, options: ArchiveOptions) -> Result<Self> {
let password = options.password().map(|s| s.to_string());
let inner = match format {
ArchiveFormat::Ace => {
let mut archive = AceArchive::new(reader)?;
if let Some(ref pwd) = password {
archive.set_password(pwd);
}
if let Some(ref provider) = options.volume_provider {
archive.set_volume_provider(provider.clone());
}
ArchiveInner::Ace(archive)
}
ArchiveFormat::Arc => {
let mut archive = ArcArchive::new(reader)?;
if let Some(ref pwd) = password {
archive.set_password(pwd);
}
ArchiveInner::Arc(archive)
}
ArchiveFormat::Arj => {
let mut archive = ArjArchive::new(reader)?;
if let Some(ref pwd) = password {
archive.set_password(pwd);
}
if let Some(ref provider) = options.volume_provider {
archive.set_volume_provider(provider.clone());
}
ArchiveInner::Arj(archive)
}
ArchiveFormat::Zoo => ArchiveInner::Zoo(ZooArchive::new(reader)?),
ArchiveFormat::Sq => ArchiveInner::Sq(SqArchive::new(reader)?),
ArchiveFormat::Sqz => ArchiveInner::Sqz(SqzArchive::new(reader)?),
ArchiveFormat::Z => ArchiveInner::Z(ZArchive::new(reader)?, false),
ArchiveFormat::Gz => ArchiveInner::Gz(GzArchive::new(reader)?, false),
ArchiveFormat::Bz2 => ArchiveInner::Bz2(Bz2Archive::new(reader)?, false),
ArchiveFormat::Ice => ArchiveInner::Ice(IceArchive::new(reader)?, false),
ArchiveFormat::PackIce => ArchiveInner::PackIce(PackIceArchive::from_reader(reader)?, false),
ArchiveFormat::Hyp => ArchiveInner::Hyp(HypArchive::new(reader)?),
ArchiveFormat::Ha => ArchiveInner::Ha(HaArchive::new(reader)?),
ArchiveFormat::Uc2 => ArchiveInner::Uc2(Uc2Archive::new(reader)?),
ArchiveFormat::Lha => ArchiveInner::Lha(LhaArchiveSeekable::new(reader)?),
ArchiveFormat::Zip => {
let mut archive = ZipArchive::new(reader)?;
if let Some(ref pwd) = password {
archive.set_password(pwd.as_bytes());
}
ArchiveInner::Zip(archive)
}
ArchiveFormat::Rar => {
let mut archive = RarArchive::new(reader)?;
if let Some(ref pwd) = password {
archive.set_password(pwd);
}
ArchiveInner::Rar(archive)
}
ArchiveFormat::SevenZ => {
// 7z needs password at open time for encrypted archives
ArchiveInner::SevenZ(SevenZArchive::new_with_password(reader, password)?)
}
ArchiveFormat::Tar => ArchiveInner::Tar(TarArchive::new(reader)?),
ArchiveFormat::Tgz => ArchiveInner::Tgz(TgzArchive::new(reader)?),
ArchiveFormat::Tbz => ArchiveInner::Tbz(TbzArchive::new(reader)?),
ArchiveFormat::TarZ => ArchiveInner::TarZ(TarZArchive::new(reader)?),
};
Ok(Self {
inner,
format,
single_file_name: None,
options,
})
}
/// Returns the archive format
pub fn format(&self) -> ArchiveFormat {
self.format
}
/// Set the filename for single-file formats (.Z, .gz, .bz2) since they don't contain the filename
///
/// This is typically derived from the archive filename by removing the extension.
pub fn set_single_file_name(&mut self, name: String) {
self.single_file_name = Some(name);
}
/// Returns a reference to the archive options
pub fn options(&self) -> &ArchiveOptions {
&self.options
}
/// Returns a mutable reference to the archive options
pub fn options_mut(&mut self) -> &mut ArchiveOptions {
&mut self.options
}
/// Set the archive options
pub fn set_options(&mut self, options: ArchiveOptions) {
// Pass volume provider to inner archives that support multi-volume
if let Some(ref provider) = options.volume_provider {
match &mut self.inner {
ArchiveInner::Ace(archive) => {
archive.set_volume_provider(provider.clone());
}
ArchiveInner::Arj(archive) => {
archive.set_volume_provider(provider.clone());
}
_ => {}
}
}
self.options = options;
}
/// Set the password for encrypted entries
pub fn set_password<S: Into<String>>(&mut self, password: S) {
self.options = std::mem::take(&mut self.options).with_password(password);
}
/// Clear the password
pub fn clear_password(&mut self) {
self.options.password = None;
}
/// Get the next entry in the archive
///
/// Returns `Ok(None)` when there are no more entries.
pub fn next_entry(&mut self) -> Result<Option<ArchiveEntry>> {
match &mut self.inner {
ArchiveInner::Ace(archive) => {
if let Some(header) = archive.get_next_entry()? {
Ok(Some(ArchiveEntry {
name: header.filename.clone(),
compressed_size: header.packed_size,
original_size: header.original_size,
compression_method: format!("{}", header.compression_type),
modified_time: Some(header.datetime),
crc: header.crc32 as u64,
encryption: if header.is_encrypted() {
EncryptionMethod::Ace
} else {
EncryptionMethod::None
},
index: EntryIndex::Ace(header),
}))
} else {
Ok(None)
}
}
ArchiveInner::Arc(archive) => {
if let Some(header) = archive.get_next_entry()? {
Ok(Some(ArchiveEntry {
name: header.name.clone(),
compressed_size: header.compressed_size as u64,
original_size: header.original_size as u64,
compression_method: format!("{:?}", header.compression_method),
modified_time: Some(header.date_time),
crc: header.crc16 as u64,
encryption: EncryptionMethod::None,
index: EntryIndex::Arc(header),
}))
} else {
Ok(None)
}
}
ArchiveInner::Arj(archive) => {
if let Some(header) = archive.get_next_entry()? {
Ok(Some(ArchiveEntry {
name: header.name.clone(),
compressed_size: header.compressed_size as u64,
original_size: header.original_size as u64,
compression_method: format!("{:?}", header.compression_method),
modified_time: Some(header.date_time_modified),
crc: header.original_crc32 as u64,
encryption: match archive.get_encryption_type() {
Some(enc) => EncryptionMethod::Arj(enc),
None => EncryptionMethod::None,
},
index: EntryIndex::Arj(header),
}))
} else {
Ok(None)
}
}
ArchiveInner::Zoo(archive) => {
if let Some(entry) = archive.get_next_entry()? {
let result = ArchiveEntry {
name: entry.name.clone(),
compressed_size: entry.size_now as u64,
original_size: entry.org_size as u64,
compression_method: format!("{:?}", entry.compression_method),
modified_time: Some(entry.date_time),
crc: entry.file_crc16 as u64,
encryption: EncryptionMethod::None,
index: EntryIndex::Zoo(entry),
};
Ok(Some(result))
} else {
Ok(None)
}
}
ArchiveInner::Sq(archive) => {
if let Some(header) = archive.get_next_entry()? {
Ok(Some(ArchiveEntry {
name: header.name.clone(),
compressed_size: 0, // SQ doesn't track compressed size
original_size: 0, // SQ doesn't track original size in header
compression_method: "Squeezed".to_string(),
modified_time: None,
crc: header.checksum as u64,
encryption: EncryptionMethod::None,
index: EntryIndex::Sq(header),
}))
} else {
Ok(None)
}
}
ArchiveInner::Sqz(archive) => {
if let Some(header) = archive.get_next_entry()? {
Ok(Some(ArchiveEntry {
name: header.name.clone(),
compressed_size: header.compressed_size as u64,
original_size: header.original_size as u64,
compression_method: format!("{:?}", header.compression_method),
modified_time: Some(header.date_time),
crc: header.crc32 as u64,
encryption: EncryptionMethod::None,
index: EntryIndex::Sqz(header),
}))
} else {
Ok(None)
}
}
ArchiveInner::Z(_, ref mut read) => {
if *read {
Ok(None)
} else {
*read = true;
let name = self.single_file_name.clone().unwrap_or_else(|| "compressed".to_string());
Ok(Some(ArchiveEntry {
name,
compressed_size: 0, // Would need to seek to get this
original_size: 0, // Unknown until decompressed
compression_method: "LZW".to_string(),
modified_time: None,
crc: 0,
encryption: EncryptionMethod::None,
index: EntryIndex::Z,
}))
}
}
ArchiveInner::Gz(_, ref mut read) => {
if *read {
Ok(None)
} else {
*read = true;
let name = self.single_file_name.clone().unwrap_or_else(|| "compressed".to_string());
Ok(Some(ArchiveEntry {
name,
compressed_size: 0, // Would need to seek to get this
original_size: 0, // Unknown until decompressed
compression_method: "Deflate".to_string(),
modified_time: None,
crc: 0,
encryption: EncryptionMethod::None,
index: EntryIndex::Gz,
}))
}
}
ArchiveInner::Bz2(_, ref mut read) => {
if *read {
Ok(None)
} else {
*read = true;
let name = self.single_file_name.clone().unwrap_or_else(|| "compressed".to_string());
Ok(Some(ArchiveEntry {
name,
compressed_size: 0, // Would need to seek to get this
original_size: 0, // Unknown until decompressed
compression_method: "Bzip2".to_string(),
modified_time: None,
crc: 0,
encryption: EncryptionMethod::None,
index: EntryIndex::Bz2,
}))
}
}
ArchiveInner::Ice(ref archive, ref mut read) => {
if *read {
Ok(None)
} else {
*read = true;
let name = self.single_file_name.clone().unwrap_or_else(|| "compressed".to_string());
Ok(Some(ArchiveEntry {
name,
compressed_size: 0, // Not stored in ICE format
original_size: archive.original_size() as u64,
compression_method: "LH1".to_string(),
modified_time: None,
crc: 0,
encryption: EncryptionMethod::None,
index: EntryIndex::Ice,
}))
}
}
ArchiveInner::PackIce(ref archive, ref mut read) => {
if *read {
Ok(None)
} else {
*read = true;
let name = self.single_file_name.clone().unwrap_or_else(|| "compressed".to_string());
Ok(Some(ArchiveEntry {
name,
compressed_size: 0, // Not stored in Pack-Ice format
original_size: archive.original_size() as u64,
compression_method: "Pack-Ice".to_string(),
modified_time: None,
crc: 0,
encryption: EncryptionMethod::None,
index: EntryIndex::PackIce,
}))
}
}
ArchiveInner::Hyp(archive) => {
if let Some(header) = archive.get_next_entry()? {
Ok(Some(ArchiveEntry {
name: header.name.clone(),
compressed_size: header.compressed_size as u64,
original_size: header.original_size as u64,
compression_method: format!("{:?}", header.compression_method),
modified_time: Some(header.date_time),
crc: header.checksum as u64,
encryption: EncryptionMethod::None,
index: EntryIndex::Hyp(header),
}))
} else {
Ok(None)
}
}
ArchiveInner::Ha(archive) => {
if let Some(header) = archive.get_next_entry()? {
Ok(Some(ArchiveEntry {
name: header.full_path(),
compressed_size: header.compressed_size as u64,
original_size: header.original_size as u64,
compression_method: format!("{:?}", header.method),
modified_time: Some(header.timestamp),
crc: header.crc32 as u64,
encryption: EncryptionMethod::None,
index: EntryIndex::Ha(header),
}))
} else {
Ok(None)
}
}
ArchiveInner::Uc2(archive) => {
if let Some(header) = archive.get_next_entry()? {
Ok(Some(ArchiveEntry {
name: header.name.clone(),
compressed_size: header.compress_info.compressed_length as u64,
original_size: header.original_size as u64,
compression_method: format!("{:?}", header.compress_info.method),
modified_time: Some(DosDateTime::new(header.dos_time)),
crc: header.crc as u64,
encryption: EncryptionMethod::None,
index: EntryIndex::Uc2(header),
}))
} else {
Ok(None)
}
}
ArchiveInner::Lha(archive) => {
if let Some(header) = archive.get_next_entry()? {
Ok(Some(ArchiveEntry {
name: header.name.clone(),
compressed_size: header.compressed_size,
original_size: header.original_size,
compression_method: header.compression_method.clone(),
modified_time: header.date_time,
crc: header.crc16 as u64,
encryption: EncryptionMethod::None,
index: EntryIndex::Lha(header),
}))
} else {
Ok(None)
}
}
ArchiveInner::Zip(archive) => {
if let Some(header) = archive.get_next_entry()? {
let encryption = if header.is_encrypted {
EncryptionMethod::Zip(ZipEncryption::Unknown)
} else {
EncryptionMethod::None
};
Ok(Some(ArchiveEntry {
name: header.name.clone(),
compressed_size: header.compressed_size,
original_size: header.original_size,
compression_method: header.compression_method.clone(),
modified_time: header.date_time,
crc: header.crc32 as u64,
encryption,
index: EntryIndex::Zip(header),
}))
} else {
Ok(None)
}
}
ArchiveInner::Rar(archive) => {
if let Some(header) = archive.get_next_entry()? {
let encryption = if header.is_encrypted {
EncryptionMethod::Rar(RarEncryption::Unknown)
} else {
EncryptionMethod::None
};
Ok(Some(ArchiveEntry {
name: header.name.clone(),
compressed_size: header.compressed_size,
original_size: header.original_size,
compression_method: header.compression_method.clone(),
modified_time: header.date_time,
crc: header.crc32 as u64,
encryption,
index: EntryIndex::Rar(header),
}))
} else {
Ok(None)
}
}
ArchiveInner::SevenZ(archive) => {
if let Some(header) = archive.get_next_entry()? {
// 7z encrypts at block level - check if this file's block is encrypted
let encryption = if header.is_encrypted {
EncryptionMethod::SevenZ(crate::encryption::SevenZEncryption::Aes256)
} else {
EncryptionMethod::None
};
Ok(Some(ArchiveEntry {
name: header.name.clone(),
compressed_size: header.compressed_size,
original_size: header.original_size,
compression_method: header.compression_method.clone(),
modified_time: header.date_time,
crc: header.crc32 as u64,
encryption,
index: EntryIndex::SevenZ(header),
}))
} else {
Ok(None)
}
}
ArchiveInner::Tar(archive) => {
if let Some(header) = archive.get_next_entry()? {
Ok(Some(ArchiveEntry {
name: header.name.clone(),
compressed_size: header.size, // TAR doesn't compress
original_size: header.size,
compression_method: "Stored".to_string(),
modified_time: header.modified_time(),
crc: 0, // TAR doesn't use CRC
encryption: EncryptionMethod::None,
index: EntryIndex::Tar(header),
}))
} else {
Ok(None)
}
}
ArchiveInner::Tgz(archive) => {
if let Some(header) = archive.get_next_entry()? {
Ok(Some(ArchiveEntry {
name: header.name.clone(),
compressed_size: header.size, // Original TAR size (uncompressed)
original_size: header.size,
compression_method: "Gzip + Stored".to_string(),
modified_time: header.modified_time(),
crc: 0, // TAR doesn't use CRC
encryption: EncryptionMethod::None,
index: EntryIndex::Tgz(header),
}))
} else {
Ok(None)
}
}
ArchiveInner::Tbz(archive) => {
if let Some(header) = archive.get_next_entry()? {
Ok(Some(ArchiveEntry {
name: header.name.clone(),
compressed_size: header.size, // Original TAR size (uncompressed)
original_size: header.size,
compression_method: "Bzip2 + Stored".to_string(),
modified_time: header.modified_time(),
crc: 0, // TAR doesn't use CRC
encryption: EncryptionMethod::None,
index: EntryIndex::Tbz(header),
}))
} else {
Ok(None)
}
}
ArchiveInner::TarZ(archive) => {
if let Some(header) = archive.get_next_entry()? {
Ok(Some(ArchiveEntry {
name: header.name.clone(),
compressed_size: header.size, // Original TAR size (uncompressed)
original_size: header.size,
compression_method: "LZW + Stored".to_string(),
modified_time: header.modified_time(),
crc: 0, // TAR doesn't use CRC
encryption: EncryptionMethod::None,
index: EntryIndex::TarZ(header),
}))
} else {
Ok(None)
}
}
}
}
/// Read (decompress) an entry's data
///
/// # Example
/// ```no_run
/// use std::fs::File;
/// use unarc_rs::unified::{UnifiedArchive, ArchiveFormat};
///
/// let file = File::open("archive.arj").unwrap();
/// let mut archive = UnifiedArchive::open_with_format(file, ArchiveFormat::Arj).unwrap();
///
/// while let Some(entry) = archive.next_entry().unwrap() {
/// let data = archive.read(&entry).unwrap();
/// println!("Read {} bytes from {}", data.len(), entry.name());
/// }
/// ```
pub fn read(&mut self, entry: &ArchiveEntry) -> Result<Vec<u8>> {
match (&mut self.inner, &entry.index) {
(ArchiveInner::Ace(archive), EntryIndex::Ace(header)) => archive.read(header),
(ArchiveInner::Arc(archive), EntryIndex::Arc(header)) => archive.read(header),
(ArchiveInner::Arj(archive), EntryIndex::Arj(header)) => archive.read(header),
(ArchiveInner::Zoo(archive), EntryIndex::Zoo(header)) => archive.read(header),
(ArchiveInner::Sq(archive), EntryIndex::Sq(header)) => archive.read(header),
(ArchiveInner::Sqz(archive), EntryIndex::Sqz(header)) => archive.read(header),
(ArchiveInner::Z(archive, _), EntryIndex::Z) => archive.read(),
(ArchiveInner::Gz(archive, _), EntryIndex::Gz) => archive.read(),
(ArchiveInner::Bz2(archive, _), EntryIndex::Bz2) => archive.read(),
(ArchiveInner::Ice(archive, _), EntryIndex::Ice) => archive.read(),
(ArchiveInner::PackIce(archive, _), EntryIndex::PackIce) => archive.read(),
(ArchiveInner::Hyp(archive), EntryIndex::Hyp(header)) => archive.read(header),
(ArchiveInner::Ha(archive), EntryIndex::Ha(header)) => archive.read(header),
(ArchiveInner::Uc2(archive), EntryIndex::Uc2(header)) => archive.read(header),
(ArchiveInner::Lha(archive), EntryIndex::Lha(header)) => archive.read(header),
(ArchiveInner::Zip(archive), EntryIndex::Zip(header)) => archive.read(header),
(ArchiveInner::Rar(archive), EntryIndex::Rar(header)) => archive.read(header),
(ArchiveInner::SevenZ(archive), EntryIndex::SevenZ(header)) => archive.read(header),
(ArchiveInner::Tar(archive), EntryIndex::Tar(header)) => archive.read(header),
(ArchiveInner::Tgz(archive), EntryIndex::Tgz(header)) => archive.read(header),
(ArchiveInner::Tbz(archive), EntryIndex::Tbz(header)) => archive.read(header),
(ArchiveInner::TarZ(archive), EntryIndex::TarZ(header)) => archive.read(header),
_ => Err(ArchiveError::IndexMismatch("Entry does not belong to this archive".to_string())),
}
}
/// Read (decompress) an entry's data and write it directly to a writer
///
/// This is more memory-efficient than `read()` for large files, as it streams
/// the data directly to the output without buffering the entire file in memory.
///
/// Returns the number of bytes written.
///
/// # Example
/// ```no_run
/// use std::fs::File;
/// use unarc_rs::unified::ArchiveFormat;
///
/// let mut archive = ArchiveFormat::open_path("archive.arj").unwrap();
///
/// while let Some(entry) = archive.next_entry().unwrap() {
/// let mut output = File::create(entry.file_name()).unwrap();
/// let bytes_written = archive.read_to(&entry, &mut output).unwrap();
/// println!("Extracted {} bytes to {}", bytes_written, entry.file_name());
/// }
/// ```
pub fn read_to<W: Write>(&mut self, entry: &ArchiveEntry, writer: &mut W) -> Result<u64> {
// For now, we use the existing read() method and write the result.
// In the future, individual archive implementations could provide
// streaming variants for better memory efficiency.
let data = self.read(entry)?;
writer.write_all(&data)?;
Ok(data.len() as u64)
}
/// Read (decompress) an entry's data with specific options
///
/// This allows passing a different password or CRC verification setting
/// for a specific read operation without changing the archive's default options.
///
/// # Example
/// ```no_run
/// use std::fs::File;
/// use unarc_rs::unified::{UnifiedArchive, ArchiveFormat, ArchiveOptions};
///
/// let file = File::open("archive.zip").unwrap();
/// let mut archive = UnifiedArchive::open_with_format(file, ArchiveFormat::Zip).unwrap();
///
/// while let Some(entry) = archive.next_entry().unwrap() {
/// // Read with a specific password for this entry
/// let options = ArchiveOptions::new()
/// .with_password("file_specific_password")
/// .with_verify_crc(true);
/// let data = archive.read_with_options(&entry, &options).unwrap();
/// println!("Read {} bytes from {}", data.len(), entry.name());
/// }
/// ```
pub fn read_with_options(&mut self, entry: &ArchiveEntry, options: &ArchiveOptions) -> Result<Vec<u8>> {
let password = options.password().map(|s| s.to_string());
match (&mut self.inner, &entry.index) {
(ArchiveInner::Ace(archive), EntryIndex::Ace(header)) => archive.read_with_password(header, password),
(ArchiveInner::Arc(archive), EntryIndex::Arc(header)) => archive.read_with_password(header, password),
(ArchiveInner::Zip(archive), EntryIndex::Zip(header)) => archive.read_with_password(header, password.as_ref().map(|s| s.as_bytes())),
(ArchiveInner::Rar(archive), EntryIndex::Rar(header)) => archive.read_with_password(header, password),
(ArchiveInner::SevenZ(archive), EntryIndex::SevenZ(header)) => archive.read_with_password(header, password),
(ArchiveInner::Arj(archive), EntryIndex::Arj(header)) => archive.read_with_password(header, password),
// Other formats don't support encryption, use normal read
_ => self.read(entry),
}
}
/// Read (decompress) an entry's data with options and write directly to a writer
///
/// This combines the memory efficiency of `read_to` with the flexibility
/// of per-read options.
pub fn read_to_with_options<W: Write>(&mut self, entry: &ArchiveEntry, writer: &mut W, options: &ArchiveOptions) -> Result<u64> {
let data = self.read_with_options(entry, options)?;
writer.write_all(&data)?;
Ok(data.len() as u64)
}
/// Skip an entry without reading its data
///
/// This is more efficient than reading if you only need to process certain files.
pub fn skip(&mut self, entry: &ArchiveEntry) -> Result<()> {
match (&mut self.inner, &entry.index) {
(ArchiveInner::Ace(archive), EntryIndex::Ace(header)) => archive.skip(header),
(ArchiveInner::Arc(archive), EntryIndex::Arc(header)) => archive.skip(header),
(ArchiveInner::Arj(archive), EntryIndex::Arj(header)) => archive.skip(header),
(ArchiveInner::Zoo(archive), EntryIndex::Zoo(header)) => archive.skip(header),
(ArchiveInner::Sq(archive), EntryIndex::Sq(header)) => archive.skip(header),
(ArchiveInner::Sqz(archive), EntryIndex::Sqz(header)) => archive.skip(header),
(ArchiveInner::Z(archive, _), EntryIndex::Z) => archive.skip(),
(ArchiveInner::Gz(archive, _), EntryIndex::Gz) => archive.skip(),
(ArchiveInner::Bz2(archive, _), EntryIndex::Bz2) => archive.skip(),
(ArchiveInner::Ice(archive, _), EntryIndex::Ice) => archive.skip(),
(ArchiveInner::PackIce(archive, _), EntryIndex::PackIce) => archive.skip(),
(ArchiveInner::Hyp(archive), EntryIndex::Hyp(header)) => archive.skip(header),
(ArchiveInner::Ha(archive), EntryIndex::Ha(header)) => archive.skip(header),
(ArchiveInner::Uc2(archive), EntryIndex::Uc2(header)) => archive.skip(header),
(ArchiveInner::Lha(archive), EntryIndex::Lha(header)) => archive.skip(header),
(ArchiveInner::Zip(archive), EntryIndex::Zip(header)) => archive.skip(header),
(ArchiveInner::Rar(archive), EntryIndex::Rar(header)) => archive.skip(header),
(ArchiveInner::SevenZ(archive), EntryIndex::SevenZ(header)) => archive.skip(header),
(ArchiveInner::Tar(archive), EntryIndex::Tar(header)) => archive.skip(header),
(ArchiveInner::Tgz(archive), EntryIndex::Tgz(header)) => archive.skip(header),
(ArchiveInner::Tbz(archive), EntryIndex::Tbz(header)) => archive.skip(header),
(ArchiveInner::TarZ(archive), EntryIndex::TarZ(header)) => archive.skip(header),
_ => Err(ArchiveError::IndexMismatch("Entry does not belong to this archive".to_string())),
}
}
/// Collect all entries into a vector
///
/// Note: After calling this, you'll need to re-open the archive to read entries.
/// This method is useful when you need to know all entries upfront.
pub fn entries(&mut self) -> Result<Vec<ArchiveEntry>> {
let mut entries = Vec::new();
while let Some(entry) = self.next_entry()? {
entries.push(entry.clone());
self.skip(&entry)?;
}
Ok(entries)
}
/// Returns an iterator over all entries in the archive
///
/// This is the idiomatic way to iterate over archive entries.
/// The iterator automatically skips entries after yielding them.
///
/// # Example
/// ```no_run
/// use unarc_rs::unified::ArchiveFormat;
///
/// let mut archive = ArchiveFormat::open_path("archive.arj").unwrap();
///
/// for entry in archive.entries_iter() {
/// let entry = entry.unwrap();
/// println!("{}: {} bytes", entry.name(), entry.original_size());
/// }
/// ```
pub fn entries_iter(&mut self) -> ArchiveEntryIter<'_, T> {
ArchiveEntryIter { archive: self }
}
}
/// Check if a file path appears to be a supported archive based on extension
///
/// # Example
/// ```
/// use std::path::Path;
/// use unarc_rs::unified::is_supported_archive;
///
/// assert!(is_supported_archive(Path::new("file.arj")));
/// assert!(is_supported_archive(Path::new("file.zoo")));
/// assert!(!is_supported_archive(Path::new("file.txt")));
/// ```
pub fn is_supported_archive(path: &Path) -> bool {
ArchiveFormat::from_path(path).is_some()
}
/// Get all supported file extensions
///
/// # Example
/// ```
/// use unarc_rs::unified::supported_extensions;
///
/// let exts = supported_extensions();
/// assert!(exts.contains(&"arj"));
/// assert!(exts.contains(&"zoo"));
/// ```
pub fn supported_extensions() -> Vec<&'static str> {
ArchiveFormat::ALL.iter().flat_map(|f| f.extensions().iter().copied()).collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_format_from_extension() {
assert_eq!(ArchiveFormat::from_extension("arj"), Some(ArchiveFormat::Arj));
assert_eq!(ArchiveFormat::from_extension("ARJ"), Some(ArchiveFormat::Arj));
assert_eq!(ArchiveFormat::from_extension("zoo"), Some(ArchiveFormat::Zoo));
assert_eq!(ArchiveFormat::from_extension("arc"), Some(ArchiveFormat::Arc));
assert_eq!(ArchiveFormat::from_extension("sq"), Some(ArchiveFormat::Sq));
assert_eq!(ArchiveFormat::from_extension("sq2"), Some(ArchiveFormat::Sq));
assert_eq!(ArchiveFormat::from_extension("qqq"), Some(ArchiveFormat::Sq));
assert_eq!(ArchiveFormat::from_extension("bqk"), Some(ArchiveFormat::Sq)); // ?Q? pattern
assert_eq!(ArchiveFormat::from_extension("sqz"), Some(ArchiveFormat::Sqz));
assert_eq!(ArchiveFormat::from_extension("Z"), Some(ArchiveFormat::Z));
assert_eq!(ArchiveFormat::from_extension("hyp"), Some(ArchiveFormat::Hyp));
assert_eq!(ArchiveFormat::from_extension("txt"), None);
assert_eq!(ArchiveFormat::from_extension("zip"), Some(ArchiveFormat::Zip));
assert_eq!(ArchiveFormat::from_extension("rar"), Some(ArchiveFormat::Rar));
assert_eq!(ArchiveFormat::from_extension("7z"), Some(ArchiveFormat::SevenZ));
}
#[test]
fn test_format_from_path() {
assert_eq!(ArchiveFormat::from_path(Path::new("test.arj")), Some(ArchiveFormat::Arj));
assert_eq!(ArchiveFormat::from_path(Path::new("/path/to/archive.zoo")), Some(ArchiveFormat::Zoo));
assert_eq!(ArchiveFormat::from_path(Path::new("C:\\files\\data.arc")), Some(ArchiveFormat::Arc));
assert_eq!(ArchiveFormat::from_path(Path::new("noext")), None);
}
#[test]
fn test_is_supported_archive() {
assert!(is_supported_archive(Path::new("file.arj")));
assert!(is_supported_archive(Path::new("file.zoo")));
assert!(is_supported_archive(Path::new("file.arc")));
assert!(is_supported_archive(Path::new("file.zip")));
assert!(is_supported_archive(Path::new("file.rar")));
assert!(is_supported_archive(Path::new("file.7z")));
assert!(!is_supported_archive(Path::new("file.txt")));
}
#[test]
fn test_supported_extensions() {
let exts = supported_extensions();
assert!(exts.contains(&"arj"));
assert!(exts.contains(&"zoo"));
assert!(exts.contains(&"arc"));
assert!(exts.contains(&"sq"));
assert!(exts.contains(&"sqz"));
assert!(exts.contains(&"Z"));
assert!(exts.contains(&"gz"));
assert!(exts.contains(&"bz2"));
assert!(exts.contains(&"hyp"));
assert!(exts.contains(&"rar"));
assert!(exts.contains(&"7z"));
}
#[test]
fn test_gz_bz2_format_detection() {
// Test .gz as single file format
assert_eq!(ArchiveFormat::from_extension("gz"), Some(ArchiveFormat::Gz));
assert_eq!(ArchiveFormat::from_extension("GZ"), Some(ArchiveFormat::Gz));
// Test .bz2 as single file format
assert_eq!(ArchiveFormat::from_extension("bz2"), Some(ArchiveFormat::Bz2));
assert_eq!(ArchiveFormat::from_extension("BZ2"), Some(ArchiveFormat::Bz2));
// Test path detection
assert_eq!(ArchiveFormat::from_path(Path::new("file.gz")), Some(ArchiveFormat::Gz));
assert_eq!(ArchiveFormat::from_path(Path::new("file.bz2")), Some(ArchiveFormat::Bz2));
// Test .tar.gz still detects as Tgz (tar archive)
assert_eq!(ArchiveFormat::from_path(Path::new("file.tar.gz")), Some(ArchiveFormat::Tgz));
assert_eq!(ArchiveFormat::from_path(Path::new("file.tar.bz2")), Some(ArchiveFormat::Tbz));
}
#[test]
fn test_entry_file_name() {
let entry = ArchiveEntry {
name: "path/to/file.txt".to_string(),
compressed_size: 100,
original_size: 200,
compression_method: "Stored".to_string(),
modified_time: None,
crc: 0,
encryption: EncryptionMethod::None,
index: EntryIndex::Z,
};
assert_eq!(entry.file_name(), "file.txt");
let entry2 = ArchiveEntry {
name: "simple.txt".to_string(),
compressed_size: 100,
original_size: 200,
compression_method: "Stored".to_string(),
modified_time: None,
crc: 0,
encryption: EncryptionMethod::None,
index: EntryIndex::Z,
};
assert_eq!(entry2.file_name(), "simple.txt");
}
#[test]
fn test_compression_ratio() {
let entry = ArchiveEntry {
name: "test".to_string(),
compressed_size: 50,
original_size: 100,
compression_method: "LZW".to_string(),
modified_time: None,
crc: 0,
encryption: EncryptionMethod::None,
index: EntryIndex::Z,
};
assert!((entry.compression_ratio() - 0.5).abs() < 0.001);
let empty = ArchiveEntry {
name: "empty".to_string(),
compressed_size: 0,
original_size: 0,
compression_method: "Stored".to_string(),
modified_time: None,
crc: 0,
encryption: EncryptionMethod::None,
index: EntryIndex::Z,
};
assert!((empty.compression_ratio() - 1.0).abs() < 0.001);
}
#[test]
fn test_preambles() {
// Test formats with single preamble
assert_eq!(ArchiveFormat::Arj.preambles(), Some(&[&[0x60u8, 0xEA][..]][..]));
assert_eq!(ArchiveFormat::Sqz.preambles(), Some(&[b"HLSQZ".as_slice()][..]));
assert_eq!(ArchiveFormat::Ha.preambles(), Some(&[b"HA".as_slice()][..]));
// Test formats with multiple preambles
assert_eq!(ArchiveFormat::Sq.preambles(), Some(&[&[0x76u8, 0xFF][..], &[0xFAu8, 0xFF][..]][..]));
assert_eq!(ArchiveFormat::Hyp.preambles(), Some(&[b"HP".as_slice(), b"ST".as_slice()][..]));
assert_eq!(ArchiveFormat::Lha.preambles(), Some(&[b"-lh".as_slice(), b"-lz".as_slice()][..]));
assert_eq!(ArchiveFormat::Zip.preambles(), Some(&[b"PK\x03\x04".as_slice(), b"PK\x05\x06".as_slice()][..]));
// Test formats with offset > 0
assert_eq!(ArchiveFormat::Lha.preamble_offset(), 2);
assert_eq!(ArchiveFormat::Ace.preamble_offset(), 7);
assert_eq!(ArchiveFormat::Tar.preamble_offset(), 257);
assert_eq!(ArchiveFormat::Arj.preamble_offset(), 0);
// ICE has no fixed magic
assert_eq!(ArchiveFormat::Ice.preambles(), None);
}
#[test]
fn test_detect_from_bytes() {
// Test ZIP detection
assert_eq!(ArchiveFormat::detect_from_bytes(b"PK\x03\x04rest of data"), Some(ArchiveFormat::Zip));
// Test ARJ detection
assert_eq!(ArchiveFormat::detect_from_bytes(&[0x60, 0xEA, 0x00, 0x00]), Some(ArchiveFormat::Arj));
// Test RAR detection
assert_eq!(ArchiveFormat::detect_from_bytes(b"Rar!\x1a\x07\x00rest"), Some(ArchiveFormat::Rar));
// Test 7z detection
assert_eq!(
ArchiveFormat::detect_from_bytes(&[0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C, 0x00]),
Some(ArchiveFormat::SevenZ)
);
// Test gzip detection
assert_eq!(ArchiveFormat::detect_from_bytes(&[0x1F, 0x8B, 0x08, 0x00]), Some(ArchiveFormat::Gz));
// Test bzip2 detection
assert_eq!(ArchiveFormat::detect_from_bytes(b"BZh9data"), Some(ArchiveFormat::Bz2));
// Test SQZ detection
assert_eq!(ArchiveFormat::detect_from_bytes(b"HLSQZrest"), Some(ArchiveFormat::Sqz));
// Test HA detection
assert_eq!(ArchiveFormat::detect_from_bytes(b"HArest"), Some(ArchiveFormat::Ha));
// Test ARC detection (0x1A + method 1-11)
assert_eq!(ArchiveFormat::detect_from_bytes(&[0x1A, 0x02, 0x00]), Some(ArchiveFormat::Arc));
// Test ZOO detection
assert_eq!(ArchiveFormat::detect_from_bytes(b"ZOO 2.10 Archive."), Some(ArchiveFormat::Zoo));
// Test ACE detection (magic at offset 7)
let mut ace_data = vec![0u8; 20];
ace_data[7..14].copy_from_slice(b"**ACE**");
assert_eq!(ArchiveFormat::detect_from_bytes(&ace_data), Some(ArchiveFormat::Ace));
// Test LHA detection (magic at offset 2)
assert_eq!(ArchiveFormat::detect_from_bytes(b"\x00\x00-lh5-rest"), Some(ArchiveFormat::Lha));
// Test UC2 detection
assert_eq!(ArchiveFormat::detect_from_bytes(b"UC2\x1arest"), Some(ArchiveFormat::Uc2));
// Test UE2 (UltraCrypt) detection as UC2
assert_eq!(ArchiveFormat::detect_from_bytes(b"UE2\x01rest"), Some(ArchiveFormat::Uc2));
// Test unknown format
assert_eq!(ArchiveFormat::detect_from_bytes(b"random data here"), None);
// Test too short data
assert_eq!(ArchiveFormat::detect_from_bytes(b"X"), None);
assert_eq!(ArchiveFormat::detect_from_bytes(b""), None);
}
}