SHARE
TWEET

Untitled

a guest Apr 20th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use flate2::read::GzDecoder;
  2. use std::error::Error;
  3. use std::fs::File;
  4. use std::io;
  5. use std::io::Read;
  6. use tar::Archive as TarArchive;
  7. use tar::Entry as TarEntry;
  8.  
  9. pub struct Archive {
  10.     archive: TarArchive<Box<dyn Read>>,
  11. }
  12.  
  13. pub struct ArchiveEntry<'a> {
  14.     entry: TarEntry<'a, Box<dyn Read>>,
  15. }
  16.  
  17. impl Archive {
  18.     pub fn open(archive: &str) -> Result<Self, Box<dyn Error>> {
  19.         let reader: Box<dyn Read> = Box::new(GzDecoder::new(File::open(archive)?));
  20.         let archive = TarArchive::new(reader);
  21.  
  22.         Ok(Archive { archive })
  23.     }
  24.  
  25.     pub fn open_file(&mut self, file: &str) -> Result<ArchiveEntry, Box<dyn Error>> {
  26.         for entry in self.archive.entries()? {
  27.             let entry = entry?;
  28.  
  29.             if entry.path()?.to_string_lossy() == file {
  30.                 return Ok(ArchiveEntry { entry });
  31.             }
  32.         }
  33.  
  34.         Err(Box::new(io::Error::new(
  35.             io::ErrorKind::NotFound,
  36.             "File not found in archive",
  37.         )))
  38.     }
  39. }
  40.  
  41. impl<'a> Read for ArchiveEntry<'a> {
  42.     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
  43.         self.entry.read(buf)
  44.     }
  45. }
  46.  
  47. fn main() {}
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top