this repo has no description
1use super::stream::{copy_stream, guard_file_output, open_input, prepare_output};
2use crate::progress::ProgressArgs;
3use crate::utils::{CmprssInput, CmprssOutput, CommonArgs, Compressor, Result};
4use clap::Args;
5use lz4_flex::frame::{FrameDecoder, FrameEncoder};
6
7#[derive(Args, Debug)]
8pub struct Lz4Args {
9 #[clap(flatten)]
10 pub common_args: CommonArgs,
11
12 #[clap(flatten)]
13 pub progress_args: ProgressArgs,
14}
15
16#[derive(Default, Clone)]
17pub struct Lz4 {
18 pub progress_args: ProgressArgs,
19}
20
21impl Lz4 {
22 pub fn new(args: &Lz4Args) -> Lz4 {
23 Lz4 {
24 progress_args: args.progress_args,
25 }
26 }
27}
28
29impl Compressor for Lz4 {
30 /// The standard extension for the lz4 format.
31 fn extension(&self) -> &str {
32 "lz4"
33 }
34
35 /// Full name for lz4.
36 fn name(&self) -> &str {
37 "lz4"
38 }
39
40 /// Compress an input file or pipe to a lz4 archive
41 fn compress(&self, input: CmprssInput, output: CmprssOutput) -> Result {
42 guard_file_output(&output, "LZ4")?;
43 let (input_stream, file_size, pipeline_inner) = open_input(input, "LZ4")?;
44 let (writer, target) = prepare_output(output)?;
45 let mut encoder = FrameEncoder::new(writer);
46 copy_stream(
47 input_stream,
48 &mut encoder,
49 file_size,
50 pipeline_inner,
51 &self.progress_args,
52 target,
53 )?;
54 encoder.finish()?;
55 Ok(())
56 }
57
58 /// Extract a lz4 archive to an output file or pipe
59 fn extract(&self, input: CmprssInput, output: CmprssOutput) -> Result {
60 guard_file_output(&output, "LZ4")?;
61 let (input_stream, file_size, pipeline_inner) = open_input(input, "LZ4")?;
62 let decoder = FrameDecoder::new(input_stream);
63 let (writer, target) = prepare_output(output)?;
64 copy_stream(
65 decoder,
66 writer,
67 file_size,
68 pipeline_inner,
69 &self.progress_args,
70 target,
71 )?;
72 Ok(())
73 }
74}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79 use crate::test_utils::*;
80
81 /// Test the basic interface of the Lz4 compressor
82 #[test]
83 fn test_lz4_interface() {
84 let compressor = Lz4::default();
85 test_compressor_interface(&compressor, "lz4", Some("lz4"));
86 }
87
88 /// Test the default compression level
89 #[test]
90 fn test_lz4_default_compression() -> Result {
91 let compressor = Lz4::default();
92 test_compression(&compressor)
93 }
94}