Mirror of space-wizards/bsdiff-rs
Find a file
2025-06-04 23:06:22 +01:00
.github/workflows A few improvement ideas 2024-04-01 15:08:35 -04:00
src remove unused import 2025-06-04 23:06:22 +01:00
tests Move functions to top level 2021-08-08 23:54:11 +01:00
.editorconfig A few improvement ideas 2024-04-01 15:08:35 -04:00
.gitignore A few improvement ideas 2024-04-01 15:08:35 -04:00
Cargo.toml add docs and examples 2025-06-04 23:06:22 +01:00
LICENSE Repurpose project, more cleanup. 2017-06-21 23:41:49 +02:00
README.md A few improvement ideas 2024-04-01 15:08:35 -04:00

bsdiff-rs

GitHub crates.io version docs.rs docs crates.io version CI build

Bsdiff is a method of diffing files. This crate is a port of a bsdiff library. High performance patching. All written in safe Rust.

It is usually a good idea to use bsdiff alongside a compression algorithm like bzip2.

Usage

fn main() {
    let one = vec![1, 2, 3, 4, 5];
    let two = vec![1, 2, 4, 6];
    let mut patch = Vec::new();

    bsdiff::diff(&one, &two, &mut patch).unwrap();

    let mut patched = Vec::with_capacity(two.len());
    bsdiff::patch(&one, &mut patch.as_slice(), &mut patched).unwrap();
    assert_eq!(patched, two);
}

Diffing Files

fn diff_files(file_a: &str, file_b: &str, patch_file: &str) -> std::io::Result<()> {
    let old = std::fs::read(file_a)?;
    let new = std::fs::read(file_b)?;
    let mut patch = Vec::new();

    bsdiff::diff(&old, &new, &mut patch)?;
    // TODO: compress `patch` here
    std::fs::write(patch_file, &patch)
}

Patching Files

fn patch_file(file_a: &str, patch_file: &str, file_b: &str) -> std::io::Result<()> {
    let old = std::fs::read(file_a)?;
    let patch = std::fs::read(patch_file)?;
    // TODO: decompress `patch` here
    let mut new = Vec::new();

    bsdiff::patch(&old, &mut patch.as_slice(), &mut new)?;
    std::fs::write(file_b, &new)
}