unrar-ng 0.7.3

list and extract RAR archives. Actively maintained fork of unrar.
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
use crate::error::*;
use crate::open_archive::{CursorBeforeHeader, List, ListSplit, OpenArchive, OpenMode, Process};
use regex::Regex;
use std::borrow::Cow;
use std::iter::repeat;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;

fn multipart_extension() -> &'static Regex {
    static INSTANCE: OnceLock<Regex> = OnceLock::new();
    INSTANCE.get_or_init(|| Regex::new(r"(\.part|\.r?)(\d+)((?:\.rar)?)$").unwrap())
}

fn extension() -> &'static Regex {
    static INSTANCE: OnceLock<Regex> = OnceLock::new();
    INSTANCE.get_or_init(|| Regex::new(r"(\.part|\.r?)(\d+)((?:\.rar)?)$|\.rar$").unwrap())
}

/// A RAR archive on the file system.
///
/// This struct provides two major classes of methods:
///    1. methods that do not touch the FS. These are opinionated utility methods
///         that are based on RAR path conventions out in the wild. Most commonly, multipart
///         files usually have extensions such as `.part08.rar` or `.r08.rar`. Since extracting
///         must start at the first part, it may be helpful to figure that out using, for instance,
///         [`archive.as_first_part()`](Archive::as_first_part)
///    2. methods that open the underlying path in the specified mode
///         (possible modes are [`List`], [`ListSplit`] and [`Process`]).
///         These methods have the word `open` in them, are fallible operations,
///         return [`OpenArchive`](struct.OpenArchive.html) inside a `Result` and are as follows:
///         - [`open_for_listing`](Archive::open_for_listing) and
///             [`open_for_listing_split`](Archive::open_for_listing_split): list the archive
///             entries (skipping over content/payload)
///         - [`open_for_processing`](Archive::open_for_processing): process archive entries
///             as well as content/payload
///         - [`break_open`](Archive::break_open): read archive even if an error is returned,
///             if possible. The [`OpenMode`](open_archive/struct.OpenMode.html) must be provided
///             explicitly.
pub struct Archive<'a> {
    filename: Cow<'a, Path>,
    password: Option<&'a [u8]>,
    comments: Option<&'a mut Vec<u8>>,
}

pub type Glob = PathBuf;

impl<'a> Archive<'a> {
    /// Creates an `Archive` object to operate on a plain non-encrypted RAR archive.
    pub fn new<T>(file: &'a T) -> Self
    where
        T: AsRef<Path> + ?Sized,
    {
        Archive {
            filename: Cow::Borrowed(file.as_ref()),
            password: None,
            comments: None,
        }
    }

    /// Creates an `Archive` object to operate on a password encrypted RAR archive.
    pub fn with_password<F, Pw>(file: &'a F, password: &'a Pw) -> Self
    where
        F: AsRef<Path> + ?Sized,
        Pw: AsRef<[u8]> + ?Sized,
    {
        Archive {
            filename: Cow::Borrowed(file.as_ref()),
            password: Some(password.as_ref()),
            comments: None,
        }
    }
    
    /// Creates an `Archive` object to operate on a plain non-encrypted RAR archive.
    /// as opposed to [`new`](struct.Archive.html#method.new) that borrows from its input, this function takes ownership of it,
    /// potentially cloning if the input is a reference.
    /// this function is lifetime-free, allowing the creation of an `Archive<'static>`
    ///  with dynamically constructed Paths.
    pub fn new_owned<T>(file: T) -> Self
    where
        T: Into<PathBuf>,
    {
        Archive {
            filename: Cow::Owned(file.into()),
            password: None,
            comments: None,
        }
    }

    /// returns the archive's path
    pub fn filename(&self) -> &Path {
        &self.filename
    }

    /// Set the comment buffer of the underlying archive.
    /// Note: Comments are not supported yet so this method will have no effect.
    pub fn set_comments(&mut self, comments: &'a mut Vec<u8>) {
        self.comments = Some(comments);
    }

    /// Returns `true` if the filename matches a RAR archive.
    ///
    /// This method does not make any FS operations and operates purely on strings.
    pub fn is_archive(&self) -> bool {
        is_archive(&self.filename)
    }

    /// Returns `true` if the filename matches a part of a multipart collection, `false` otherwise
    ///
    /// This method does not make any FS operations and operates purely on strings.
    pub fn is_multipart(&self) -> bool {
        is_multipart(&self.filename)
    }

    /// Returns a glob string covering all parts of the multipart collection or `None` if the
    /// underlying archive does not appear to be a multipart archive (based solely on filename).
    ///
    /// This method does not make any FS operations and operates purely on strings.
    ///
    /// # Example
    ///
    /// Basic usage (multipart archive):
    ///
    /// ```
    /// # use unrar_ng::Archive;
    /// # use std::path::PathBuf;
    /// let glob = Archive::new("path/my.archive.part01.rar").all_parts_option();
    ///
    /// assert_eq!(glob, Some(PathBuf::from("path/my.archive.part??.rar")));
    /// ```
    ///
    /// Single part archive:
    /// ```
    /// # use unrar_ng::Archive;
    /// let glob = Archive::new("path/my.archive.rar").all_parts_option();
    ///
    /// assert_eq!(glob, None);
    /// ```
    pub fn all_parts_option(&self) -> Option<Glob> {
        get_rar_extension(&self.filename)
            .and_then(|full_ext| {
                multipart_extension().captures(&full_ext).map(|captures| {
                    let mut replacement = String::from(captures.get(1).unwrap().as_str());
                    replacement.push_str(
                        &repeat("?")
                            .take(captures.get(2).unwrap().as_str().len())
                            .collect::<String>(),
                    );
                    replacement.push_str(captures.get(3).unwrap().as_str());
                    full_ext.replace(captures.get(0).unwrap().as_str(), &replacement)
                })
            })
            .and_then(|new_ext| {
                self.filename.file_stem().map(|x| {
                    self.filename
                        .with_file_name(Path::new(x).with_extension(&new_ext[1..]))
                })
            })
    }

    /// Returns a glob string covering all parts of the multipart collection or `self.filename` if
    /// the underlying archive does not appear to be a multipart archive (based solely on filename).
    ///
    /// This method does not make any FS operations and operates purely on strings.
    ///
    /// # Examples
    ///
    /// Basic usage (multipart archive):
    ///
    /// ```
    /// # use unrar_ng::Archive;
    /// # use std::path::PathBuf;
    /// let glob = Archive::new("path/my.archive.part01.rar").all_parts();
    ///
    /// assert_eq!(glob, PathBuf::from("path/my.archive.part??.rar"));
    /// ```
    ///
    /// Single part archive:
    /// ```
    /// # use unrar_ng::Archive;
    /// # use std::path::PathBuf;
    /// let glob = Archive::new("path/my.archive.rar").all_parts();
    ///
    /// assert_eq!(glob, PathBuf::from("path/my.archive.rar"));
    /// ```
    pub fn all_parts(&self) -> Glob {
        match self.all_parts_option() {
            Some(x) => x,
            None => self.filename.to_path_buf(),
        }
    }

    /// Returns the nth part of this multi-part collection or `None` if
    /// the underlying archive does not appear to be a multipart archive (based solely on filename).
    ///
    /// This method does not make any FS operations and operates purely on strings.
    ///
    /// # Examples
    ///
    /// Simple usage:
    ///
    /// ```
    /// # use unrar_ng::Archive;
    /// # use std::path::PathBuf;
    /// let part42 = Archive::new("path/my.archive.part01.rar").nth_part(42).unwrap();
    ///
    /// assert_eq!(part42, PathBuf::from("path/my.archive.part42.rar"));
    /// ```
    ///
    /// Returns None for single-part archives:
    ///
    /// ```
    /// # use unrar_ng::Archive;
    /// let part42 = Archive::new("path/my.archive.rar").nth_part(42);
    ///
    /// assert_eq!(part42, None);
    /// ```
    pub fn nth_part(&self, n: i32) -> Option<PathBuf> {
        get_rar_extension(&self.filename)
            .and_then(|full_ext| {
                multipart_extension().captures(&full_ext).map(|captures| {
                    let mut replacement = String::from(captures.get(1).unwrap().as_str());
                    // `n` padded with zeroes to the length of archive's number's length
                    replacement.push_str(&format!(
                        "{:01$}",
                        n,
                        captures.get(2).unwrap().as_str().len()
                    ));
                    replacement.push_str(captures.get(3).unwrap().as_str());
                    full_ext.replace(captures.get(0).unwrap().as_str(), &replacement)
                })
            })
            .and_then(|new_ext| {
                self.filename.file_stem().map(|x| {
                    self.filename
                        .with_file_name(Path::new(x).with_extension(&new_ext[1..]))
                })
            })
    }

    /// Return the first part of the multipart collection or `None` if
    /// the underlying archive does not appear to be a multipart archive (based solely on filename).
    ///
    /// This method does not make any FS operations and operates purely on strings.
    ///
    /// Equivalent to [`nth_part(1)`](Archive::nth_part).
    ///
    /// # Examples
    ///
    /// Simple usage:
    ///
    /// ```
    /// # use unrar_ng::Archive;
    /// # use std::path::PathBuf;
    /// let part1 = Archive::new("path/my.archive.part42.rar").first_part_option().unwrap();
    ///
    /// assert_eq!(part1, PathBuf::from("path/my.archive.part01.rar"));
    /// ```
    ///
    /// Returns None for single-part archives:
    ///
    /// ```
    /// # use unrar_ng::Archive;
    /// let part1 = Archive::new("path/my.archive.rar").first_part_option();
    ///
    /// assert_eq!(part1, None);
    /// ```
    pub fn first_part_option(&self) -> Option<PathBuf> {
        self.nth_part(1)
    }

    /// Returns the first part of the multipart collection or `self.filename` if
    /// the underlying archive does not appear to be a multipart archive (based solely on filename).
    ///
    /// This method does not make any FS operations and operates purely on strings.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # use unrar_ng::Archive;
    /// # use std::path::PathBuf;
    /// let part1 = Archive::new("path/archive.part33.rar").first_part();
    ///
    /// assert_eq!(part1, PathBuf::from("path/archive.part01.rar"));
    /// ```
    ///
    /// Single part archive:
    ///
    /// ```
    /// # use unrar_ng::Archive;
    /// # use std::path::PathBuf;
    /// let part1 = Archive::new("path/archive.rar").first_part();
    ///
    /// assert_eq!(part1, PathBuf::from("path/archive.rar"));
    /// ```
    ///
    /// Note that this will always return the underlying path
    /// if a first part could not be found:
    ///
    /// ```
    /// # use unrar_ng::Archive;
    /// # use std::path::PathBuf;
    /// let part1 = Archive::new("https://gibberish").first_part();
    ///
    /// assert_eq!(part1, PathBuf::from("https://gibberish"));
    /// ```
    pub fn first_part(&self) -> PathBuf {
        match self.nth_part(1) {
            Some(x) => x,
            None => self.filename.to_path_buf(),
        }
    }

    /// Changes the filename to point to the first part of the multipart collection. Does nothing if
    /// the underlying archive does not appear to be a multipart archive (based solely on filename).
    ///
    /// This method does not make any FS operations and operates purely on strings.
    ///
    /// # Example
    ///
    /// Basic usage:
    ///
    /// ```
    /// # use unrar_ng::Archive;
    /// # use std::path::PathBuf;
    /// let mut archive = Archive::new("path/some.004.rar").as_first_part();
    /// assert_eq!(archive.filename(), PathBuf::from("path/some.001.rar"));
    /// ```
    pub fn as_first_part(mut self) -> Self {
        self.first_part_option()
            .map(|fp| self.filename = Cow::Owned(fp));
        self
    }

    /// Opens the underlying archive for processing, that is, the payloads of each archive entry can be
    /// actively read. What actually happens with individual entries (e.g. read, extract, skip, test),
    /// can be specified during processing.
    ///
    /// See also: [`Process`]
    ///
    /// # Panics
    ///
    /// Panics if `self.filename` contains nul values.
    pub fn open_for_processing(self) -> UnrarResult<OpenArchive<Process, CursorBeforeHeader>> {
        self.open(None)
    }

    /// Opens the underlying archive for listing its entries, i.e. the payloads are skipped automatically.
    ///
    /// See also: [`List`]
    ///
    /// # Panics
    ///
    /// Panics if `self.filename` contains nul values.
    pub fn open_for_listing(self) -> UnrarResult<OpenArchive<List, CursorBeforeHeader>> {
        self.open(None)
    }

    /// Opens the underlying archive for listing its entries without omitting or pooling split entries.
    /// For a multipart archive, this means a file spanning the border of 2 parts will appear twice,
    /// or even more often than that if it spans multiple parts, whereas in the normal list, it will
    /// appear once.
    ///
    /// See also: [`ListSplit`]
    ///
    /// # Panics
    ///
    /// Panics if `self.filename` contains nul values.

    pub fn open_for_listing_split(self) -> UnrarResult<OpenArchive<ListSplit, CursorBeforeHeader>> {
        self.open(None)
    }

    /// Opens the underlying archive with the provided parameters.
    ///
    /// # Panics
    ///
    /// Panics if `path` contains nul values.
    fn open<M: OpenMode>(
        self,
        recover: Option<&mut Option<OpenArchive<M, CursorBeforeHeader>>>,
    ) -> UnrarResult<OpenArchive<M, CursorBeforeHeader>> {
        OpenArchive::new(&self.filename, self.password, recover)
    }

    /// Opens the underlying archive with the provided OpenMode,
    /// even if archive is broken (e.g. malformed header).
    ///
    /// Provide an optional mutable reference for book-keeping, to check whether an error
    /// did occur. Note that this error will never be set if an Err is returned, i.e. if we
    /// were not able to read the archive.
    ///
    /// # Example: I don't care if there was a recoverable error
    ///
    /// ```no_run
    /// # use unrar_ng::{Archive, List, UnrarResult};
    /// # fn x() -> UnrarResult<()> {
    /// let mut open_archive = Archive::new("file").break_open::<List>(None)?;
    /// // use open_archive
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Example: I want to know if there was a recoverable error
    ///
    /// ```no_run
    /// # use unrar_ng::{Archive, List, UnrarResult};
    /// # fn x() -> UnrarResult<()> {
    /// let mut possible_error = None;
    /// let mut open_archive = Archive::new("file").break_open::<List>(Some(&mut possible_error))?;
    /// // check the error, e.g.:
    /// possible_error.map(|error| eprintln!("recoverable error occurred: {error}"));
    /// // use open_archive
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if `path` contains nul values.
    pub fn break_open<M: OpenMode>(
        self,
        error: Option<&mut Option<UnrarError>>,
    ) -> UnrarResult<OpenArchive<M, CursorBeforeHeader>> {
        let mut recovered = None;
        self.open(Some(&mut recovered))
            .or_else(|x| match recovered {
                Some(archive) => {
                    error.map(|error| *error = Some(x));
                    Ok(archive)
                }
                None => Err(x),
            })
    }
}

fn get_rar_extension<T: AsRef<Path>>(path: T) -> Option<String> {
    path.as_ref().extension().and_then(|ext| {
        let pre_ext = path
            .as_ref()
            .file_stem()
            .and_then(|x| Path::new(x).extension());
        Some(match pre_ext {
            Some(pre_ext) => format!(".{}.{}", pre_ext.to_str()?, ext.to_str()?),
            None => format!(".{}", ext.to_str()?),
        })
    })
}

pub fn is_archive(s: &Path) -> bool {
    get_rar_extension(s).is_some_and(|e| extension().is_match(&e))
}

pub fn is_multipart(s: &Path) -> bool {
    get_rar_extension(s).is_some_and(|e| multipart_extension().is_match(&e))
}

#[cfg(test)]
mod tests {
    use super::Archive;
    use std::path::PathBuf;

    #[test]
    fn glob() {
        assert_eq!(
            Archive::new("arc.part0010.rar").all_parts(),
            PathBuf::from("arc.part????.rar")
        );
        assert_eq!(
            Archive::new("archive.r100").all_parts(),
            PathBuf::from("archive.r???")
        );
        assert_eq!(
            Archive::new("archive.r9").all_parts(),
            PathBuf::from("archive.r?")
        );
        assert_eq!(
            Archive::new("archive.999").all_parts(),
            PathBuf::from("archive.???")
        );
        assert_eq!(
            Archive::new("archive.rar").all_parts(),
            PathBuf::from("archive.rar")
        );
        assert_eq!(
            Archive::new("random_string").all_parts(),
            PathBuf::from("random_string")
        );
        assert_eq!(
            Archive::new("v8/v8.rar").all_parts(),
            PathBuf::from("v8/v8.rar")
        );
        assert_eq!(Archive::new("v8/v8").all_parts(), PathBuf::from("v8/v8"));
    }

    #[test]
    fn first_part() {
        assert_eq!(
            Archive::new("arc.part0010.rar").first_part(),
            PathBuf::from("arc.part0001.rar")
        );
        assert_eq!(
            Archive::new("archive.r100").first_part(),
            PathBuf::from("archive.r001")
        );
        assert_eq!(
            Archive::new("archive.r9").first_part(),
            PathBuf::from("archive.r1")
        );
        assert_eq!(
            Archive::new("archive.999").first_part(),
            PathBuf::from("archive.001")
        );
        assert_eq!(
            Archive::new("archive.rar").first_part(),
            PathBuf::from("archive.rar")
        );
        assert_eq!(
            Archive::new("random_string").first_part(),
            PathBuf::from("random_string")
        );
        assert_eq!(
            Archive::new("v8/v8.rar").first_part(),
            PathBuf::from("v8/v8.rar")
        );
        assert_eq!(Archive::new("v8/v8").first_part(), PathBuf::from("v8/v8"));
    }

    #[test]
    fn is_archive() {
        assert_eq!(super::is_archive(&PathBuf::from("archive.rar")), true);
        assert_eq!(super::is_archive(&PathBuf::from("archive.part1.rar")), true);
        assert_eq!(
            super::is_archive(&PathBuf::from("archive.part100.rar")),
            true
        );
        assert_eq!(super::is_archive(&PathBuf::from("archive.r10")), true);
        assert_eq!(super::is_archive(&PathBuf::from("archive.part1rar")), false);
        assert_eq!(super::is_archive(&PathBuf::from("archive.rar\n")), false);
        assert_eq!(super::is_archive(&PathBuf::from("archive.zip")), false);
    }

    #[test]
    fn nul_in_input() {
        assert!(Archive::new("\0archive.rar").is_archive());
        assert!(Archive::with_password("archive.rar", "un\0rar").is_archive());
    }
}