Skip to content

Commit

Permalink
Add zip support and bump release to 0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
harmless-tech committed Feb 25, 2024
1 parent 2d7bcec commit 812fbe3
Show file tree
Hide file tree
Showing 9 changed files with 674 additions and 11 deletions.
577 changes: 576 additions & 1 deletion Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "qstract"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
authors = ["harmless-tech"]
description = "Extract tars"
description = "A basic tar/zip extraction program"
readme = "README.md"
license = "MIT"
repository = "https://github.com/cargo-prebuilt/qstract"
keywords = ["binary", "tar"]
rust-version = "1.63"
rust-version = "1.74"
include = [
"src/",
"build.rs",
Expand All @@ -24,6 +24,7 @@ include = [
anyhow = "1.0.80"
flate2 = "1.0.28"
pico-args = { version = "0.5.0", features = ["eq-separator"] }
rc-zip-sync = { version = "4.0.0", default-features = false, features = ["deflate", "deflate64"] }
tar = "0.4.40"

[profile.release]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Rust Checks](https://github.com/cargo-prebuilt/qstract/actions/workflows/checks.yml/badge.svg?event=push)](https://github.com/cargo-prebuilt/qstract/actions/workflows/checks.yml)
[![rustc-msrv](https://img.shields.io/badge/rustc-1.63%2B-blue?logo=rust)](https://www.rust-lang.org/tools/install)

A very simple tar extraction program.
A basic tar/zip extraction program.

## Installation

Expand All @@ -13,14 +13,14 @@ A very simple tar extraction program.
<!-- - Cargo prebuilt: ```cargo prebuilt qstract``` -->
- Cargo binstall: ```cargo binstall qstract --no-confirm```
- Cargo quickinstall: ```cargo quickinstall qstract```
- Homebrew: ```brew install crow-rest/harmless/qstract```
- Install script (unix platforms): ```curl --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-prebuilt/qstract/main/scripts/install-qstract.sh | bash```
<!-- - For github actions you can use [cargo-prebuilt/cargo-prebuilt-action](https://github.com/cargo-prebuilt/cargo-prebuilt-action) -->

## Args

- `-C` for directory to extract to.
- `-z` for gzip.
- `--zip` for zip. (Only deflate and deflate64)

First positional arg is the file to extract.

Expand Down
2 changes: 2 additions & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ allow = [
"MIT",
"Apache-2.0",
#"Apache-2.0 WITH LLVM-exception",
"Unicode-DFS-2016",
"BSD-3-Clause"
]
# The confidence threshold for detecting a license from license text.
# The higher the value, the more closely the license text must be to the
Expand Down
53 changes: 48 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use flate2::bufread::GzDecoder;
use rc_zip_sync::{rc_zip::parse::EntryKind, ReadZip};
use std::{
env,
fs::File,
io::{BufReader, Read},
path::PathBuf,
Expand All @@ -20,13 +20,15 @@ FLAGS:
OPTIONS:
-z Extract gzip compressed file
-C [DIR] Extract to [DIR]
--zip Extract zip compressed file (none, deflate, deflate64)
ARGS:
<FILE> Tar file to extract
";

struct Args {
gzip: bool,
zip: bool,
output: PathBuf,
input: PathBuf,
}
Expand All @@ -46,25 +48,66 @@ fn main() -> anyhow::Result<()> {

let args = Args {
gzip: pargs.contains("-z"),
zip: pargs.contains("--zip"),
output: if let Some(p) =
pargs.opt_value_from_os_str("-C", |s| Ok::<PathBuf, String>(PathBuf::from(s)))?
{
p
}
else {
env::current_dir()?
std::env::current_dir()?
},
input: pargs.free_from_os_str(|s| Ok::<PathBuf, String>(PathBuf::from(s)))?,
};

if args.gzip && args.zip {
panic!("Cannot use gzip and zip at the same time.");
}

let file = File::open(args.input)?;
let file = BufReader::new(file);

let file: Box<dyn Read> =
let mut file: Box<dyn Read> =
if args.gzip { Box::new(GzDecoder::new(file)) } else { Box::new(file) };

let mut archive = Archive::new(file);
archive.unpack(args.output)?;
if !args.zip {
let mut archive = Archive::new(file);
archive.unpack(args.output)?;
}
else {
unzip(&mut file, args.output)?;
}

Ok(())
}

fn unzip(read: &mut Box<dyn Read>, output: PathBuf) -> anyhow::Result<()> {
let mut bytes = Vec::new();
read.read_to_end(&mut bytes)?;
let reader = bytes.read_zip()?;

for entry in reader.entries() {
let name = match entry.sanitized_name() {
Some(name) => name,
None => continue,
};

match entry.kind() {
EntryKind::Directory => {
let path = output.join(name);
std::fs::create_dir_all(path.parent().expect("No parent path."))?;
}
EntryKind::File => {
let path = output.join(name);
std::fs::create_dir_all(path.parent().expect("No parent path."))?;

let mut w = File::create(path)?;
let mut r = entry.reader();
std::io::copy(&mut r, &mut w)?;
}
EntryKind::Symlink => eprintln!("Unsupported symlink, skipping {}", name),
}
}

Ok(())
}
Binary file added test/7z-zarchive1.zip
Binary file not shown.
42 changes: 42 additions & 0 deletions test/_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,53 @@ function test_2 {
function test_3 {
$QSTRACT_BIN t3archive.tar
b101_files ./ff
rm -rf ./ff
}

function test_4 {
$QSTRACT_BIN --zip ./zarchive1.zip -C ./t1
10_files ./t1
rm -rf ./t1

$QSTRACT_BIN ./zarchive1.zip -C ./ --zip
10_files ./
rm -rf *.txt

$QSTRACT_BIN -C ./t555/111 --zip ./zarchive1.zip
10_files ./t555/111
rm -rf ./t555
}

function test_5 {
$QSTRACT_BIN --zip ./7z-zarchive1.zip -C ./t1
10_files ./t1
rm -rf ./t1

$QSTRACT_BIN ./7z-zarchive1.zip -C ./ --zip
10_files ./
rm -rf *.txt

$QSTRACT_BIN -C ./t555/111 --zip ./7z-zarchive1.zip
10_files ./t555/111
rm -rf ./t555
}

function test_6 {
$QSTRACT_BIN --zip nocomp1.zip
b101_files ./ff
rm -rf ./ff

$QSTRACT_BIN --zip nocomp1.zip -C ./ttf
b101_files ./ttf/ff
rm -rf ./ttf/ff
}

test_1
test_2
test_3
test_4
test_5
test_6

popd
rm -rf "$TEMP_DIR"
Binary file added test/nocomp1.zip
Binary file not shown.
Binary file added test/zarchive1.zip
Binary file not shown.

0 comments on commit 812fbe3

Please sign in to comment.