1use crate::{password::Password, Error, *};
2use std::io::{Read, Seek};
3use std::path::{Path, PathBuf};
4#[inline]
13pub fn decompress_file(src_path: impl AsRef<Path>, dest: impl AsRef<Path>) -> Result<(), Error> {
14 let file = std::fs::File::open(src_path.as_ref())
15 .map_err(|e| Error::file_open(e, src_path.as_ref().to_string_lossy().to_string()))?;
16 decompress(file, dest)
17}
18
19#[inline]
20pub fn decompress_file_with_extract_fn(
21 src_path: impl AsRef<Path>,
22 dest: impl AsRef<Path>,
23 extract_fn: impl FnMut(&SevenZArchiveEntry, &mut dyn Read, &PathBuf) -> Result<bool, Error>,
24) -> Result<(), Error> {
25 let file = std::fs::File::open(src_path.as_ref())
26 .map_err(|e| Error::file_open(e, src_path.as_ref().to_string_lossy().to_string()))?;
27 decompress_with_extract_fn(file, dest, extract_fn)
28}
29
30#[inline]
32pub fn decompress<R: Read + Seek>(src_reader: R, dest: impl AsRef<Path>) -> Result<(), Error> {
33 decompress_with_extract_fn(src_reader, dest, default_entry_extract_fn)
34}
35
36#[cfg(not(target_arch = "wasm32"))]
37#[inline]
38pub fn decompress_with_extract_fn<R: Read + Seek>(
39 src_reader: R,
40 dest: impl AsRef<Path>,
41 extract_fn: impl FnMut(&SevenZArchiveEntry, &mut dyn Read, &PathBuf) -> Result<bool, Error>,
42) -> Result<(), Error> {
43 decompress_impl(src_reader, dest, Password::empty(), extract_fn)
44}
45
46#[cfg(all(feature = "aes256", not(target_arch = "wasm32")))]
47#[inline]
54pub fn decompress_file_with_password(
55 src_path: impl AsRef<Path>,
56 dest: impl AsRef<Path>,
57 password: Password,
58) -> Result<(), Error> {
59 let file = std::fs::File::open(src_path.as_ref())
60 .map_err(|e| Error::file_open(e, src_path.as_ref().to_string_lossy().to_string()))?;
61 decompress_with_password(file, dest, password)
62}
63
64#[cfg(all(feature = "aes256", not(target_arch = "wasm32")))]
65#[inline]
66pub fn decompress_with_password<R: Read + Seek>(
67 src_reader: R,
68 dest: impl AsRef<Path>,
69 password: Password,
70) -> Result<(), Error> {
71 decompress_impl(src_reader, dest, password, default_entry_extract_fn)
72}
73
74#[cfg(all(feature = "aes256", not(target_arch = "wasm32")))]
75pub fn decompress_with_extract_fn_and_password<R: Read + Seek>(
76 src_reader: R,
77 dest: impl AsRef<Path>,
78 password: Password,
79 extract_fn: impl FnMut(&SevenZArchiveEntry, &mut dyn Read, &PathBuf) -> Result<bool, Error>,
80) -> Result<(), Error> {
81 decompress_impl(src_reader, dest, password, extract_fn)
82}
83
84#[cfg(not(target_arch = "wasm32"))]
85fn decompress_impl<R: Read + Seek>(
86 mut src_reader: R,
87 dest: impl AsRef<Path>,
88 password: Password,
89 mut extract_fn: impl FnMut(&SevenZArchiveEntry, &mut dyn Read, &PathBuf) -> Result<bool, Error>,
90) -> Result<(), Error> {
91 use std::io::SeekFrom;
92
93 let pos = src_reader.stream_position().map_err(Error::io)?;
94 let len = src_reader.seek(SeekFrom::End(0)).map_err(Error::io)?;
95 src_reader.seek(SeekFrom::Start(pos)).map_err(Error::io)?;
96 let mut seven = SevenZReader::new(src_reader, len, password)?;
97 let dest = PathBuf::from(dest.as_ref());
98 if !dest.exists() {
99 std::fs::create_dir_all(&dest).map_err(Error::io)?;
100 }
101 seven.for_each_entries(|entry, reader| {
102 let dest_path = dest.join(entry.name());
103 extract_fn(entry, reader, &dest_path)
104 })?;
105
106 Ok(())
107}
108
109#[cfg(not(target_arch = "wasm32"))]
110pub fn default_entry_extract_fn(
111 entry: &SevenZArchiveEntry,
112 reader: &mut dyn Read,
113 dest: &PathBuf,
114) -> Result<bool, Error> {
115 use std::{fs::File, io::BufWriter};
116
117 if entry.is_directory() {
118 let dir = dest;
119 if !dir.exists() {
120 std::fs::create_dir_all(dir).map_err(Error::io)?;
121 }
122 } else {
123 let path = dest;
124 path.parent().and_then(|p| {
125 if !p.exists() {
126 std::fs::create_dir_all(p).ok()
127 } else {
128 None
129 }
130 });
131 let file = File::create(path)
132 .map_err(|e| Error::file_open(e, path.to_string_lossy().to_string()))?;
133 if entry.size() > 0 {
134 let mut writer = BufWriter::new(file);
135 std::io::copy(reader, &mut writer).map_err(Error::io)?;
136 ft::set_file_handle_times(
137 writer.get_ref(),
138 Some(ft::FileTime::from_system_time(entry.access_date().into())),
139 Some(ft::FileTime::from_system_time(
140 entry.last_modified_date().into(),
141 )),
142 Some(ft::FileTime::from_system_time(entry.creation_date().into())),
143 )
144 .unwrap_or_default();
145 }
146 }
147 Ok(true)
148}