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;

/// Making archives with stored compression is not supported yet and only used on directory
/// entries.
#[repr(u16)]
#[derive(Debug, Clone, Copy)]
pub enum CompressionType {
    Stored = 0,
    Deflate = 8
}

/// Initialize using Default trait.
#[derive(Debug, Default)]
pub struct ZipArchive {
    jobs: Mutex<Vec<ZipJob>>,
    data: Mutex<ZipData>,
    compressed: Mutex<bool>
}

impl ZipArchive {
    /// Add file from silesystem. Will read on compression.
    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);
        }
    }

    /// Add file from slice. Stores the data in archive struct for later compression.
    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);
        }
    }

    /// Add a directory entry
    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);
        }
    }

    /// Call to execute compression. Will be done automatically on write if files were added before
    /// write.
    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)
                    }
                });
            }
        })
    }

    /// Write compressed data to a writer. Automatically calls [compress](ZipArchive::compress) if files were added
    /// before write.
    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 // Possible improvement: read
                                                                 // permissions/attributes from fs
                    }
                },
                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();
        // Zip file records
        for file in &self.files {
            offsets.push(buf.len() as u32);
            file.to_bytes_filerecord(buf);
        }
        let central_dir_offset = buf.len() as u32;
        // Zip directory entries
        for (file, offset) in self.files.iter().zip(offsets.iter()) {
            file.to_bytes_direntry(buf, *offset);
        }

        // End of central dir record
        let central_dir_start = buf.len() as u32;

        // Signature
        buf.extend(END_OF_CENTRAL_DIR_SIGNATURE.to_le_bytes());
        // number of this disk
        buf.extend(0_u16.to_le_bytes());
        // number of the disk with start
        buf.extend(0_u16.to_le_bytes());
        // Number of entries on this disk
        buf.extend((self.files.len() as u16).to_le_bytes());
        // Number of entries
        buf.extend((self.files.len() as u16).to_le_bytes());
        // Central dir size
        buf.extend((central_dir_start - central_dir_offset).to_le_bytes());
        // Central dir offset
        buf.extend(central_dir_offset.to_le_bytes());
        // Comment length
        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>) {
        // signature
        buf.extend(FILE_RECORD_SIGNATURE.to_le_bytes());
        // version needed to extract
        buf.extend(VERSION_NEEDED_TO_EXTRACT.to_le_bytes()); 
        // flags
        buf.extend(0_u16.to_le_bytes());
        // compression type
        buf.extend((self.compression_type as u16).to_le_bytes());
        // Time // TODO
        buf.extend(0_u16.to_le_bytes());
        // Date // TODO
        buf.extend(0_u16.to_le_bytes());
        // crc 
        buf.extend(self.crc.to_le_bytes());
        // Compressed size
        buf.extend((self.data.len() as u32).to_le_bytes());
        // Uncompressed size
        buf.extend(self.uncompressed_size.to_le_bytes());
        // Filename size
        buf.extend((self.filename.len() as u16).to_le_bytes());
        // extra field size
        buf.extend(0_u16.to_le_bytes());
        // Filename
        buf.extend(self.filename.as_bytes());
        // Data
        buf.extend(&self.data);
    }

    fn to_bytes_direntry(&self, buf: &mut Vec<u8>, local_header_offset: u32) {
        // signature
        buf.extend(DIRECTORY_ENTRY_SIGNATURE.to_le_bytes());
        // version made by
        buf.extend(VERSION_MADE_BY.to_le_bytes());
        // version needed to extract
        buf.extend(VERSION_NEEDED_TO_EXTRACT.to_le_bytes());
        // flags
        buf.extend(0_u16.to_le_bytes());
        // compression type
        buf.extend((self.compression_type as u16).to_le_bytes());
        // Time // TODO
        buf.extend(0_u16.to_le_bytes());
        // Date // TODO
        buf.extend(0_u16.to_le_bytes());
        // crc
        buf.extend(self.crc.to_le_bytes());
        // Compressed size
        buf.extend((self.data.len() as u32).to_le_bytes());
        // Uncompressed size
        buf.extend(self.uncompressed_size.to_le_bytes());
        // Filename size
        buf.extend((self.filename.len() as u16).to_le_bytes());
        // extra field size
        buf.extend(0_u16.to_le_bytes());
        // comment size
        buf.extend(0_u16.to_le_bytes());
        // disk number start
        buf.extend(0_u16.to_le_bytes());
        // internal file attributes
        buf.extend(0_u16.to_le_bytes());
        // external file attributes
        buf.extend(self.external_file_attributes.to_le_bytes());
        // relative offset of local header
        buf.extend(local_header_offset.to_le_bytes());
        // Filename
        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
    }
}