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 as TarArchive;
- use tar::Entry as TarEntry;
- pub struct Archive {
- archive: TarArchive<Box<dyn Read>>,
- }
- pub struct ArchiveEntry<'a> {
- entry: TarEntry<'a, Box<dyn Read>>,
- }
- impl Archive {
- pub fn open(archive: &str) -> Result<Self, Box<dyn Error>> {
- let reader: Box<dyn Read> = Box::new(GzDecoder::new(File::open(archive)?));
- let archive = TarArchive::new(reader);
- Ok(Archive { archive })
- }
- pub fn open_file(&mut self, file: &str) -> Result<ArchiveEntry, Box<dyn Error>> {
- for entry in self.archive.entries()? {
- let entry = entry?;
- if entry.path()?.to_string_lossy() == file {
- return Ok(ArchiveEntry { entry });
- }
- }
- Err(Box::new(io::Error::new(
- io::ErrorKind::NotFound,
- "File not found in archive",
- )))
- }
- }
- impl<'a> Read for ArchiveEntry<'a> {
- fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
- self.entry.read(buf)
- }
- }
- fn main() {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement