an efficient binary archive format

Bindle::read_to

+25
+25
src/lib.rs
··· 437 437 } 438 438 } 439 439 440 + pub fn read_to<W: std::io::Write>(&self, name: &str, mut w: W) -> std::io::Result<()> { 441 + let entry = self 442 + .index 443 + .get(name) 444 + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "Invalid entry"))?; 445 + let mmap = self 446 + .mmap 447 + .as_ref() 448 + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "Missing mmap"))?; 449 + let data = mmap 450 + .get(entry.offset() as usize..(entry.offset() + entry.compressed_size()) as usize) 451 + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "Invalid mmap offset"))?; 452 + 453 + if entry.compression_type == 1 { 454 + std::io::copy( 455 + &mut zstd::Decoder::new(data) 456 + .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?, 457 + &mut w, 458 + )?; 459 + } else { 460 + w.write_all(&data)?; 461 + } 462 + Ok(()) 463 + } 464 + 440 465 /// The number of entries 441 466 pub fn len(&self) -> usize { 442 467 self.index.len()