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
use flate2::{read::DeflateEncoder, Compression, CrcReader};
use std::{sync::Mutex, path::Path, io::{Read, Write}, fs::File};
const VERSION_NEEDED_TO_EXTRACT: u16 = 20;
const VERSION_MADE_BY: u16 = 0x033F;
const FILE_RECORD_SIGNATURE: u32 = 0x04034B50;
const DIRECTORY_ENTRY_SIGNATURE: u32 = 0x02014B50;
const END_OF_CENTRAL_DIR_SIGNATURE: u32 = 0x06054B50;
#[repr(u16)]
#[derive(Debug, Clone, Copy)]
pub enum CompressionType {
Stored = 0,
Deflate = 8
}
#[derive(Debug, Default)]
pub struct ZipArchive {
jobs: Mutex<Vec<ZipJob>>,
data: Mutex<ZipData>,
compressed: Mutex<bool>
}
impl ZipArchive {
pub fn add_file(&self, fs_path: impl AsRef<Path>, archive_name: &str) {
{
let mut compressed = self.compressed.lock().unwrap();
*compressed = false
}
let path: Box<Path> = fs_path.as_ref().into();
let name = archive_name.to_string();
let job = ZipJob{
data_origin: ZipJobOrigin::Filesystem(path),
archive_path: name
};
{
let mut jobs = self.jobs.lock().unwrap();
jobs.push(job);
}
}
pub fn add_file_from_slice(&self, data: &[u8], archive_name: &str) {
{
let mut compressed = self.compressed.lock().unwrap();
*compressed = false
}
let data = Vec::from(data);
let name = archive_name.to_string();
let job = ZipJob {
data_origin: ZipJobOrigin::RawData(data),
archive_path: name
};
{
let mut jobs = self.jobs.lock().unwrap();
jobs.push(job);
}
}
pub fn add_directory(&self, archive_name: &str) {
{
let mut compressed = self.compressed.lock().unwrap();
*compressed = false
}
let name = archive_name.to_string();
let job = ZipJob {
data_origin: ZipJobOrigin::Directory,
archive_path: name
};
{
let mut jobs = self.jobs.lock().unwrap();
jobs.push(job);
}
}
pub fn compress(&self, threads: usize) {
{
let mut compressed = self.compressed.lock().unwrap();
*compressed = true
}
std::thread::scope(|s| {
for _ in 0..threads {
s.spawn(|| {
loop {
let job = {
let mut job_lock = self.jobs.lock().unwrap();
if job_lock.is_empty() {
break;
} else {
job_lock.pop().unwrap()
}
};
job.to_data(&self.data)
}
});
}
})
}
pub fn write(&self, writer: &mut impl Write, threads: Option<usize>) {
if !*self.compressed.lock().unwrap() {
self.compress(threads.unwrap_or(1))
}
let data_lock = self.data.lock().unwrap();
let mut data = Vec::with_capacity(data_lock.len());
data_lock.to_bytes(&mut data);
writer.write_all(&data).unwrap();
}
}
#[derive(Debug)]
struct ZipJob {
data_origin: ZipJobOrigin,
archive_path: String
}
impl ZipJob {
fn to_data(self, archive: &Mutex<ZipData>) {
let data = {
match self.data_origin {
ZipJobOrigin::Directory => ZipFile::directory(self.archive_path),
ZipJobOrigin::Filesystem(fs_path) => {
let file = File::open(fs_path).unwrap();
let uncompressed_size = file.metadata().unwrap().len() as u32;
let crc_reader = CrcReader::new(file);
let mut encoder = DeflateEncoder::new(crc_reader, Compression::new(9));
let mut data = Vec::new();
encoder.read_to_end(&mut data).unwrap();
let crc_reader = encoder.into_inner();
let crc = crc_reader.crc().sum();
ZipFile {
compression_type: CompressionType::Deflate,
crc,
uncompressed_size,
filename: self.archive_path,
data,
external_file_attributes: 0o100644 << 16 }
},
ZipJobOrigin::RawData(in_data) => {
let uncompressed_size = in_data.len() as u32;
let crc_reader = CrcReader::new(in_data.as_slice());
let mut encoder = DeflateEncoder::new(crc_reader, Compression::new(9));
let mut data = Vec::new();
encoder.read_to_end(&mut data).unwrap();
let crc_reader = encoder.into_inner();
let crc = crc_reader.crc().sum();
ZipFile {
compression_type: CompressionType::Deflate,
crc,
uncompressed_size,
filename: self.archive_path,
data,
external_file_attributes: 0o100644 << 16
}
}
}
};
{
let mut data_lock = archive.lock().unwrap();
data_lock.files.push(data);
}
}
}
#[derive(Debug)]
enum ZipJobOrigin {
Filesystem(Box<Path>),
RawData(Vec<u8>),
Directory
}
#[derive(Debug, Default)]
struct ZipData {
files: Vec<ZipFile>
}
impl ZipData {
fn to_bytes(&self, buf: &mut Vec<u8>) {
let mut offsets: Vec<u32> = Vec::new();
for file in &self.files {
offsets.push(buf.len() as u32);
file.to_bytes_filerecord(buf);
}
let central_dir_offset = buf.len() as u32;
for (file, offset) in self.files.iter().zip(offsets.iter()) {
file.to_bytes_direntry(buf, *offset);
}
let central_dir_start = buf.len() as u32;
buf.extend(END_OF_CENTRAL_DIR_SIGNATURE.to_le_bytes());
buf.extend(0_u16.to_le_bytes());
buf.extend(0_u16.to_le_bytes());
buf.extend((self.files.len() as u16).to_le_bytes());
buf.extend((self.files.len() as u16).to_le_bytes());
buf.extend((central_dir_start - central_dir_offset).to_le_bytes());
buf.extend(central_dir_offset.to_le_bytes());
buf.extend(0_u16.to_le_bytes());
}
fn len(&self) -> usize {
self.files.iter().fold(0, |total, file| total + file.len()) + 22
}
}
#[derive(Debug)]
struct ZipFile {
compression_type: CompressionType,
crc: u32,
uncompressed_size: u32,
filename: String,
data: Vec<u8>,
external_file_attributes: u32
}
impl ZipFile {
fn to_bytes_filerecord(&self, buf: &mut Vec<u8>) {
buf.extend(FILE_RECORD_SIGNATURE.to_le_bytes());
buf.extend(VERSION_NEEDED_TO_EXTRACT.to_le_bytes());
buf.extend(0_u16.to_le_bytes());
buf.extend((self.compression_type as u16).to_le_bytes());
buf.extend(0_u16.to_le_bytes());
buf.extend(0_u16.to_le_bytes());
buf.extend(self.crc.to_le_bytes());
buf.extend((self.data.len() as u32).to_le_bytes());
buf.extend(self.uncompressed_size.to_le_bytes());
buf.extend((self.filename.len() as u16).to_le_bytes());
buf.extend(0_u16.to_le_bytes());
buf.extend(self.filename.as_bytes());
buf.extend(&self.data);
}
fn to_bytes_direntry(&self, buf: &mut Vec<u8>, local_header_offset: u32) {
buf.extend(DIRECTORY_ENTRY_SIGNATURE.to_le_bytes());
buf.extend(VERSION_MADE_BY.to_le_bytes());
buf.extend(VERSION_NEEDED_TO_EXTRACT.to_le_bytes());
buf.extend(0_u16.to_le_bytes());
buf.extend((self.compression_type as u16).to_le_bytes());
buf.extend(0_u16.to_le_bytes());
buf.extend(0_u16.to_le_bytes());
buf.extend(self.crc.to_le_bytes());
buf.extend((self.data.len() as u32).to_le_bytes());
buf.extend(self.uncompressed_size.to_le_bytes());
buf.extend((self.filename.len() as u16).to_le_bytes());
buf.extend(0_u16.to_le_bytes());
buf.extend(0_u16.to_le_bytes());
buf.extend(0_u16.to_le_bytes());
buf.extend(0_u16.to_le_bytes());
buf.extend(self.external_file_attributes.to_le_bytes());
buf.extend(local_header_offset.to_le_bytes());
buf.extend(self.filename.as_bytes());
}
fn directory(mut name: String) -> Self {
if !(name.ends_with("/") || name.ends_with("\\")) {
name += "/"
};
Self {
compression_type: CompressionType::Stored,
crc: 0,
uncompressed_size: 0,
filename: name,
data: vec![],
external_file_attributes: 0o40755 << 16
}
}
fn len(&self) -> usize {
self.data.len() + self.filename.len() + self.filename.len() + 32 + 46
}
}