Skip to content

Instantly share code, notes, and snippets.

@ucarion
Created January 30, 2015 20:17
Show Gist options
  • Save ucarion/feaca76de5705507ff17 to your computer and use it in GitHub Desktop.
Save ucarion/feaca76de5705507ff17 to your computer and use it in GitHub Desktop.
Read a blob file in Rust
extern crate flate2;
use std::old_io::{BufferedReader, File};
use flate2::reader::ZlibDecoder;
#[test]
fn test() {
let path = ".git/objects/c3/05bbc0533de6c05e73f37280d31667c5514169";
let file = File::open(&Path::new(path));
let mut reader = BufferedReader::new(file);
let mut d = ZlibDecoder::new(reader);
let bytes = d.read_to_end().unwrap();
let null_byte_index = bytes.position_elem(&0).unwrap();
let header = &bytes[0..null_byte_index];
let blob_contents = &bytes[null_byte_index + 1..];
println!("{:?}", std::str::from_utf8(header));
println!("{}", std::str::from_utf8(blob_contents).unwrap());
println!("{}", blob_contents.len());
panic!();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment