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
use crate::{ZipEntry, ZipReader};

/// Reads the given file in chunks of N bytes and returns one `ZipEntry` at a time
pub struct ZipIterator<F, const N: usize> {
    file: F,
    zip_reader: ZipReader,
}

impl<F, const N: usize> ZipIterator<F, N> {
    pub fn new(file: F) -> Self {
        Self {
            file,
            zip_reader: ZipReader::default(),
        }
    }
}

impl<F, const N: usize> From<F> for ZipIterator<F, N>
where
    F: std::io::Read,
{
    fn from(value: F) -> Self {
        ZipIterator::new(value)
    }
}

impl<F, const N: usize> Iterator for ZipIterator<F, N>
where
    F: std::io::Read,
{
    type Item = ZipEntry;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match self.zip_reader.take_entry() {
                None => {
                    let mut buf = [0u8; N];
                    let num = self.file.read(&mut buf).unwrap();

                    if num == 0 {
                        return None;
                    }

                    self.zip_reader.update(buf[..num].to_vec().into());
                }
                entry => return entry,
            }
        }
    }
}