Created
August 19, 2018 09:54
-
-
Save boyter/95afeb5cd3943fc4ab53e2198dd9b3e1 to your computer and use it in GitHub Desktop.
Walk directory in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate walkdir; | |
use walkdir::WalkDir; | |
use std::fs::File; | |
use std::io::Read; | |
fn main() { | |
let nul = 0; | |
let mut bytes_count: i32; | |
for entry in WalkDir::new("./").into_iter().filter_map(|e| e.ok()) { | |
if entry.file_type().is_file() { | |
let mut file = File::open(entry.path().display().to_string()).unwrap(); | |
bytes_count = 0; | |
let mut s: Vec<u8> = Vec::with_capacity(file.metadata().unwrap().len() as usize); | |
file.read_to_end(&mut s).unwrap(); | |
for b in s.into_iter() { | |
if b == nul { | |
println!("{} bytes={} binary file", entry.path().display().to_string(), bytes_count); | |
break | |
} | |
bytes_count += 1; | |
} | |
println!("{} bytes={}", entry.path().display().to_string(), bytes_count) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment