Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add append_dir_all doc test without renaming #365

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,35 @@ impl<W: Write> Builder<W> {
/// // with a different name.
/// ar.append_dir_all("bardir", ".").unwrap();
/// ```
///
/// Use `append_dir_all` with an empty string as the first path argument to
/// create an archive from all files in a directory without renaming.
///
/// ```
/// use std::fs;
/// use std::path::PathBuf;
/// use tar::{Archive, Builder};
///
/// let tmpdir = tempfile::tempdir().unwrap();
/// let path = tmpdir.path();
/// fs::write(path.join("a.txt"), b"hello").unwrap();
/// fs::write(path.join("b.txt"), b"world").unwrap();
///
/// // Create a tarball from the files in the directory
/// let mut ar = Builder::new(Vec::new());
/// ar.append_dir_all("", path).unwrap();
///
/// // List files in the archive
/// let archive = ar.into_inner().unwrap();
/// let archived_files = Archive::new(archive.as_slice())
/// .entries()
/// .unwrap()
/// .map(|entry| entry.unwrap().path().unwrap().into_owned())
/// .collect::<Vec<_>>();
///
/// assert!(archived_files.contains(&PathBuf::from("a.txt")));
/// assert!(archived_files.contains(&PathBuf::from("b.txt")));
/// ```
pub fn append_dir_all<P, Q>(&mut self, path: P, src_path: Q) -> io::Result<()>
where
P: AsRef<Path>,
Expand Down