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
use crate::file_utils::file_write_all_bytes;
use std::fs::File;
use std::io;
use std::io::{Error, ErrorKind, Read};
use std::path::PathBuf;
use zip::read::ZipFile;
use zip::result::{ZipError, ZipResult};
use zip::ZipArchive;
pub fn zip_extract(archive_file: &PathBuf, target_dir: &PathBuf) -> ZipResult<()> {
let file = File::open(archive_file)?;
let mut archive = zip::ZipArchive::new(file)?;
archive.extract(target_dir)
}
pub fn zip_extract_file(
archive_file: &PathBuf,
entry_path: &PathBuf,
target_dir: &PathBuf,
overwrite: bool,
) -> ZipResult<()> {
let file = File::open(archive_file)?;
let mut archive = zip::ZipArchive::new(file)?;
let file_number: usize = match archive.file_number(entry_path) {
Some(index) => index,
None => return Err(ZipError::FileNotFound),
};
let destination_file_path = target_dir.join(entry_path);
archive.extract_file(file_number, &destination_file_path, overwrite)
}
pub fn zip_extract_file_to_memory(
archive_file: &PathBuf,
entry_path: &PathBuf,
buffer: &mut Vec<u8>,
) -> ZipResult<()> {
let file = File::open(archive_file)?;
let mut archive = zip::ZipArchive::new(file)?;
let file_number: usize = match archive.file_number(entry_path) {
Some(index) => index,
None => return Err(ZipError::FileNotFound),
};
archive.extract_file_to_memory(file_number, buffer)
}
pub fn try_is_zip(file: &PathBuf) -> ZipResult<bool> {
const ZIP_SIGNATURE: [u8; 2] = [0x50, 0x4b];
const ZIP_ARCHIVE_FORMAT: [u8; 6] = [0x03, 0x04, 0x05, 0x06, 0x07, 0x08];
let mut file = File::open(file)?;
let mut buffer: [u8; 4] = [0; 4];
let bytes_read = file.read(&mut buffer)?;
if bytes_read == buffer.len() {
for i in 0..ZIP_SIGNATURE.len() {
if buffer[i] != ZIP_SIGNATURE[i] {
return Ok(false);
}
}
for i in (0..ZIP_ARCHIVE_FORMAT.len()).step_by(2) {
if buffer[2] == ZIP_ARCHIVE_FORMAT[i] || buffer[3] == ZIP_ARCHIVE_FORMAT[i + 1] {
return Ok(true);
}
}
}
Ok(false)
}
pub fn is_zip(file: &PathBuf) -> bool {
try_is_zip(file).unwrap_or_default()
}
pub trait ZipArchiveExtensions {
fn extract(&mut self, path: &PathBuf) -> ZipResult<()>;
fn extract_file(
&mut self,
file_number: usize,
destination_file_path: &PathBuf,
overwrite: bool,
) -> ZipResult<()>;
fn extract_file_to_memory(&mut self, file_number: usize, buffer: &mut Vec<u8>)
-> ZipResult<()>;
fn entry_path(&mut self, file_number: usize) -> ZipResult<PathBuf>;
fn file_number(&mut self, entry_path: &PathBuf) -> Option<usize>;
}
impl<R: Read + io::Seek> ZipArchiveExtensions for ZipArchive<R> {
fn extract(&mut self, target_directory: &PathBuf) -> ZipResult<()> {
if !target_directory.is_dir() {
return Err(ZipError::Io(Error::new(
ErrorKind::InvalidInput,
"The specified path does not indicate a valid directory path.",
)));
}
for file_number in 0..self.len() {
let mut next: ZipFile = self.by_index(file_number)?;
let sanitized_name = next.sanitized_name();
if next.is_dir() {
let extracted_folder_path = target_directory.join(sanitized_name);
std::fs::create_dir_all(extracted_folder_path)?;
} else if next.is_file() {
let mut buffer: Vec<u8> = Vec::new();
let _bytes_read = next.read_to_end(&mut buffer)?;
let extracted_file_path = target_directory.join(sanitized_name);
file_write_all_bytes(extracted_file_path, buffer.as_ref(), true)?;
}
}
Ok(())
}
fn extract_file(
&mut self,
file_number: usize,
destination_file_path: &PathBuf,
overwrite: bool,
) -> ZipResult<()> {
let mut buffer: Vec<u8> = Vec::new();
self.extract_file_to_memory(file_number, &mut buffer)?;
file_write_all_bytes(
destination_file_path.to_path_buf(),
buffer.as_ref(),
overwrite,
)?;
Ok(())
}
fn extract_file_to_memory(
&mut self,
file_number: usize,
buffer: &mut Vec<u8>,
) -> ZipResult<()> {
let mut next: ZipFile = self.by_index(file_number)?;
if next.is_file() {
let _bytes_read = next.read_to_end(buffer)?;
return Ok(());
}
Err(ZipError::Io(Error::new(
ErrorKind::InvalidInput,
"The specified index does not indicate a file entry.",
)))
}
fn entry_path(&mut self, file_number: usize) -> ZipResult<PathBuf> {
let next: ZipFile = self.by_index(file_number)?;
Ok(next.sanitized_name())
}
fn file_number(&mut self, entry_path: &PathBuf) -> Option<usize> {
for file_number in 0..self.len() {
if let Ok(next) = self.by_index(file_number) {
let sanitized_name = next.sanitized_name();
if sanitized_name == *entry_path {
return Some(file_number);
}
}
}
None
}
}