Skip to content

Commit 0dab338

Browse files
committed
Addition of doc comments for seek & stream
1 parent ef0364f commit 0dab338

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

src/read/seek.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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 seekable source.
4+
//! A module for reading ZIP files from a seekable source.
55
//!
66
//! # Example
77
//! ```no_run
@@ -43,7 +43,7 @@ pub struct ZipFileReader<R: AsyncRead + AsyncSeek + Unpin> {
4343
}
4444

4545
impl<R: AsyncRead + AsyncSeek + Unpin> ZipFileReader<R> {
46-
/// Constructs a new ZIP file reader from a mutable reference to a reader.
46+
/// Constructs a new ZIP file reader from a reader which implements [`AsyncRead`] and [`AsyncSeek`].
4747
pub async fn new(mut reader: R) -> Result<ZipFileReader<R>> {
4848
let (entries, comment) = read_cd(&mut reader).await?;
4949
Ok(ZipFileReader { reader, entries, comment })
@@ -95,6 +95,10 @@ pub(crate) async fn read_cd<R: AsyncRead + AsyncSeek + Unpin>(
9595
let delimiter = crate::spec::signature::END_OF_CENTRAL_DIRECTORY.to_le_bytes();
9696
let mut reader = AsyncDelimiterReader::new(reader, &delimiter);
9797

98+
// We need to find the last EOCDH as there's a possibility that an inner ZIP file exists with the Stored
99+
// compression method, so matching that would be undesirable. For the moment, we match all EOCDHs
100+
// sequentially and store the latest's offest.
101+
// TODO: Seeking in reverse may be a better a approach for this - needs some testing.
98102
'outer: loop {
99103
'inner: loop {
100104
let mut buffer = [0; async_io_utilities::SUGGESTED_BUFFER_SIZE];

src/read/stream.rs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
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
1033
use crate::error::{Result, ZipError};
@@ -23,21 +46,27 @@ pub struct ZipFileReader<R: AsyncRead + Unpin> {
2346
}
2447

2548
impl<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

Comments
 (0)