dfcdata / script /extract_all.sh
shuohann's picture
Add files using upload-large-folder tool
8a11391 verified
Raw History Blame Contribute Delete
12 kB
#!/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