11// Copyright (c) 2021 Harry [Majored] [hello@majored.pw]
22// MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE)
33
4- //! A module for reading ZIP file from a non-seekable source.
4+ //! A module for reading ZIP files from a non-seekable source.
5+ //!
6+ //! ## Note
7+ //! This method fully relies on the information provided in each individual local file header. As a result, this method
8+ //! doesn't support entries which desynchronise the information provided in the local file header VS the central
9+ //! directory file header. This is a practice that the
10+ //! [specification suggests](https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#719) when you wish to
11+ //! conceal that information.
12+ //!
13+ //! ## Example
14+ //! ```no_run
15+ //! # use async_zip::read::stream::ZipFileReader;
16+ //! # use tokio::fs::File;
17+ //! # use async_zip::error::ZipError;
18+ //! #
19+ //! # async fn run() -> Result<(), ZipError> {
20+ //! let mut file = File::open("./Archive.zip").await?;
21+ //! let mut zip = ZipFileReader::new(&mut file);
522//!
6- //! # Example
7- //! ```
23+ //! // Consume all entries in order.
24+ //! while !zip.finished() {
25+ //! if let Some(reader) = zip.entry_reader().await? {
26+ //! reader.read_to_string_crc().await?;
27+ //! }
28+ //! }
29+ //! # Ok(())
30+ //! # }
831//! ```
932
1033use crate :: error:: { Result , ZipError } ;
@@ -23,21 +46,27 @@ pub struct ZipFileReader<R: AsyncRead + Unpin> {
2346}
2447
2548impl < R : AsyncRead + Unpin > ZipFileReader < R > {
26- /// Constructs a new ZIP file reader from a mutable reference to a reader .
49+ /// Constructs a new ZIP file reader from a reader which implements [`AsyncRead`] .
2750 pub fn new ( reader : R ) -> Self {
2851 let reader = AsyncPrependReader :: new ( reader) ;
2952 ZipFileReader { reader, entry : None , finished : false }
3053 }
3154
32- /// Returns whether or not `entry_reader()` will yield more entries.
55+ /// Returns whether or not it's possible for this reader to yeild more entries.
56+ ///
57+ /// # Note
58+ /// It's still possible for this function to return false whilst a call to [`ZipFileReader::entry_reader()`]
59+ /// returns no entry. This happens in the case where the last entry has been read but the central directory has not
60+ /// been reached (and would be reached in a succeeding call to [`ZipFileReader::entry_reader()`]).
3361 pub fn finished ( & self ) -> bool {
3462 self . finished
3563 }
3664
37- /// Opens the next entry for reading if the central directory hasn't already been reached.
65+ /// Opens the next entry for reading if the central directory hasn't yet been reached.
66+ ///
67+ /// # Note
68+ /// It's essential that each entry reader returned by this function is fully consumed before a new one is opened.
3869 pub async fn entry_reader ( & mut self ) -> Result < Option < ZipEntryReader < ' _ , R > > > {
39- // TODO: Ensure the previous entry has been fully read.
40-
4170 if self . finished {
4271 return Ok ( None ) ;
4372 } else if let Some ( inner) = read_lfh ( & mut self . reader ) . await ? {
0 commit comments