1use crate::{Error, password::Password, *};
2use std::io::{Read, Seek};
3use std::path::{Path, PathBuf};
4
5#[cfg_attr(docsrs, doc(cfg(feature = "util")))]
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#[cfg_attr(docsrs, doc(cfg(feature = "util")))]
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#[cfg_attr(docsrs, doc(cfg(feature = "util")))]
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#[cfg_attr(docsrs, doc(cfg(feature = "util")))]
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#[cfg_attr(docsrs, doc(cfg(all(feature = "aes256", feature = "util"))))]
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#[cfg_attr(docsrs, doc(cfg(all(feature = "aes256", feature = "util"))))]
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")))]
75#[cfg_attr(docsrs, doc(cfg(all(feature = "aes256", feature = "util"))))]
76pub fn decompress_with_extract_fn_and_password<R: Read + Seek>(
77 src_reader: R,
78 dest: impl AsRef<Path>,
79 password: Password,
80 extract_fn: impl FnMut(&SevenZArchiveEntry, &mut dyn Read, &PathBuf) -> Result<bool, Error>,
81) -> Result<(), Error> {
82 decompress_impl(src_reader, dest, password, extract_fn)
83}
84
85#[cfg(not(target_arch = "wasm32"))]
86fn decompress_impl<R: Read + Seek>(
87 mut src_reader: R,
88 dest: impl AsRef<Path>,
89 password: Password,
90 mut extract_fn: impl FnMut(&SevenZArchiveEntry, &mut dyn Read, &PathBuf) -> Result<bool, Error>,
91) -> Result<(), Error> {
92 use std::io::SeekFrom;
93
94 let pos = src_reader.stream_position().map_err(Error::io)?;
95 src_reader.seek(SeekFrom::Start(pos)).map_err(Error::io)?;
96 let mut seven = SevenZReader::new(src_reader, 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"))]
110#[cfg_attr(docsrs, doc(cfg(feature = "util")))]
111pub fn default_entry_extract_fn(
112 entry: &SevenZArchiveEntry,
113 reader: &mut dyn Read,
114 dest: &PathBuf,
115) -> Result<bool, Error> {
116 use std::{fs::File, io::BufWriter};
117
118 if entry.is_directory() {
119 let dir = dest;
120 if !dir.exists() {
121 std::fs::create_dir_all(dir).map_err(Error::io)?;
122 }
123 } else {
124 let path = dest;
125 path.parent().and_then(|p| {
126 if !p.exists() {
127 std::fs::create_dir_all(p).ok()
128 } else {
129 None
130 }
131 });
132 let file = File::create(path)
133 .map_err(|e| Error::file_open(e, path.to_string_lossy().to_string()))?;
134 if entry.size() > 0 {
135 let mut writer = BufWriter::new(file);
136 std::io::copy(reader, &mut writer).map_err(Error::io)?;
137 filetime_creation::set_file_handle_times(
138 writer.get_ref(),
139 Some(filetime_creation::FileTime::from_system_time(
140 entry.access_date().into(),
141 )),
142 Some(filetime_creation::FileTime::from_system_time(
143 entry.last_modified_date().into(),
144 )),
145 Some(filetime_creation::FileTime::from_system_time(
146 entry.creation_date().into(),
147 )),
148 )
149 .unwrap_or_default();
150 }
151 }
152 Ok(true)
153}