Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use flate2::read::GzDecoder;
- use std::error::Error;
- use std::fs::File;
- use std::io;
- use std::io::Read;
- use tar::{Archive, Entries, Entry};
- pub struct ArchiveFile<'a> {
- archive: Archive<Box<dyn Read>>,
- file: String,
- entry: Option<Entry<'a, Box<dyn Read>>>,
- }
- impl<'a> ArchiveFile<'a> {
- pub fn open(archive: &str, file: &str) -> Result<Self, Box<dyn Error>> {
- let reader: Box<dyn Read> = Box::new(GzDecoder::new(File::open(archive)?));
- let mut archive = Archive::new(reader);
- Ok(ArchiveFile {
- archive,
- file: file.to_string(),
- entry: None,
- })
- }
- }
- impl<'a> Read for ArchiveFile<'a> {
- fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
- match self.entry {
- None => {
- for entry in self.archive.entries().unwrap() {
- let entry = entry.unwrap();
- if self.file == entry.path().unwrap().to_string_lossy() {
- self.entry = Some(entry);
- return self.entry.unwrap().read(buf);
- }
- }
- Err(io::Error::new(
- io::ErrorKind::NotFound,
- "File not found in archive",
- ))
- }
- Some(entry) => entry.read(buf),
- }
- }
- }
- fn main() {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement