| 1 | // Copyright (c) 2022 Harry [Majored] [[email protected]] |
| 2 | // MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE) |
| 3 | |
| 4 | //! A ZIP reader which acts over a seekable source. |
| 5 | //! |
| 6 | //! ### Example |
| 7 | //! ```no_run |
| 8 | //! # use async_zip::base::read::seek::ZipFileReader; |
| 9 | //! # use async_zip::error::Result; |
| 10 | //! # use futures_lite::io::AsyncReadExt; |
| 11 | //! # use tokio::fs::File; |
| 12 | //! # use tokio_util::compat::TokioAsyncReadCompatExt; |
| 13 | //! # use tokio::io::BufReader; |
| 14 | //! # |
| 15 | //! async fn run() -> Result<()> { |
| 16 | //! let mut data = BufReader::new(File::open("./foo.zip").await?); |
| 17 | //! let mut reader = ZipFileReader::new(data.compat()).await?; |
| 18 | //! |
| 19 | //! let mut data = Vec::new(); |
| 20 | //! let mut entry = reader.reader_without_entry(0).await?; |
| 21 | //! entry.read_to_end(&mut data).await?; |
| 22 | //! |
| 23 | //! // Use data within current scope. |
| 24 | //! |
| 25 | //! Ok(()) |
| 26 | //! } |
| 27 | //! ``` |
| 28 | |
| 29 | use craterust-analyzer cargo async_zip 0.0.18 crate/::baserust-analyzer cargo async_zip 0.0.18 base/::readrust-analyzer cargo async_zip 0.0.18 base/read/::iorust-analyzer cargo async_zip 0.0.18 base/read/io/::entryrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/::ZipEntryReaderrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/ZipEntryReader#; |
| 30 | use craterust-analyzer cargo async_zip 0.0.18 crate/::errorrust-analyzer cargo async_zip 0.0.18 error/::{Resultrust-analyzer cargo async_zip 0.0.18 error/Result#, ZipErrorrust-analyzer cargo async_zip 0.0.18 error/ZipError#}; |
| 31 | use craterust-analyzer cargo async_zip 0.0.18 crate/::filerust-analyzer cargo async_zip 0.0.18 file/::ZipFilerust-analyzer cargo async_zip 0.0.18 file/ZipFile#; |
| 32 | |
| 33 | #[cfg(feature = "tokio")] |
| 34 | use craterust-analyzer cargo async_zip 0.0.18 crate/::tokiorust-analyzer cargo async_zip 0.0.18 tokio/::readrust-analyzer cargo async_zip 0.0.18 tokio/read/::seekrust-analyzer cargo async_zip 0.0.18 tokio/read/seek/::ZipFileReaderrust-analyzer cargo async_zip 0.0.18 tokio/read/seek/ZipFileReader# as TokioZipFileReaderrust-analyzer cargo async_zip 0.0.18 tokio/read/seek/ZipFileReader#; |
| 35 | |
| 36 | use futures_literust-analyzer cargo futures-lite 2.1.0 crate/::iorust-analyzer cargo futures-lite 2.1.0 io/::{AsyncBufReadrust-analyzer cargo futures-io 0.3.25 if_std/AsyncBufRead#, AsyncSeekrust-analyzer cargo futures-io 0.3.25 if_std/AsyncSeek#}; |
| 37 | |
| 38 | #[cfg(feature = "tokio")] |
| 39 | use tokio_utilrust-analyzer cargo tokio-util 0.7.4 crate/::compatrust-analyzer cargo tokio-util 0.7.4 compat/::{Compatrust-analyzer cargo tokio-util 0.7.4 compat/Compat#, TokioAsyncReadCompatExtrust-analyzer cargo tokio-util 0.7.4 compat/TokioAsyncReadCompatExt#}; |
| 40 | |
| 41 | use superrust-analyzer cargo async_zip 0.0.18 base/read/::iorust-analyzer cargo async_zip 0.0.18 base/read/io/::entryrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/::{WithEntryrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/WithEntry#, WithoutEntryrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/WithoutEntry#}; |
| 42 | |
| 43 | /// A ZIP reader which acts over a seekable source. |
| 44 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 45 | pub struct ZipFileReaderrust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#<R> { |
| 46 | readerrust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#reader.: R, |
| 47 | filerust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#file.: ZipFilerust-analyzer cargo async_zip 0.0.18 file/ZipFile#, |
| 48 | } |
| 49 | |
| 50 | impl<R> ZipFileReaderrust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#<R> |
| 51 | where |
| 52 | R: AsyncBufReadrust-analyzer cargo futures-io 0.3.25 if_std/AsyncBufRead# + AsyncSeekrust-analyzer cargo futures-io 0.3.25 if_std/AsyncSeek# + Unpinrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Unpin#, |
| 53 | { |
| 54 | /// Constructs a new ZIP reader from a seekable source. |
| 55 | pub async fn newrust-analyzer cargo async_zip 0.0.18 base/read/seek/impl#[`ZipFileReader<R>`]new().(mut reader: R) -> Resultrust-analyzer cargo async_zip 0.0.18 error/Result#<ZipFileReaderrust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#<R>> { |
| 56 | let file = craterust-analyzer cargo async_zip 0.0.18 crate/::baserust-analyzer cargo async_zip 0.0.18 base/::readrust-analyzer cargo async_zip 0.0.18 base/read/::filerust-analyzer cargo async_zip 0.0.18 base/read/file().(&mut reader).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 57 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ZipFileReaderrust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#::from_raw_partsrust-analyzer cargo async_zip 0.0.18 base/read/seek/impl#[`ZipFileReader<R>`]from_raw_parts().(reader, file)) |
| 58 | } |
| 59 | |
| 60 | /// Constructs a ZIP reader from a seekable source and ZIP file information derived from that source. |
| 61 | /// |
| 62 | /// Providing a [`ZipFile`] that wasn't derived from that source may lead to inaccurate parsing. |
| 63 | pub fn from_raw_partsrust-analyzer cargo async_zip 0.0.18 base/read/seek/impl#[`ZipFileReader<R>`]from_raw_parts().(reader: R, file: ZipFilerust-analyzer cargo async_zip 0.0.18 file/ZipFile#) -> ZipFileReaderrust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#<R> { |
| 64 | ZipFileReaderrust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader# { readerrust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#reader., filerust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#file. } |
| 65 | } |
| 66 | |
| 67 | /// Returns this ZIP file's information. |
| 68 | pub fn filerust-analyzer cargo async_zip 0.0.18 base/read/seek/impl#[`ZipFileReader<R>`]file().(&self) -> &ZipFilerust-analyzer cargo async_zip 0.0.18 file/ZipFile# { |
| 69 | &self.filerust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#file. |
| 70 | } |
| 71 | |
| 72 | /// Returns a mutable reference to the inner seekable source. |
| 73 | /// |
| 74 | /// Swapping the source (eg. via std::mem operations) may lead to inaccurate parsing. |
| 75 | pub fn inner_mutrust-analyzer cargo async_zip 0.0.18 base/read/seek/impl#[`ZipFileReader<R>`]inner_mut().(&mut self) -> &mut R { |
| 76 | &mut self.readerrust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#reader. |
| 77 | } |
| 78 | |
| 79 | /// Returns the inner seekable source by consuming self. |
| 80 | pub fn into_innerrust-analyzer cargo async_zip 0.0.18 base/read/seek/impl#[`ZipFileReader<R>`]into_inner().(self) -> R { |
| 81 | self.readerrust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#reader. |
| 82 | } |
| 83 | |
| 84 | /// Returns a new entry reader if the provided index is valid. |
| 85 | pub async fn reader_without_entryrust-analyzer cargo async_zip 0.0.18 base/read/seek/impl#[`ZipFileReader<R>`]reader_without_entry().(&mut self, index: usize) -> Resultrust-analyzer cargo async_zip 0.0.18 error/Result#<ZipEntryReaderrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/ZipEntryReader#<'_, R, WithoutEntryrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/WithoutEntry#>> { |
| 86 | let stored_entry = self.filerust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#file..entriesrust-analyzer cargo async_zip 0.0.18 file/ZipFile#entries..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(index).ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(ZipErrorrust-analyzer cargo async_zip 0.0.18 error/ZipError#::EntryIndexOutOfBoundsrust-analyzer cargo async_zip 0.0.18 error/ZipError#EntryIndexOutOfBounds#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 87 | stored_entry.seek_to_data_offsetrust-analyzer cargo async_zip 0.0.18 entry/impl#[StoredZipEntry]seek_to_data_offset().(&mut self.readerrust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#reader.).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 88 | |
| 89 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ZipEntryReaderrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/ZipEntryReader#::new_with_borrowrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/impl#[`ZipEntryReader<'a, R, WithoutEntry>`]new_with_borrow().( |
| 90 | &mut self.readerrust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#reader., |
| 91 | stored_entry.entryrust-analyzer cargo async_zip 0.0.18 entry/StoredZipEntry#entry..compressionrust-analyzer cargo async_zip 0.0.18 entry/impl#[ZipEntry]compression().(), |
| 92 | stored_entry.entryrust-analyzer cargo async_zip 0.0.18 entry/StoredZipEntry#entry..compressed_sizerust-analyzer cargo async_zip 0.0.18 entry/impl#[ZipEntry]compressed_size().(), |
| 93 | )) |
| 94 | } |
| 95 | |
| 96 | /// Returns a new entry reader if the provided index is valid. |
| 97 | pub async fn reader_with_entryrust-analyzer cargo async_zip 0.0.18 base/read/seek/impl#[`ZipFileReader<R>`]reader_with_entry().(&mut self, index: usize) -> Resultrust-analyzer cargo async_zip 0.0.18 error/Result#<ZipEntryReaderrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/ZipEntryReader#<'_, R, WithEntryrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/WithEntry#<'_>>> { |
| 98 | let stored_entry = self.filerust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#file..entriesrust-analyzer cargo async_zip 0.0.18 file/ZipFile#entries..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(index).ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(ZipErrorrust-analyzer cargo async_zip 0.0.18 error/ZipError#::EntryIndexOutOfBoundsrust-analyzer cargo async_zip 0.0.18 error/ZipError#EntryIndexOutOfBounds#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 99 | |
| 100 | stored_entry.seek_to_data_offsetrust-analyzer cargo async_zip 0.0.18 entry/impl#[StoredZipEntry]seek_to_data_offset().(&mut self.readerrust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#reader.).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 101 | |
| 102 | let reader = ZipEntryReaderrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/ZipEntryReader#::new_with_borrowrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/impl#[`ZipEntryReader<'a, R, WithoutEntry>`]new_with_borrow().( |
| 103 | &mut self.readerrust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#reader., |
| 104 | stored_entry.entryrust-analyzer cargo async_zip 0.0.18 entry/StoredZipEntry#entry..compressionrust-analyzer cargo async_zip 0.0.18 entry/impl#[ZipEntry]compression().(), |
| 105 | stored_entry.entryrust-analyzer cargo async_zip 0.0.18 entry/StoredZipEntry#entry..compressed_sizerust-analyzer cargo async_zip 0.0.18 entry/impl#[ZipEntry]compressed_size().(), |
| 106 | ); |
| 107 | |
| 108 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(reader.into_with_entryrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/impl#[`ZipEntryReader<'a, R, WithoutEntry>`]into_with_entry().(stored_entry)) |
| 109 | } |
| 110 | |
| 111 | /// Returns a new entry reader if the provided index is valid. |
| 112 | /// Consumes self |
| 113 | pub async fn into_entryrust-analyzer cargo async_zip 0.0.18 base/read/seek/impl#[`ZipFileReader<R>`]into_entry().<'a>(mut self, index: usize) -> Resultrust-analyzer cargo async_zip 0.0.18 error/Result#<ZipEntryReaderrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/ZipEntryReader#<'a, R, WithoutEntryrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/WithoutEntry#>> |
| 114 | where |
| 115 | R: 'a, |
| 116 | { |
| 117 | let stored_entry = self.filerust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#file..entriesrust-analyzer cargo async_zip 0.0.18 file/ZipFile#entries..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(index).ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(ZipErrorrust-analyzer cargo async_zip 0.0.18 error/ZipError#::EntryIndexOutOfBoundsrust-analyzer cargo async_zip 0.0.18 error/ZipError#EntryIndexOutOfBounds#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 118 | |
| 119 | stored_entry.seek_to_data_offsetrust-analyzer cargo async_zip 0.0.18 entry/impl#[StoredZipEntry]seek_to_data_offset().(&mut self.readerrust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#reader.).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 120 | |
| 121 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ZipEntryReaderrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/ZipEntryReader#::new_with_ownedrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/impl#[`ZipEntryReader<'a, R, WithoutEntry>`]new_with_owned().( |
| 122 | self.readerrust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#reader., |
| 123 | stored_entry.entryrust-analyzer cargo async_zip 0.0.18 entry/StoredZipEntry#entry..compressionrust-analyzer cargo async_zip 0.0.18 entry/impl#[ZipEntry]compression().(), |
| 124 | stored_entry.entryrust-analyzer cargo async_zip 0.0.18 entry/StoredZipEntry#entry..compressed_sizerust-analyzer cargo async_zip 0.0.18 entry/impl#[ZipEntry]compressed_size().(), |
| 125 | )) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | #[cfg(feature = "tokio")] |
| 130 | impl<R> ZipFileReaderrust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#<Compatrust-analyzer cargo tokio-util 0.7.4 compat/Compat#<R>> |
| 131 | where |
| 132 | R: tokiorust-analyzer cargo tokio 1.25.0 crate/::iorust-analyzer cargo tokio 1.25.0 io/::AsyncBufReadrust-analyzer cargo tokio 1.25.0 io/async_buf_read/AsyncBufRead# + tokiorust-analyzer cargo tokio 1.25.0 crate/::iorust-analyzer cargo tokio 1.25.0 io/::AsyncSeekrust-analyzer cargo tokio 1.25.0 io/async_seek/AsyncSeek# + Unpinrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Unpin#, |
| 133 | { |
| 134 | /// Constructs a new tokio-specific ZIP reader from a seekable source. |
| 135 | pub async fn with_tokiorust-analyzer cargo async_zip 0.0.18 base/read/seek/impl#[`ZipFileReader<Compat<R>>`]with_tokio().(reader: R) -> Resultrust-analyzer cargo async_zip 0.0.18 error/Result#<TokioZipFileReaderrust-analyzer cargo async_zip 0.0.18 tokio/read/seek/ZipFileReader#<R>> { |
| 136 | let mut reader = reader.compatrust-analyzer cargo tokio-util 0.7.4 compat/TokioAsyncReadCompatExt#compat().(); |
| 137 | let file = craterust-analyzer cargo async_zip 0.0.18 crate/::baserust-analyzer cargo async_zip 0.0.18 base/::readrust-analyzer cargo async_zip 0.0.18 base/read/::filerust-analyzer cargo async_zip 0.0.18 base/read/file().(&mut reader).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 138 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ZipFileReaderrust-analyzer cargo async_zip 0.0.18 base/read/seek/ZipFileReader#::from_raw_partsrust-analyzer cargo async_zip 0.0.18 base/read/seek/impl#[`ZipFileReader<R>`]from_raw_parts().(reader, file)) |
| 139 | } |
| 140 | } |