added xz support

This commit is contained in:
specCon18 2023-09-02 15:11:48 -04:00
parent 3dc2c9cc5f
commit 670a380cd5
8 changed files with 136 additions and 66 deletions

21
Cargo.lock generated
View file

@ -356,7 +356,6 @@ dependencies = [
"rayon", "rayon",
"tar", "tar",
"unrar", "unrar",
"xz2",
"zip", "zip",
] ]
@ -506,17 +505,6 @@ version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "lzma-sys"
version = "0.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fda04ab3764e6cde78b9974eec4f779acaba7c4e84b36eca3cf77c581b85d27"
dependencies = [
"cc",
"libc",
"pkg-config",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.5.0" version = "2.5.0"
@ -1183,15 +1171,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "xz2"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "388c44dc09d76f1536602ead6d325eb532f5c122f17782bd57fb47baeeb767e2"
dependencies = [
"lzma-sys",
]
[[package]] [[package]]
name = "zip" name = "zip"
version = "0.6.6" version = "0.6.6"

View file

@ -12,7 +12,7 @@ crossterm = "0.27.0"
eyre = "0.6.8" eyre = "0.6.8"
indicatif = "0.17.6" indicatif = "0.17.6"
rayon = "1.7.0" rayon = "1.7.0"
#rust-lzma = "0.6.0"
tar = "0.4.40" tar = "0.4.40"
unrar = "0.5.1" unrar = "0.5.1"
xz2 = "0.1.7"
zip = "0.6.6" zip = "0.6.6"

27
flake.lock generated Normal file
View file

@ -0,0 +1,27 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1693565476,
"narHash": "sha256-ya00zHt7YbPo3ve/wNZ/6nts61xt7wK/APa6aZAfey0=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "aa8aa7e2ea35ce655297e8322dc82bf77a31d04b",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

31
flake.nix Normal file
View file

@ -0,0 +1,31 @@
{
description = "Spec's NixOS-config";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs, ... }@inputs:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in
{
devShells.${system}.rust = pkgs.mkShell{
buildInputs = with pkgs; [
cargo
cargo-watch
rustc
just
rustup
clippy
lolcat
rust-analyzer
xz # Add xz (liblzma) to the build inputs
];
# Add a shellHook to modify PKG_CONFIG_PATH
shellHook = ''
export PKG_CONFIG_PATH="${pkgs.xz}/lib/pkgconfig:$PKG_CONFIG_PATH"
'';
};
};
}

View file

@ -1,2 +0,0 @@
dev:
cargo watch -x 'run'

View file

@ -8,7 +8,6 @@ pub enum Extensions{
Txz, Txz,
Lzma, Lzma,
Gz, Gz,
Gz,
Xz, Xz,
Z, Z,
Sevenz, Sevenz,

View file

@ -1,6 +1,9 @@
use std::{fs, io, path::Path}; /*
TODO_2: implement remaining extractor functions and write tests
*/
use std::{fs, io::{self, Write}, path::Path, fs::File};
use unrar::Archive; use unrar::Archive;
// use lzma::reader::LzmaReader;
pub fn extract_zip(zip_file: &Path) -> io::Result<()> { pub fn extract_zip(zip_file: &Path) -> io::Result<()> {
let file = fs::File::open(zip_file)?; let file = fs::File::open(zip_file)?;
let mut archive = zip::ZipArchive::new(file)?; let mut archive = zip::ZipArchive::new(file)?;
@ -94,13 +97,37 @@ pub fn extract_rar(rar_file: &Path) -> Result<(), Box<dyn std::error::Error>> {
Ok(()) 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)?;
// // Create a LzmaReader to decompress the data
// let mut xz_reader = LzmaReader::new_decompressor(file)?;
// // Read the decompressed data into a buffer
// let mut decompressed_data = Vec::new();
// xz_reader.read_to_end(&mut decompressed_data)?;
// // 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 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)?;
// Ok(())
// }
pub fn extract_bz2(){} pub fn extract_bz2(){}
pub fn extract_tbz2(){} pub fn extract_tbz2(){}
pub fn extract_tgz(){} pub fn extract_tgz(){}
pub fn extract_txz(){} pub fn extract_txz(){}
pub fn extract_lzma(){} pub fn extract_lzma(){}
pub fn extract_gz(){} pub fn extract_gz(){}
pub fn extract_xz(){}
pub fn extract_z(){} pub fn extract_z(){}
pub fn extract_7z(){} pub fn extract_7z(){}
pub fn extract_arj(){} pub fn extract_arj(){}

View file

@ -1,44 +1,48 @@
/*
TODO_3: setup rayon to handle parallel file processsing when passed
more than one file and square up concurrency model for files with
more than one extension.
*/
/*
TODO_1: define supported extensions as structs and write an extensions enum
*/
mod extractors; mod extractors;
// mod extensions;
// use extenstions::{
// File,
// Extensions
// }
use std::{path::Path, fs};
use extractors::{ use extractors::{
extract_zip, extract_zip,
extract_rar, extract_rar,
extract_tar, extract_tar,
extract_bz2, // extract_xz,
extract_tbz2, // extract_bz2,
extract_tgz, // extract_tbz2,
extract_txz, // extract_tgz,
extract_lzma, // extract_txz,
extract_gz, // extract_lzma,
extract_gz, // extract_gz,
extract_xz, // extract_z,
extract_z, // extract_7z,
extract_7z, // extract_arj,
extract_arj, // extract_cab,
extract_cab, // extract_chm,
extract_chm, // extract_deb,
extract_deb, // extract_dmg,
extract_dmg, // extract_iso,
extract_iso, // extract_lzh,
extract_lzh, // extract_msi,
extract_msi, // extract_rpm,
extract_rpm, // extract_udf,
extract_udf, // extract_wim,
extract_wim, // extract_xar,
extract_xar, // extract_exe
extract_exe
}; };
fn main() { fn main() {
std::process::exit(run()); std::process::exit(run());
} }
/*
TODO: [ ] setup rayon to handle concurrent file processsing when passed
more than one file
*/
/*
TODO: [ ] Write for loop to iter over all extensions of a file to handle
files that are tared and then compressed ex: foo.tar.gz, foo.tar.gz
*/
fn run() -> i32 { fn run() -> i32 {
let args: Vec<_> = std::env::args().collect(); let args: Vec<_> = std::env::args().collect();
if args.len() < 2 { if args.len() < 2 {
@ -66,13 +70,18 @@ fn run() -> i32 {
return 1; 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" => {
if let Err(err) = extract_xz(&fname) {
println!("Error extracting XZ: {}", err);
return 1;
}
}
"gz" => { "gz" => {
if let Err(err) = extract_gz(&fname) { if let Err(err) = extract_gz(&fname) {
println!("Error extracting GZ: {}", err); println!("Error extracting GZ: {}", err);