SHARE
TWEET

Untitled

a guest Apr 20th, 2019 95 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;
  7. use tar::Entry;
  8.  
  9. fn pick_entry<'a, R: Read>(archive: &'a mut Archive<R>, file: &str)
  10. -> Result<Entry<'a, R>, Box<dyn Error>> {
  11.     for entry in archive.entries().expect("Cannot be called more than once mate") {
  12.         let entry = entry?;
  13.         if entry.path()?.to_string_lossy() == file {
  14.             return Ok(entry)
  15.         }
  16.     }
  17.        
  18.     Err(Box::new(io::Error::new(
  19.         io::ErrorKind::NotFound,
  20.         "File not found in archive",
  21.     )))
  22. }
  23.  
  24. fn main() {
  25.     let reader = GzDecoder::new(File::open("a.tar.gz").unwrap());
  26.     let mut archive = Archive::new(reader);
  27.     let mut entry = pick_entry(&mut archive, "a/file1").unwrap();
  28.     let mut data = Vec::new();
  29.  
  30.     entry.read_to_end(&mut data).unwrap();
  31. }
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