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
use std::io::{self, Read, Seek, BufReader, Write, BufWriter};
use crossbeam_channel::{Sender, Receiver};
use flate2::bufread::DeflateEncoder;
use zip::{ZipArchive, ZipWriter, write::FileOptions, CompressionMethod};

use crate::{entry::{EntryReader, EntrySaver}, optimizer::{EntryType, ProgressState, StrError, ERR_SIGNFILE}, fop::{FileOp, check_file_by_name}, errors::ErrorCollector, blacklist};

/// An entry reader implementation for ZIP archive. It reads its contents from a provided reader (with seeking).
pub struct ZipEntryReader<R: Read + Seek> {
    r: R
}
impl <R: Read + Seek> ZipEntryReader<R> {
    /// Creates an entry reader with a specified reader.
    pub fn new(r: R) -> Self {
        Self { r }
    }
}
impl <R: Read + Seek> EntryReader for ZipEntryReader<R> {
    fn read_entries(
        self,
        tx: Sender<EntryType>,
        use_blacklist: bool
    ) -> io::Result<()> {
        let mut za = ZipArchive::new(BufReader::new(self.r))?;
        let jfc = za.len() as u64;
        tx.send(EntryType::Count(jfc)).unwrap();
        for i in 0..jfc {
            let mut jf = za.by_index(i as usize)?;
            let fname = jf.name().to_string();
            tx.send(if fname.ends_with('/') {
                EntryType::Directory(fname)
            } else {
                let fop = check_file_by_name(&fname, use_blacklist);
                let mut obuf = Vec::new();
                match fop {
                    FileOp::Ignore => {}
                    _ => {
                        obuf.reserve_exact(jf.size() as usize);
                        jf.read_to_end(&mut obuf)?;
                    }
                }
                EntryType::File(fname, obuf, fop)
            }).unwrap();
        }
        Ok(())
    }
}

/// An entry saver implementation for ZIP archive. It writes entries to it using a provided writer.
pub struct ZipEntrySaver<W: Write + Seek> {
    w: ZipWriter<BufWriter<W>>,
    file_opts: FileOptions
}
impl <W: Write + Seek> ZipEntrySaver<W> {
    /// Creates an entry saver with a seekable writer.
    pub fn new(w: W) -> Self {
        Self {
            w: ZipWriter::new(BufWriter::new(w)),
            file_opts: FileOptions::default().compression_level(Some(9))
        }
    }
    /// Creates an entry saver with custom file options for ZIP archive and seekable writer.
    pub fn custom(w: W, file_opts: FileOptions) -> Self {
        Self {
            w: ZipWriter::new(BufWriter::new(w)), file_opts
        }
    }

    fn pack_file(&mut self, name: &str, data: &[u8], compress_min: usize) -> io::Result<()> {
        let z = &mut self.w;
        z.start_file(name, self.file_opts.clone()
            .compression_method(compress_check(data, compress_min))
        )?;
        z.write_all(data)
    }
}
impl <W: Write + Seek> EntrySaver for ZipEntrySaver<W> {
    fn save_entries(
        mut self,
        rx: Receiver<EntryType>,
        ev: &mut dyn ErrorCollector,
        ps: &Sender<ProgressState>
    ) -> io::Result<i64> {
        let mut dsum = 0;
        let mut cv = Vec::new();
        for et in rx {
            match et {
                EntryType::Count(u) => {
                    ps.send(ProgressState::Start(u)).unwrap();
                }
                EntryType::Directory(d) => {
                    if d != ".cache/" {
                        self.w.add_directory(d, self.file_opts.clone())?;
                    }
                }
                EntryType::File(fname, buf, fop) => {
                    use FileOp::*;
                    ps.send(ProgressState::Push(fname.clone())).unwrap();
                    match fop {
                        Recompress(cmin) => {
                            self.pack_file(
                                &fname,
                                &buf,
                                cmin
                            )?;
                        }
                        Minify(m) => {
                            let fsz = buf.len() as i64;
                            let buf = match m.minify(&buf, &mut cv) {
                                Ok(()) => &cv,
                                Err(e) => {
                                    ev.collect(&fname, e);
                                    &buf
                                }
                            };
                            dsum -= (buf.len() as i64) - fsz;
                            self.pack_file(&fname, &buf, m.compress_min())?;
                            cv.clear();
                        }
                        Ignore => {
                            ev.collect(&fname, Box::new(blacklist::BlacklistedFile));
                        }
                        Warn(x) => {
                            ev.collect(&fname, Box::new(StrError(x)));
                            self.pack_file(&fname, &buf, 0)?;
                        }
                        Signfile => {
                            ev.collect(&fname, Box::new(StrError(ERR_SIGNFILE.to_string())));
                        }
                    }
                }
            }
        }
        self.w.finish()?;
        ps.send(ProgressState::Finish).unwrap();
        Ok(dsum)
    }
}

fn compress_check(b: &[u8], compress_min: usize) -> CompressionMethod {
    let lb = b.len();
    let nc = if lb > compress_min {
        let de = DeflateEncoder::new(b, flate2::Compression::best());
        let sum = de.bytes().count();
        sum < lb
    } else { false };
    if nc { CompressionMethod::DEFLATE } else { CompressionMethod::STORE }
}