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
use Mmap;
use *;
use HashMap;
use ;
use ;
use Path;
use PathBuf;
use ;
use ZipArchive;
use ZipFile;
/// Extracts the contents of a ZIP archive into a target directory using memory-mapped I/O and parallelism.
///
/// # Arguments
/// - `zip_path`: Path to the ZIP file to be extracted.
/// - `extract_path`: Path to the directory where files will be extracted.
/// - `password`: Optional password used to decrypt encrypted entries. If `None`, only
/// unencrypted files will be accessible.
///
/// # Behavior
/// - Uses [`memmap2`](https://docs.rs/memmap2/latest/memmap2/) to memory-map the entire ZIP file for efficient random access.
/// - Uses [`rayon`](https://docs.rs/rayon/latest/rayon/) to extract files in parallel.
/// - Attempts decryption with [`by_index_decrypt`](https://docs.rs/zip/latest/zip/read/struct.ZipArchive.html#method.by_index_decrypt) if `password` is provided.
/// - Recreates directory structure as found in the ZIP archive.
/// - Preserves relative paths; directory traversal protection (e.g., stripping `../`) should be added externally if required.
///
/// # Performance
/// - Each parallel task re-initializes its own `ZipArchive` view over the shared memory-mapped file.
/// - This avoids contention but increases overhead for archives with many entries.
///
/// # Errors
/// Returns an error if:
/// - The ZIP file cannot be opened or memory-mapped.
/// - The archive is corrupted or unreadable.
/// - A file cannot be decrypted with the provided password.
/// - Directories or files cannot be created under `extract_path`.
/// - File write operations fail.
///
/// # Security Notes
/// - Only legacy ZipCrypto is supported for decryption. This scheme is weak and may
/// incorrectly accept invalid passwords due to ZIP spec limitations.
/// - AES-encrypted ZIPs may not be supported. Test with your archives before relying on this in production.
///
/// # Panics
/// - Panics if a file entry in the ZIP archive does not have a valid parent directory path.
///
/// # Example
/// ```rust,no_run
/// use zipoxide::extract_zip;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Extract unencrypted archive
/// extract_zip("archive.zip".to_string(), "output".to_string(), None)?;
///
/// // Extract password-protected archive
/// extract_zip("secret.zip".to_string(), "output".to_string(), Some("hunter2".to_string()))?;
///
/// Ok(())
/// }
/// ```
/// Reads all files inside a ZIP archive into memory buffers in parallel,
/// returning a `HashMap` where keys are file names and values are file contents.
///
/// # Arguments
/// - `zip_path`: Path to the ZIP file to be read.
/// - `password`: Optional password used to decrypt encrypted files. If `None`, only
/// unencrypted entries will be accessible.
///
/// # Behavior
/// - Uses [`memmap2`](https://docs.rs/memmap2/latest/memmap2/) to memory-map the entire ZIP file for efficient random access.
/// - Uses [`rayon`](https://docs.rs/rayon/latest/rayon/) to read files in parallel.
/// - If `password` is provided, attempts to decrypt each entry with [`by_index_decrypt`](https://docs.rs/zip/latest/zip/read/struct.ZipArchive.html#method.by_index_decrypt).
/// - Stores each file's full contents into a `Vec<u8>` in memory.
/// - File names are taken directly from the ZIP archive’s metadata (UTF-8 required).
///
/// # Concurrency Model
/// - Each worker thread creates its own `ZipArchive` view over the shared memory-mapped file.
/// - Results are collected in a thread-safe `Arc<Mutex<HashMap<...>>>` before being unwrapped.
/// - Suitable for archives with many medium-sized files; overhead may dominate if there are only a few entries.
///
/// # Returns
/// A `HashMap<String, Vec<u8>>` where:
/// - Key = file name inside the ZIP archive.
/// - Value = full file contents as bytes.
///
/// # Errors
/// Returns an error if:
/// - The ZIP file cannot be opened or memory-mapped.
/// - The archive is corrupted or unreadable.
/// - A file cannot be extracted or decrypted (wrong password).
///
/// # Panics
/// - Panics if the `Arc<Mutex<_>>` cannot be unwrapped (only occurs if still shared, which should not happen here).
///
/// # Security Notes
/// - Only legacy ZipCrypto is supported for decryption. This scheme is weak and may
/// incorrectly accept invalid passwords due to ZIP spec limitations.
/// - AES-encrypted ZIP files may not be supported. Test with your target archives.
///
/// # Example
/// ```rust,no_run
/// use zipoxide::read_zip_contents_into_buffer;
///
/// fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// // Unencrypted ZIP
/// let plain = read_zip_contents_into_buffer("plain.zip".to_string(), None)?;
/// println!("Read {} files", plain.len());
///
/// // Encrypted ZIP
/// let secret = read_zip_contents_into_buffer("secret.zip".to_string(), Some("hunter2".to_string()))?;
/// for (name, data) in secret {
/// println!("File: {}, Size: {} bytes", name, data.len());
/// }
/// Ok(())
/// }
/// ```