Move src/bin/* to an examples directory

This commit is contained in:
Alexandre Bury 2016-04-17 17:08:23 -07:00
parent a4f44d3703
commit 34dc231ff9
5 changed files with 45 additions and 79 deletions

View file

@ -8,27 +8,18 @@ name = "zstd"
repository = "https://github.com/Gyscos/zstd-rs"
version = "0.1.5"
[[bin]]
doc = false
name = "zstd"
test = false
[[bin]]
doc = false
name = "stream"
test = false
[[bin]]
doc = false
name = "train"
test = false
[build-dependencies]
gcc = "0.3"
[dependencies]
libc = "0.2"
clippy = { version = "0.0.44", optional = true }
[dependencies.clippy]
optional = true
version = "0.0.44"
[dev-dependencies]
clap = "2.2.6"
[features]
default = []

38
examples/train.rs Normal file
View file

@ -0,0 +1,38 @@
extern crate zstd;
#[macro_use]
extern crate clap;
use std::io;
use clap::{App, Arg};
// This program trains a dictionary from one or more files,
// to make future compression of similar small files more efficient.
//
// The dictionary will need to be present during decompression,
// but it you need to compress many small files individually,
// it may be worth the trouble.
fn main() {
let matches = App::new("train")
.author("Alexandre Bury <alexandre.bury@gmail.com>")
.about("A zstd dict trainer")
.arg(Arg::with_name("MAX_SIZE")
.help("Maximum dictionary size in bytes")
.short("s")
.long("max_size")
.takes_value(true))
.arg(Arg::with_name("FILE")
.help("Files to use as input")
.required(true)
.multiple(true))
.get_matches();
let size = value_t!(matches, "MAX_SIZE", usize).unwrap_or(110 * 1024);
let files: Vec<_> = matches.values_of("FILE").unwrap().collect();
let dict = zstd::dict::from_files(&files, size).unwrap();
let mut dict_reader: &[u8] = &dict;
io::copy(&mut dict_reader, &mut io::stdout()).unwrap();
}

View file

@ -1,63 +0,0 @@
extern crate zstd;
use std::env;
use std::fs;
use std::str::FromStr;
use std::io::{self, Read, Write};
// This program trains a dictionary from one or more files,
// to make future compression of similar small files more efficient.
//
// The dictionary will need to be present during decompression,
// but it you need to compress many small files individually,
// it may be worth the trouble.
fn main() {
// We will concatenate all the input files in one big buffer.
let mut buffer = Vec::new();
// We'll keep track of each file size
let mut sample_sizes = Vec::new();
// Passing "--" treats everything after that as a filename
let mut ignore_options = false;
// A tiny state machine to parse the max size.
let mut expect_size = false;
let mut max_size = 110 * 1024;
if env::args().len() == 1 {
writeln!(io::stderr(), "Usage: `train [-c MAX_SIZE] FILES...`")
.unwrap();
return;
}
// We do some ugly manual option parsing, because I'm trying to avoid using `clap`.
// When cargo allows per-binary dependencies, this may get cleaner.
for filename in env::args().skip(1) {
if !ignore_options {
match filename.as_ref() {
"--" => {
ignore_options = true;
continue;
}
"-c" => {
expect_size = true;
continue;
}
other if expect_size => {
expect_size = false;
max_size = usize::from_str(other).expect("Invalid size");
continue;
}
_ => (),
}
}
let mut file = fs::File::open(filename).unwrap();
let size = file.read_to_end(&mut buffer).unwrap();
sample_sizes.push(size);
}
let dict = zstd::dict::from_continuous(&buffer, &sample_sizes, max_size)
.unwrap();
let mut dict_reader: &[u8] = &dict;
io::copy(&mut dict_reader, &mut io::stdout()).unwrap();
}