File size: 11,988 Bytes
8a11391 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | #!/bin/bash
# Script to automatically extract all compressed files in the dataset directories
# Handles: .zip, .zip.zip, split .z01/.z02, split .001/.002, .tar.gz, split .tar.gz.00/.01, etc.
# Don't exit on error - we want to continue processing even if some extractions fail
# Configuration
SEVENZ="/ocean/projects/cis220031p/shan1/7z/7zz"
BASE_DIR="/ocean/projects/cis220031p/shan1/data_ck/deepfake_dataset/compress/test"
LOG_FILE="${BASE_DIR}/extraction.log"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging function
log() {
echo -e "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
log_error() {
echo -e "${RED}$(date '+%Y-%m-%d %H:%M:%S') - ERROR: $1${NC}" | tee -a "$LOG_FILE"
}
log_success() {
echo -e "${GREEN}$(date '+%Y-%m-%d %H:%M:%S') - SUCCESS: $1${NC}" | tee -a "$LOG_FILE"
}
log_warning() {
echo -e "${YELLOW}$(date '+%Y-%m-%d %H:%M:%S') - WARNING: $1${NC}" | tee -a "$LOG_FILE"
}
log_info() {
echo -e "${BLUE}$(date '+%Y-%m-%d %H:%M:%S') - INFO: $1${NC}" | tee -a "$LOG_FILE"
}
# Check if 7zz exists
if [ ! -f "$SEVENZ" ]; then
log_error "7zz not found at $SEVENZ"
exit 1
fi
# Make 7zz executable
chmod +x "$SEVENZ" 2>/dev/null || true
log_info "Starting extraction process..."
log_info "Using 7zz at: $SEVENZ"
log_info "Base directory: $BASE_DIR"
cd "$BASE_DIR" || exit 1
# Track processed files to avoid duplicate processing
declare -A processed_files
# Function to check if file is a split archive part
is_split_part() {
local file="$1"
# Check for .z01, .z02, etc.
if [[ "$file" =~ \.(z[0-9]{2})$ ]]; then
return 0
fi
# Check for .001, .002, etc.
if [[ "$file" =~ \.([0-9]{3})$ ]]; then
return 0
fi
# Check for .00, .01, etc. (for tar.gz splits)
if [[ "$file" =~ \.([0-9]{2})$ ]]; then
return 0
fi
return 1
}
# Function to extract split zip files (.z01, .z02, etc.)
extract_split_zip() {
local dir="$1"
local base_name="$2"
local first_file="${dir}/${base_name}.z01"
local zip_file="${dir}/${base_name}.zip"
log_info "Extracting split zip: ${base_name}.zip (.z01-.zXX format)"
if [ -f "$zip_file" ]; then
# Use the .zip file to extract (it contains the archive info)
if "$SEVENZ" x "$zip_file" -o"${dir}/" -y >/dev/null 2>&1; then
log_success "Extracted split zip: ${base_name}.zip"
return 0
else
log_error "Failed to extract split zip: ${base_name}.zip"
return 1
fi
else
log_warning "Main zip file not found for split archive: ${base_name}"
return 1
fi
}
# Function to extract split zip files (.001, .002, etc. or .zip.001, .zip.002)
extract_split_zip_001() {
local dir="$1"
local base_name="$2"
local first_file=""
# Check if it's .zip.001 format
if [ -f "${dir}/${base_name}.zip.001" ]; then
first_file="${dir}/${base_name}.zip.001"
log_info "Extracting split zip: ${base_name}.zip.001 (.zip.001-.zip.XXX format)"
# Check if it's just .001 format (without .zip prefix)
elif [ -f "${dir}/${base_name}.001" ]; then
first_file="${dir}/${base_name}.001"
log_info "Extracting split zip: ${base_name}.001 (.001-.XXX format)"
else
log_warning "First split file not found for: ${base_name}"
return 1
fi
if "$SEVENZ" x "$first_file" -o"${dir}/" -y >/dev/null 2>&1; then
log_success "Extracted split zip: $(basename "$first_file")"
return 0
else
log_error "Failed to extract split zip: $(basename "$first_file")"
return 1
fi
}
# Function to combine and extract split tar.gz files
extract_split_targz() {
local dir="$1"
local base_name="$2"
local output_file="${dir}/${base_name}.tar.gz"
log_info "Combining and extracting split tar.gz: ${base_name}.tar.gz.00-.XX"
# Find all split parts (format: base_name.tar.gz.00, .01, etc.)
local parts=()
local part_num=0
while [ -f "${dir}/${base_name}.tar.gz.$(printf "%02d" $part_num)" ]; do
parts+=("${dir}/${base_name}.tar.gz.$(printf "%02d" $part_num)")
part_num=$((part_num + 1))
done
if [ ${#parts[@]} -eq 0 ]; then
log_warning "No split parts found for: ${base_name}"
return 1
fi
log_info "Found ${#parts[@]} split parts for ${base_name}.tar.gz"
# Combine split parts
log_info "Combining split parts into ${base_name}.tar.gz..."
cat "${parts[@]}" > "$output_file" 2>/dev/null
if [ $? -eq 0 ] && [ -f "$output_file" ]; then
log_success "Combined split parts into ${base_name}.tar.gz"
# Extract the combined tar.gz
log_info "Extracting ${base_name}.tar.gz..."
if tar -xzf "$output_file" -C "$dir" 2>/dev/null; then
log_success "Extracted ${base_name}.tar.gz"
# Optionally remove the combined file to save space
# rm "$output_file"
return 0
else
log_error "Failed to extract ${base_name}.tar.gz"
return 1
fi
else
log_error "Failed to combine split parts for ${base_name}.tar.gz"
return 1
fi
}
# Function to extract double zip files (.zip.zip)
extract_double_zip() {
local file="$1"
local dir=$(dirname "$file")
local filename=$(basename "$file")
local temp_zip="${dir}/${filename%.zip}"
log_warning "Found double zip file: ${filename} - will extract twice"
# First extraction: .zip.zip -> .zip
log_info "First extraction: ${filename} -> ${filename%.zip}"
if "$SEVENZ" x "$file" -o"${dir}/" -y >/dev/null 2>&1; then
log_success "First extraction completed: ${filename}"
# Second extraction: .zip -> contents
if [ -f "$temp_zip" ]; then
log_info "Second extraction: $(basename "$temp_zip") -> contents"
if "$SEVENZ" x "$temp_zip" -o"${dir}/" -y >/dev/null 2>&1; then
log_success "Second extraction completed: $(basename "$temp_zip")"
# Remove intermediate .zip file
rm -f "$temp_zip"
return 0
else
log_error "Second extraction failed: $(basename "$temp_zip")"
return 1
fi
else
log_warning "Intermediate zip file not found after first extraction"
return 1
fi
else
log_error "First extraction failed: ${filename}"
return 1
fi
}
# Function to extract regular zip file
extract_zip() {
local file="$1"
local dir=$(dirname "$file")
log_info "Extracting zip: $(basename "$file")"
if "$SEVENZ" x "$file" -o"${dir}/" -y >/dev/null 2>&1; then
log_success "Extracted: $(basename "$file")"
return 0
else
log_error "Failed to extract: $(basename "$file")"
return 1
fi
}
# Function to extract regular tar.gz file
extract_targz() {
local file="$1"
local dir=$(dirname "$file")
log_info "Extracting tar.gz: $(basename "$file")"
if tar -xzf "$file" -C "$dir" 2>/dev/null; then
log_success "Extracted: $(basename "$file")"
return 0
else
log_error "Failed to extract: $(basename "$file")"
return 1
fi
}
# Process each directory
process_directory() {
local dir="$1"
log_info "Processing directory: $(basename "$dir")"
cd "$dir" || return 1
# Track which archives we've already processed
declare -A processed_archives
# Find all archive files
while IFS= read -r file; do
local filename=$(basename "$file")
local filepath="$file"
# Skip if already processed
if [ -n "${processed_files[$filepath]}" ]; then
continue
fi
# Skip label files and other non-archive files
if [[ "$filename" =~ \.(txt|pdf|LICENSE)$ ]]; then
continue
fi
# Handle double zip files (.zip.zip)
if [[ "$filename" =~ \.zip\.zip$ ]]; then
processed_files["$filepath"]=1
extract_double_zip "$filepath"
continue
fi
# Handle split tar.gz files with .00, .01 format (process first)
if [[ "$filename" =~ \.tar\.gz\.([0-9]{2})$ ]]; then
local base_name="${filename%.tar.gz.*}"
if [ -z "${processed_archives["${base_name}_targz"]}" ]; then
processed_archives["${base_name}_targz"]=1
extract_split_targz "$dir" "$base_name"
fi
continue
fi
# Handle split zip files with .z01, .z02 format
# Process the main .zip file when we encounter it
if [[ "$filename" =~ \.zip$ ]]; then
local base_name="${filename%.zip}"
# Check if this is part of a split archive with .z01, .z02 format
if [ -f "${dir}/${base_name}.z01" ]; then
if [ -z "${processed_archives["${base_name}_z01"]}" ]; then
processed_archives["${base_name}_z01"]=1
extract_split_zip "$dir" "$base_name"
fi
continue
fi
fi
# Handle split zip files with .zip.001, .zip.002 format
if [[ "$filename" =~ \.zip\.([0-9]{3})$ ]]; then
local base_name="${filename%.zip.*}"
if [ -z "${processed_archives["${base_name}_zip001"]}" ]; then
processed_archives["${base_name}_zip001"]=1
extract_split_zip_001 "$dir" "$base_name"
fi
continue
fi
# Handle split zip files with .001, .002 format (not .zip.001)
if [[ "$filename" =~ \.([0-9]{3})$ ]] && [[ ! "$filename" =~ \.zip\.([0-9]{3})$ ]]; then
local base_name="${filename%.*}"
if [ -z "${processed_archives["${base_name}_001"]}" ]; then
processed_archives["${base_name}_001"]=1
extract_split_zip_001 "$dir" "$base_name"
fi
continue
fi
# Handle split zip parts with .z01, .z02 (skip, already processed via main .zip)
if [[ "$filename" =~ \.(z[0-9]{2})$ ]]; then
continue
fi
# Handle regular zip files
if [[ "$filename" =~ \.zip$ ]]; then
# Already checked for split archives above, so this is a regular zip
processed_files["$filepath"]=1
extract_zip "$filepath"
continue
fi
# Handle regular tar.gz files (but skip if it's part of a split archive)
if [[ "$filename" =~ \.tar\.gz$ ]]; then
# Check if this is part of a split tar.gz
local base_name="${filename%.tar.gz}"
if [ -f "${dir}/${base_name}.tar.gz.00" ]; then
continue
fi
processed_files["$filepath"]=1
extract_targz "$filepath"
continue
fi
done < <(find . -maxdepth 1 -type f | sort)
cd "$BASE_DIR" || exit 1
}
# Main execution
main() {
log_info "=== Starting Extraction Process ==="
# Create log file
touch "$LOG_FILE"
# Process each dataset directory
for dir in "$BASE_DIR"/*/; do
if [ -d "$dir" ]; then
process_directory "$dir"
fi
done
log_info "=== Extraction Process Complete ==="
log_info "Log file saved at: $LOG_FILE"
# Summary
echo ""
echo -e "${GREEN}=== Extraction Summary ===${NC}"
echo -e "Success logs: $(grep -c "SUCCESS" "$LOG_FILE" 2>/dev/null || echo 0)"
echo -e "Error logs: $(grep -c "ERROR" "$LOG_FILE" 2>/dev/null || echo 0)"
echo -e "Warning logs: $(grep -c "WARNING" "$LOG_FILE" 2>/dev/null || echo 0)"
}
# Run main function
main
|