|
| 1 | +#!/usr/bin/env bash |
| 2 | +# unpigz wrapper script for crabz |
| 3 | +# This script mimics unpigz behavior using crabz for parallel decompression |
| 4 | + |
| 5 | +# Initialize variables |
| 6 | +KEEP_ORIGINAL=false |
| 7 | +OUTPUT_FILE="" |
| 8 | +FORCE=false |
| 9 | +TEST=false |
| 10 | +LIST=false |
| 11 | +DECOMPRESS=true |
| 12 | +STDOUT=false |
| 13 | +VERBOSE=false |
| 14 | +QUIET=false |
| 15 | +INPUT_FILES=() |
| 16 | + |
| 17 | +# Parse command line arguments |
| 18 | +while [ $# -gt 0 ]; do |
| 19 | + case "$1" in |
| 20 | + -c | --stdout) |
| 21 | + STDOUT=true |
| 22 | + shift |
| 23 | + ;; |
| 24 | + -d | --decompress) |
| 25 | + DECOMPRESS=true |
| 26 | + shift |
| 27 | + ;; |
| 28 | + -k | --keep) |
| 29 | + KEEP_ORIGINAL=true |
| 30 | + shift |
| 31 | + ;; |
| 32 | + -f | --force) |
| 33 | + FORCE=true |
| 34 | + shift |
| 35 | + ;; |
| 36 | + -t | --test) |
| 37 | + TEST=true |
| 38 | + shift |
| 39 | + ;; |
| 40 | + -l | --list) |
| 41 | + LIST=true |
| 42 | + shift |
| 43 | + ;; |
| 44 | + -v | --verbose) |
| 45 | + VERBOSE=true |
| 46 | + shift |
| 47 | + ;; |
| 48 | + -q | --quiet) |
| 49 | + QUIET=true |
| 50 | + shift |
| 51 | + ;; |
| 52 | + -h | --help) |
| 53 | + cat <<EOF |
| 54 | +unpigz wrapper for crabz - decompress gzip files in parallel |
| 55 | +
|
| 56 | +Usage: unpigz [options] [file ...] |
| 57 | +
|
| 58 | +Options: |
| 59 | + -c, --stdout Write output to stdout |
| 60 | + -d, --decompress Decompress (default) |
| 61 | + -k, --keep Keep original file |
| 62 | + -f, --force Force overwrite of output file |
| 63 | + -t, --test Test compressed file integrity |
| 64 | + -l, --list List compressed file contents |
| 65 | + -v, --verbose Verbose mode |
| 66 | + -q, --quiet Quiet mode |
| 67 | + -h, --help Display this help message |
| 68 | +
|
| 69 | +This is a wrapper that uses crabz for parallel decompression. |
| 70 | +EOF |
| 71 | + exit 0 |
| 72 | + ;; |
| 73 | + -*) |
| 74 | + echo "Warning: Unrecognized option $1, ignoring" >&2 |
| 75 | + shift |
| 76 | + ;; |
| 77 | + *) |
| 78 | + INPUT_FILES+=("$1") |
| 79 | + shift |
| 80 | + ;; |
| 81 | + esac |
| 82 | +done |
| 83 | + |
| 84 | +# Build crabz command options |
| 85 | +CRABZ_OPTS="-d" # Always decompress |
| 86 | +[ "$QUIET" = "true" ] && CRABZ_OPTS="$CRABZ_OPTS -Q" |
| 87 | + |
| 88 | +# If no input files specified, use stdin |
| 89 | +if [ ${#INPUT_FILES[@]} -eq 0 ]; then |
| 90 | + if [ "$STDOUT" = "false" ]; then |
| 91 | + STDOUT=true |
| 92 | + fi |
| 93 | + # Read from stdin and output to stdout |
| 94 | + crabz $CRABZ_OPTS -o "-" "-" |
| 95 | + exit $? |
| 96 | +fi |
| 97 | + |
| 98 | +# Process each input file |
| 99 | +for INPUT_FILE in "${INPUT_FILES[@]}"; do |
| 100 | + # Check if input file exists |
| 101 | + if [ ! -f "$INPUT_FILE" ] && [ ! -p "$INPUT_FILE" ]; then |
| 102 | + echo "unpigz: $INPUT_FILE: No such file or directory" >&2 |
| 103 | + continue |
| 104 | + fi |
| 105 | + |
| 106 | + # Handle test mode |
| 107 | + if [ "$TEST" = "true" ]; then |
| 108 | + if [ "$QUIET" = "false" ]; then |
| 109 | + echo "Testing $INPUT_FILE..." >&2 |
| 110 | + fi |
| 111 | + crabz $CRABZ_OPTS -o "/dev/null" "$INPUT_FILE" >/dev/null 2>&1 |
| 112 | + if [ $? -eq 0 ]; then |
| 113 | + [ "$VERBOSE" = "true" ] && echo "$INPUT_FILE: OK" >&2 |
| 114 | + else |
| 115 | + echo "$INPUT_FILE: ERROR" >&2 |
| 116 | + exit 1 |
| 117 | + fi |
| 118 | + continue |
| 119 | + fi |
| 120 | + |
| 121 | + # Handle list mode |
| 122 | + if [ "$LIST" = "true" ]; then |
| 123 | + # crabz doesn't have a list mode, so we just show basic info |
| 124 | + if [ -f "$INPUT_FILE" ]; then |
| 125 | + SIZE=$(stat -c%s "$INPUT_FILE" 2>/dev/null || stat -f%z "$INPUT_FILE" 2>/dev/null) |
| 126 | + echo "$INPUT_FILE: compressed size: $SIZE bytes" |
| 127 | + fi |
| 128 | + continue |
| 129 | + fi |
| 130 | + |
| 131 | + # Determine output file |
| 132 | + if [ "$STDOUT" = "true" ]; then |
| 133 | + # Output to stdout |
| 134 | + crabz $CRABZ_OPTS -o "-" "$INPUT_FILE" |
| 135 | + else |
| 136 | + # Determine output filename |
| 137 | + case "$INPUT_FILE" in |
| 138 | + *.gz) |
| 139 | + OUTPUT_FILE="${INPUT_FILE%.gz}" |
| 140 | + ;; |
| 141 | + *.tgz) |
| 142 | + OUTPUT_FILE="${INPUT_FILE%.tgz}.tar" |
| 143 | + ;; |
| 144 | + *.z) |
| 145 | + OUTPUT_FILE="${INPUT_FILE%.z}" |
| 146 | + ;; |
| 147 | + *) |
| 148 | + OUTPUT_FILE="${INPUT_FILE}.out" |
| 149 | + ;; |
| 150 | + esac |
| 151 | + |
| 152 | + # Check if output file exists and handle force flag |
| 153 | + if [ -f "$OUTPUT_FILE" ] && [ "$FORCE" = "false" ]; then |
| 154 | + echo "unpigz: $OUTPUT_FILE already exists; not overwritten" >&2 |
| 155 | + continue |
| 156 | + fi |
| 157 | + |
| 158 | + # Decompress to output file |
| 159 | + if [ "$VERBOSE" = "true" ]; then |
| 160 | + echo "Decompressing $INPUT_FILE to $OUTPUT_FILE..." >&2 |
| 161 | + fi |
| 162 | + |
| 163 | + crabz $CRABZ_OPTS -o "$OUTPUT_FILE" "$INPUT_FILE" |
| 164 | + RESULT=$? |
| 165 | + |
| 166 | + if [ $RESULT -eq 0 ]; then |
| 167 | + # Preserve timestamps if possible |
| 168 | + if [ -f "$INPUT_FILE" ]; then |
| 169 | + touch -r "$INPUT_FILE" "$OUTPUT_FILE" 2>/dev/null |
| 170 | + fi |
| 171 | + |
| 172 | + # Remove original file unless -k flag is set |
| 173 | + if [ "$KEEP_ORIGINAL" = "false" ] && [ -f "$INPUT_FILE" ]; then |
| 174 | + rm -f "$INPUT_FILE" |
| 175 | + fi |
| 176 | + else |
| 177 | + # On error, remove partial output file |
| 178 | + rm -f "$OUTPUT_FILE" |
| 179 | + echo "unpigz: Error decompressing $INPUT_FILE" >&2 |
| 180 | + exit 1 |
| 181 | + fi |
| 182 | + fi |
| 183 | +done |
| 184 | + |
| 185 | +exit 0 |
0 commit comments