如何用Shell脚本对比Zip文件内不同路径的文件名及内容?
Compare Nested Zip and Folder-Based Zip Contents via Shell Script
Absolutely! I've put together a shell script that handles your specific scenario—comparing filenames (and contents, when feasible) between a nested zip (A.1 inside Zip A) and a folder inside Zip B. It uses standard Unix tools so it should work on most Linux/macOS systems.
Prerequisites
Make sure these tools are installed on your system:
unzip: For extracting zip contentsdiff: For comparing file lists and contentsfind: For generating sorted file listsrm: For cleaning up temporary files
The Script
#!/bin/bash # Check if correct number of arguments are provided if [ $# -ne 4 ]; then echo "Usage: $0 <zipA> <path_to_A1_in_A> <zipB> <path_to_folder_in_B>" echo "Example: $0 archiveA.zip \"subdir/A1.zip\" archiveB.zip \"target_folder\"" exit 1 fi # Assign input arguments to variables ZIP_A="$1" A1_PATH_IN_A="$2" ZIP_B="$3" FOLDER_PATH_IN_B="$4" # Create temporary directories (unique to avoid conflicts) TMP_DIR=$(mktemp -d) A1_EXTRACT_DIR="${TMP_DIR}/a1_content" B_FOLDER_EXTRACT_DIR="${TMP_DIR}/b_folder_content" mkdir -p "${A1_EXTRACT_DIR}" "${B_FOLDER_EXTRACT_DIR}" echo "=== Starting extraction process ===" # Extract the nested zip A.1 from Zip A echo "Extracting ${A1_PATH_IN_A} from ${ZIP_A}..." unzip -q "${ZIP_A}" "${A1_PATH_IN_A}" -d "${TMP_DIR}" if [ $? -ne 0 ]; then echo "Error: Failed to extract ${A1_PATH_IN_A} from ${ZIP_A}" rm -rf "${TMP_DIR}" exit 1 fi # Extract contents of A.1 to its temp directory echo "Extracting contents of ${A1_PATH_IN_A}..." unzip -q "${TMP_DIR}/${A1_PATH_IN_A}" -d "${A1_EXTRACT_DIR}" if [ $? -ne 0 ]; then echo "Error: Failed to extract contents of ${A1_PATH_IN_A}" rm -rf "${TMP_DIR}" exit 1 fi # Extract the target folder from Zip B echo "Extracting ${FOLDER_PATH_IN_B} from ${ZIP_B}..." unzip -q "${ZIP_B}" "${FOLDER_PATH_IN_B}/\*" -d "${B_FOLDER_EXTRACT_DIR}" if [ $? -ne 0 ]; then echo "Error: Failed to extract ${FOLDER_PATH_IN_B} from ${ZIP_B}" rm -rf "${TMP_DIR}" exit 1 fi echo "=== Extraction complete. Starting comparisons ===" # 1. Compare file names (sorted list) echo -e "\n--- File Name Comparison ---" find "${A1_EXTRACT_DIR}" -type f | sed "s|${A1_EXTRACT_DIR}/||" | sort > "${TMP_DIR}/a1_files.txt" find "${B_FOLDER_EXTRACT_DIR}" -type f | sed "s|${B_FOLDER_EXTRACT_DIR}/||" | sort > "${TMP_DIR}/b_files.txt" diff "${TMP_DIR}/a1_files.txt" "${TMP_DIR}/b_files.txt" if [ $? -eq 0 ]; then echo "✅ All file names match between A.1 and B's target folder" else echo "❌ File name differences found (see above)" fi # 2. Compare file contents (only if file names match) echo -e "\n--- File Content Comparison ---" if cmp -s "${TMP_DIR}/a1_files.txt" "${TMP_DIR}/b_files.txt"; then diff -r "${A1_EXTRACT_DIR}" "${B_FOLDER_EXTRACT_DIR}" --exclude-dir="." --exclude-dir=".." if [ $? -eq 0 ]; then echo "✅ All file contents match between A.1 and B's target folder" else echo "❌ File content differences found (see above)" fi else echo "⚠️ Skipping content comparison since file names don't match" fi # Clean up temporary files echo -e "\n=== Cleaning up temporary files ===" rm -rf "${TMP_DIR}" echo "Done!"
How to Use
- Save the script as
zip_compare.sh - Make it executable:
chmod +x zip_compare.sh - Run it with your specific paths:
./zip_compare.sh archiveA.zip "path/to/A1.zip" archiveB.zip "path/to/target_folder"- Replace the paths with the actual locations of A.1 inside Zip A and the target folder inside Zip B. If paths contain spaces, wrap them in quotes.
Key Features
- Handles nested zips: Extracts the inner A.1 zip from Zip A automatically
- Targeted extraction: Only pulls the needed folder from Zip B, not the entire archive
- Two-level comparison: First checks file names, then compares contents if names match
- Cleanup: Automatically deletes temporary files after completion
- Error handling: Checks for extraction failures and exits with meaningful messages
Notes
- For binary files,
diffmight not be the best tool—you can replace the content comparison line withdiff -r --binaryor usecmpin a loop if you need stricter binary checks. - If dealing with very large archives, ensure your temporary directory has enough disk space (the script uses the system's default temp location, usually
/tmp). - If your zip files have password protection, add the
-P <password>flag to theunzipcommands (note: this is insecure as the password will be visible in process lists—use-pinstead if you want to enter it interactively).
内容的提问来源于stack exchange,提问作者G.C
相关产品推荐
相关产品推荐

