Tar file reading/writing for Rust

Related tags

Compression tar-rs
Overview

tar-rs

Documentation

A tar archive reading/writing library for Rust.

# Cargo.toml
[dependencies]
tar = "0.4"

Reading an archive

extern crate tar;

use std::io::prelude::*;
use std::fs::File;
use tar::Archive;

fn main() {
    let file = File::open("foo.tar").unwrap();
    let mut a = Archive::new(file);

    for file in a.entries().unwrap() {
        // Make sure there wasn't an I/O error
        let mut file = file.unwrap();

        // Inspect metadata about the file
        println!("{:?}", file.header().path().unwrap());
        println!("{}", file.header().size().unwrap());

        // files implement the Read trait
        let mut s = String::new();
        file.read_to_string(&mut s).unwrap();
        println!("{}", s);
    }
}

Writing an archive

extern crate tar;

use std::io::prelude::*;
use std::fs::File;
use tar::Builder;

fn main() {
    let file = File::create("foo.tar").unwrap();
    let mut a = Builder::new(file);

    a.append_path("file1.txt").unwrap();
    a.append_file("file2.txt", &mut File::open("file3.txt").unwrap()).unwrap();
}

License

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Comments
  • Building an Archive from a MacOS App Bundle

    Building an Archive from a MacOS App Bundle

    When using the function append_dir_all it generates the archive just fine, but it's not copying the attributes properly, thus when I try to launch the app bundle after unpacking the generated archive it says that the file is corrupted, I can fix the issue by manually running xattr command on the app bundle, but that's not a feasible solution whatsoever.

    Code:

                    let mut tar = Builder::new(&mut vec);
    
                    tar.append_dir_all("BigBro.app", MACOS_EXECUTABLE_PATH)?;
    
                    tar.finish()?;
    
                    drop(tar);
    
    opened by Selyatin 0
  • Update xattr requirement from 0.2 to 1.0

    Update xattr requirement from 0.2 to 1.0

    Updates the requirements on xattr to permit the latest version.

    Commits
    • ff56009 Merge pull request #30 from Stebalien/chore/release
    • 036cc12 chore: release 1.0.0
    • 6b67d6b Merge pull request #29 from Stebalien/chore/cleanup
    • a60ad02 fix: macos build
    • 453a7d5 chore: 2021 edition and friends
    • cb96edf Merge pull request #27 from asomers/fbsd-12-3
    • 48791d8 Merge pull request #26 from asomers/use_libc
    • 4bcd35a Overhaul CI
    • 33d0210 Use libc's definitions where possible
    • cfc242e Merge pull request #25 from Stebalien/chore/use-unsupported
    • Additional commits viewable in compare view

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    dependencies 
    opened by dependabot[bot] 0
  • Error:

    Error: "failed to write entire file" in .unpack()

    I keep getting failed to write entire file errors when calling the .unpack() and .unpack_in() methods from Entry.

    I saw that the destination file was correctly being created and filled with 4096 bytes of data, then the error appears.

    I have no idea why this happens - could you please enlighten me?

    Thanks in advance for your help!

    opened by ClementNerma 7
  • Calling set_size writes wrong value

    Calling set_size writes wrong value

    hello! I've used the tar crate in a project to modify an archive and noticed the generated archives are invalid in some cases, /usr/bin/tar complains about a truncated archive and after some inspection I noticed the size that gets written appears to be multiplied by 8.

    This doesn't seem to be an interoperability issue, calling .size() immediately after calling .set_size(...) also returns a value that's much bigger than what I intended to set.

    I hacked together a minimal test case that searches with cargo-fuzz and found one:

    #![no_main]
    use libfuzzer_sys::fuzz_target;
    
    use anyhow::Result;
    use anyhow::Context;
    use tar;
    
    fn run_test(data: &[u8]) -> Result<()> {
        let mut r = &data[..];
        let mut a = tar::Archive::new(&mut r);
        let entry = a.entries()?.next().context("")??;
        let mut h = entry.header().clone();
    
        h.set_size(1337);
        if h.size().context("failed to get size")? != 1337 {
            panic!("bugged header");
        }
    
        Ok(())
    }
    
    fuzz_target!(|data: &[u8]| {
        run_test(data).ok();
    });
    

    tar

    [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 247, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 197, 250, 0, 0, 0, 0, 0, 0, 255, 9, 9, 181, 225, 154, 128, 0, 0, 16, 0, 0, 0, 0, 0, 181, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 255, 255, 5, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 46, 0, 0, 9, 9, 9, 181, 181, 181, 181, 1, 0, 0, 0, 0, 0, 0, 49, 0, 0, 0, 0, 0, 9, 181, 181, 181, 181, 55, 55, 55, 55, 55, 55, 55, 0, 0, 0, 181, 0, 0, 0, 0, 0, 181, 181, 64, 13, 245, 181, 181, 181, 181, 181, 181, 0, 245, 181, 181, 181, 181, 181, 181, 0, 0, 0, 0, 0, 41, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 9, 9, 181, 181, 181, 181, 0, 0, 0, 0, 0, 0, 0, 0, 181, 230, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 0, 0, 0, 0, 0, 12, 0, 247, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 46, 0, 0, 9, 9, 9, 181, 181, 181, 181, 1, 0, 0, 0, 0, 0, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 181, 181, 181, 181, 13, 181, 180, 181, 181, 181, 181, 181, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 181, 181, 181, 181, 13, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 0, 0, 0, 5, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 247, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 181, 225, 154, 128, 0, 0, 16, 0, 0, 0, 0, 0, 181, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 224, 255, 255, 5, 255, 255, 255, 255, 255, 255, 233, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 247, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 9, 9, 181, 181, 181, 181, 55, 55, 55, 55, 55, 55, 54, 0, 0, 0, 0, 0, 0, 0, 0, 181, 181, 181, 64, 13, 245, 181, 181, 181, 181, 181, 181, 0, 245, 181, 181, 181, 181, 181, 181, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 254, 255, 255, 255, 9, 0]
    

    Screenshot

    image

    Possibly related to #286, #298

    opened by kpcyrd 0
  • Alternative append API

    Alternative append API

    At the moment in the API, I have to provide a Read and then the append function(s) will io::copy over the bytes in one go. This is fine for many use cases, but not always desirable.

    As an alternative that may work better in some cases, I propose having an API that instead of taking a Read returns a Write. One can then write into there in order to write into the archive. Lifetimes are use to enforce that no other functions can be called on the archive in the meantime, and the Drop implementation takes care of finalizing the file (i.e. add the padding).

    opened by piegamesde 1
Owner
Alex Crichton
Alex Crichton
Opens files for reading.

smart-open Opens files for reading. What?? smart_open is a rust crate under active development with the goal of opening any text file (compressed or u

Joydeep Bhattacharjee 6 Dec 30, 2020
A utility that can download JavaScript and TypeScript module graphs and store them locally in a special zip file.

eszip A utility that can download JavaScript and TypeScript module graphs and store them locally in a special zip file. To create a new archive: > esz

Deno Land 162 Dec 24, 2022
(WIP) Taking the pain away from file (de)compression

Ouch! ouch loosely stands for Obvious Unified Compression files Helper and aims to be an easy and intuitive way of compressing and decompressing files

Vinícius Miguel 734 Dec 30, 2022
📦 Unpack deep archive files recursively over a file tree or a folder

deep-unpack Unpack deep archive files recursively over a file tree or a folder. Usage [dependencies] deep-unpack = { version = "0.1.2" } Usage fn main

null 3 Dec 4, 2022
A simple rust library to read and write Zip archives, which is also my pet project for learning Rust

rust-zip A simple rust library to read and write Zip archives, which is also my pet project for learning Rust. At the moment you can list the files in

Kang Seonghoon 2 Jan 5, 2022
A Brotli implementation in pure and safe Rust

Brotli-rs - Brotli decompression in pure, safe Rust Documentation Compression provides a <Read>-struct to wrap a Brotli-compressed stream. A consumer

Thomas Pickert 59 Oct 7, 2022
Brotli compressor and decompressor written in rust that optionally avoids the stdlib

rust-brotli What's new in 3.2 into_inner conversions for both Reader and Writer classes What's new in 3.0 A fully compatible FFI for drop-in compatibi

Dropbox 659 Dec 29, 2022
libbz2 (bzip2 compression) bindings for Rust

bzip2 Documentation A streaming compression/decompression library for rust with bindings to libbz2. # Cargo.toml [dependencies] bzip2 = "0.4" License

Alex Crichton 67 Dec 27, 2022
A Rust implementation of the Zopfli compression algorithm.

Zopfli in Rust This is a reimplementation of the Zopfli compression tool in Rust. I have totally ignored zopflipng. More info about why and how I did

Carol (Nichols || Goulding) 76 Oct 20, 2022
DEFLATE, gzip, and zlib bindings for Rust

flate2 A streaming compression/decompression library DEFLATE-based streams in Rust. This crate by default uses the miniz_oxide crate, a port of miniz.

The Rust Programming Language 619 Jan 8, 2023
Snappy bindings for Rust

Snappy [ Originally forked from https://github.com/thestinger/rust-snappy ] Documentation Usage Add this to your Cargo.toml: [dependencies] snappy = "

Jeff Belgum 14 Jan 21, 2022
Zip implementation in Rust

zip-rs Documentation Info A zip library for rust which supports reading and writing of simple ZIP files. Supported compression formats: stored (i.e. n

null 549 Jan 4, 2023
Pure Rust bzip2 decoder

bzip2-rs Pure Rust 100% safe bzip2 decompressor. Features Default features: Rust >= 1.34.2 is supported rustc_1_37: bump MSRV to 1.37, enable more opt

Paolo Barbolini 36 Jan 6, 2023
Like pigz, but rust - a cross platform, fast, compression and decompression tool.

?? crabz Like pigz, but rust. A cross platform, fast, compression and decompression tool. Synopsis This is currently a proof of concept CLI tool using

Seth 232 Jan 2, 2023
Ribzip2 - A bzip2 implementation in pure Rust.

ribzip2 - a comprehensible bzip2 implementation ribzip2 is command line utility providing bzip2 compression and decompression written in pure Rust. It

null 16 Oct 24, 2022
A reimplementation of the Zopfli compression tool in Rust.

Zopfli in Rust This is a reimplementation of the Zopfli compression tool in Rust. Carol Nichols started the Rust implementation as an experiment in in

null 11 Dec 26, 2022
A Rust application that compress files and folders

Quick Storer This is a Rust application that compress files and folders. Usage Download or build the binary and place it on your desktop, or any other

AL68 & co. 1 Feb 2, 2022
lzlib (lzip compression) bindings for Rust

lzip Documentation A streaming compression/decompression library for rust with bindings to lzlib. # Cargo.toml [dependencies] lzip = "0.1" License Lic

Firas Khalil Khana 8 Sep 20, 2022
banzai: pure rust bzip2 encoder

banzai banzai is a bzip2 encoder with linear-time complexity, written entirely in safe Rust. It is currently alpha software, which means that it is no

Jack Byrne 27 Oct 24, 2022