Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  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, Entries, Entry};
  7.  
  8. pub struct ArchiveFile<'a> {
  9. archive: Archive<Box<dyn Read>>,
  10. file: String,
  11. entry: Option<Entry<'a, Box<dyn Read>>>,
  12. }
  13.  
  14. impl<'a> ArchiveFile<'a> {
  15. pub fn open(archive: &str, file: &str) -> Result<Self, Box<dyn Error>> {
  16. let reader: Box<dyn Read> = Box::new(GzDecoder::new(File::open(archive)?));
  17. let mut archive = Archive::new(reader);
  18.  
  19. Ok(ArchiveFile {
  20. archive,
  21. file: file.to_string(),
  22. entry: None,
  23. })
  24. }
  25. }
  26.  
  27. impl<'a> Read for ArchiveFile<'a> {
  28. fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
  29. match self.entry {
  30. None => {
  31. for entry in self.archive.entries().unwrap() {
  32. let entry = entry.unwrap();
  33.  
  34. if self.file == entry.path().unwrap().to_string_lossy() {
  35. self.entry = Some(entry);
  36. return self.entry.unwrap().read(buf);
  37. }
  38. }
  39.  
  40. Err(io::Error::new(
  41. io::ErrorKind::NotFound,
  42. "File not found in archive",
  43. ))
  44. }
  45. Some(entry) => entry.read(buf),
  46. }
  47. }
  48. }
  49.  
  50. fn main() {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement