pub struct CreateRequest<'a> { /* private fields */ }Expand description
Creates an archive from files and directories.
Holds the CreationConfig and an optional ProgressCallback; the
destination is chosen by the terminal method: a file
(create), a writer (write) or memory
(to_bytes).
§Errors
All terminals return an error if:
- a source does not exist or cannot be read
- the configuration is invalid
- writing the archive fails
create also returns an error if the output is refused (see
OutputFile) or its format cannot be determined. Sources are validated
before the output is published, so a failed creation leaves no archive.
§Examples
use exarch_core::CreateRequest;
use exarch_core::CreationConfig;
use exarch_core::OutputFile;
let dir = tempfile::tempdir()?;
std::fs::write(dir.path().join("a.txt"), b"hello")?;
let config = CreationConfig::default();
let output = dir.path().join("out.tar.gz");
let report = CreateRequest::new(&config).create(
&OutputFile::create_new(&output),
&[dir.path().join("a.txt")],
)?;
assert_eq!(report.files_added, 1);
assert!(output.exists());
let missing = dir.path().join("missing.tar.gz");
assert!(
CreateRequest::new(&config)
.create(
&OutputFile::create_new(&missing),
&[dir.path().join("nope")]
)
.is_err()
);
assert!(!missing.exists());Implementations§
Source§impl<'a> CreateRequest<'a>
impl<'a> CreateRequest<'a>
Sourcepub const fn new(config: &'a CreationConfig) -> Self
pub const fn new(config: &'a CreationConfig) -> Self
A creation under config with no progress callback.
Sourcepub fn with_progress(self, progress: &'a mut dyn ProgressCallback) -> Self
pub fn with_progress(self, progress: &'a mut dyn ProgressCallback) -> Self
Reports progress to progress.
Sourcepub fn create<P: AsRef<Path>>(
self,
output: &OutputFile,
sources: &[P],
) -> Result<CreationReport>
pub fn create<P: AsRef<Path>>( self, output: &OutputFile, sources: &[P], ) -> Result<CreationReport>
Creates the archive at output from sources.
CreationReport::bytes_compressed is the size of the archive file.
§Errors
See CreateRequest.
Sourcepub fn write<P: AsRef<Path>>(
self,
writer: ArchiveWriter<'_>,
sources: &[P],
) -> Result<CreationReport>
pub fn write<P: AsRef<Path>>( self, writer: ArchiveWriter<'_>, sources: &[P], ) -> Result<CreationReport>
Creates the archive from sources into writer.
The writer fixes the format. CreationReport::bytes_compressed is the
number of bytes written to it.
§Errors
See CreateRequest.
§Examples
use exarch_core::ArchiveWriter;
use exarch_core::CreateRequest;
use exarch_core::CreationConfig;
use exarch_core::formats::CompressionCodec;
let src = tempfile::tempdir()?;
std::fs::write(src.path().join("a.txt"), b"hello")?;
let mut buf = Vec::new();
let config = CreationConfig::default();
let report = CreateRequest::new(&config).write(
ArchiveWriter::tar(&mut buf, Some(CompressionCodec::Gzip)),
&[src.path()],
)?;
assert_eq!(report.bytes_compressed, buf.len() as u64);Sourcepub fn to_bytes<P: AsRef<Path>>(
self,
format: CreatableFormat,
sources: &[P],
) -> Result<(Vec<u8>, CreationReport)>
pub fn to_bytes<P: AsRef<Path>>( self, format: CreatableFormat, sources: &[P], ) -> Result<(Vec<u8>, CreationReport)>
Creates an archive of format from sources and returns its bytes.
The whole archive is held in memory, so use create for
large ones. Because the finished ZIP can be read back, a ZIP result
carries the same warning as create when default
extraction would refuse an entry as a zip bomb.
§Errors
See CreateRequest.
§Examples
use exarch_core::CreateRequest;
use exarch_core::CreationConfig;
use exarch_core::formats::detect::CreatableFormat;
let src = tempfile::tempdir()?;
std::fs::write(src.path().join("a.txt"), b"hello")?;
let config = CreationConfig::default();
let (bytes, report) =
CreateRequest::new(&config).to_bytes(CreatableFormat::TarGz, &[src.path()])?;
assert_eq!(report.bytes_compressed, bytes.len() as u64);