stream_unzip/
iterator.rs

1use crate::{ZipEntry, ZipReader};
2
3/// Reads the given file in chunks of N bytes and returns one `ZipEntry` at a time
4pub struct ZipIterator<F, const N: usize> {
5    file: F,
6    zip_reader: ZipReader,
7}
8
9impl<F, const N: usize> ZipIterator<F, N> {
10    pub fn new(file: F) -> Self {
11        Self {
12            file,
13            zip_reader: ZipReader::default(),
14        }
15    }
16}
17
18impl<F, const N: usize> From<F> for ZipIterator<F, N>
19where
20    F: std::io::Read,
21{
22    fn from(value: F) -> Self {
23        ZipIterator::new(value)
24    }
25}
26
27impl<F, const N: usize> Iterator for ZipIterator<F, N>
28where
29    F: std::io::Read,
30{
31    type Item = ZipEntry;
32
33    fn next(&mut self) -> Option<Self::Item> {
34        loop {
35            match self.zip_reader.take_entry() {
36                None => {
37                    let mut buf = [0u8; N];
38                    let num = self.file.read(&mut buf).unwrap();
39
40                    if num == 0 {
41                        return None;
42                    }
43
44                    self.zip_reader.update(buf[..num].to_vec().into());
45                }
46                entry => return entry,
47            }
48        }
49    }
50}