nbtq/lib.rs
1use std::{
2 fs::File,
3 io::{self, Read, Seek},
4};
5
6pub fn detect_and_read_nbt(input: &mut File) -> io::Result<nbt::Blob> {
7 input.seek(std::io::SeekFrom::Start(0))?;
8 let mut header = [0; 2];
9 input.read_exact(&mut header)?;
10 input.seek(std::io::SeekFrom::Start(0))?;
11 match header {
12 // Common ZLIB headers: https://stackoverflow.com/a/17176881
13 [0x78, 0x01] | [0x78, 0x9C] | [0x78, 0xDA] => Ok(nbt::from_zlib_reader(input)?),
14 // GZIP header: https://en.wikipedia.org/wiki/Gzip#File_format
15 [0x1f, 0x8b] => Ok(nbt::from_gzip_reader(input)?),
16 // Assume we have raw (uncompressed) NBT
17 _ => Ok(nbt::from_reader(input)?),
18 }
19}