diff --git a/Cargo.lock b/Cargo.lock index 6527de5..bb979e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -354,6 +354,7 @@ dependencies = [ "eyre", "indicatif", "rayon", + "rust-lzma", "tar", "unrar", "zip", @@ -726,6 +727,16 @@ version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" +[[package]] +name = "rust-lzma" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d62915608f6cee1d7f2fc00f28b4f058ff79d6e4ec3c2fe0006b09b52437c84" +dependencies = [ + "pkg-config", + "vcpkg", +] + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -984,6 +995,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "vec_map" version = "0.8.2" diff --git a/Cargo.toml b/Cargo.toml index 67c1300..49a134f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ crossterm = "0.27.0" eyre = "0.6.8" indicatif = "0.17.6" rayon = "1.7.0" -#rust-lzma = "0.6.0" +rust-lzma = "0.6.0" tar = "0.4.40" unrar = "0.5.1" zip = "0.6.6" diff --git a/flake.nix b/flake.nix index 07a17c4..4836dbe 100644 --- a/flake.nix +++ b/flake.nix @@ -1,5 +1,5 @@ { - description = "Spec's NixOS-config"; + description = "ExtractRS for all you decompression needs"; inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; }; @@ -19,13 +19,10 @@ clippy lolcat rust-analyzer - xz # Add xz (liblzma) to the build inputs + xz + pkg-config ]; - # Add a shellHook to modify PKG_CONFIG_PATH - shellHook = '' - export PKG_CONFIG_PATH="${pkgs.xz}/lib/pkgconfig:$PKG_CONFIG_PATH" - ''; }; }; } diff --git a/src/extractors.rs b/src/extractors.rs index 5a766a4..ab03410 100644 --- a/src/extractors.rs +++ b/src/extractors.rs @@ -1,9 +1,9 @@ /* TODO_2: implement remaining extractor functions and write tests */ -use std::{fs, io::{self, Write}, path::Path, fs::File}; +use std::{fs::{self, File}, io, path::Path,}; use unrar::Archive; -// use lzma::reader::LzmaReader; +use lzma::reader::LzmaReader; pub fn extract_zip(zip_file: &Path) -> io::Result<()> { let file = fs::File::open(zip_file)?; let mut archive = zip::ZipArchive::new(file)?; @@ -76,7 +76,7 @@ pub fn extract_rar(rar_file: &Path) -> Result<(), Box> { Ok(()) } - pub fn extract_tar(tar_file: &Path) -> io::Result<()> { +pub fn extract_tar(tar_file: &Path) -> io::Result<()> { let tar_file = fs::File::open(tar_file)?; let mut a = tar::Archive::new(tar_file); @@ -97,31 +97,25 @@ pub fn extract_rar(rar_file: &Path) -> Result<(), Box> { Ok(()) } -// pub fn extract_xz(xz_file: &Path, output_dir: &Path) -> io::Result<()> { -// // Open the xz file for reading -// let file = File::open(xz_file)?; +pub fn extract_xz(input_path: &Path, output_directory: &Path) -> Result<(), io::Error> { + // Open the input XZ file + let input_file = File::open(input_path)?; -// // Create a LzmaReader to decompress the data -// let mut xz_reader = LzmaReader::new_decompressor(file)?; + // Create a decompression reader + let mut decompressor = LzmaReader::new_decompressor(input_file) + .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?; -// // Read the decompressed data into a buffer -// let mut decompressed_data = Vec::new(); -// xz_reader.read_to_end(&mut decompressed_data)?; + // Determine the output file path + let output_file_path = output_directory.join(input_path.file_stem().unwrap()); -// // Get the filename from the original path -// let xz_filename = xz_file -// .file_name() -// .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Invalid file name"))?; - -// // Create the output file path by appending the filename to the output directory -// let output_path = output_dir.join(xz_filename); + // Create the output file + let mut output_file = File::create(&output_file_path)?; -// // Create or overwrite the output file and write the decompressed data to it -// let mut output_file = File::create(output_path)?; -// output_file.write_all(&decompressed_data)?; + // Read from the decompressor and write to the output file + io::copy(&mut decompressor, &mut output_file)?; -// Ok(()) -// } + Ok(()) +} pub fn extract_bz2(){} pub fn extract_tbz2(){} pub fn extract_tgz(){} diff --git a/src/main.rs b/src/main.rs index 23bb15c..a183f3a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ use extractors::{ extract_zip, extract_rar, extract_tar, - // extract_xz, + extract_xz, // extract_bz2, // extract_tbz2, // extract_tgz, @@ -70,17 +70,17 @@ fn run() -> i32 { return 1; } } - // "xz" => { - // let output_directory = Path::new("output_directory"); // Change this to your desired output directory - // if let Err(err) = fs::create_dir_all(&output_directory) { - // println!("Error creating output directory: {}", err); - // return 1; - // } - // if let Err(err) = extract_xz(&fname, &output_directory) { - // println!("Error extracting XZ: {}", err); - // return 1; - // } - // } + "xz" => { + let output_directory = Path::new("output_directory"); // Change this to your desired output directory + if let Err(err) = fs::create_dir_all(&output_directory) { + println!("Error creating output directory: {}", err); + return 1; + } + if let Err(err) = extract_xz(&fname, &output_directory) { + println!("Error extracting XZ: {}", err); + return 1; + } + } /* "gz" => { if let Err(err) = extract_gz(&fname) {