如何解决wget镜像网站在Windows7的255字符路径限制问题?
Let's tackle this Windows 7 path length headache step by step— I've dealt with similar offline mirroring issues for legacy Windows systems, so here are actionable command-line solutions you can run directly on Debian:
First, let's refine your wget command to prevent overly deep directories in the first place. Your earlier -nH --cut-dirs=5 attempt failed because it flattened too much structure, causing index.html overlaps. Here's a balanced approach:
wget --mirror \ --adjust-extension \ --convert-links \ --restrict-file-names=windows \ --page-requisites \ -e robots=off \ --no-clobber \ --no-parent \ --base=./ \ -nH \ --cut-dirs=3 \ # Tweak this number based on the site's URL structure— start with 3 and test --directory-prefix=landesmuseum-mirror \ # Contains all files in a single root dir to avoid overwrites --no-if-modified-since \ https://www.landesmuseum-mecklenburg.de/
--cut-dirs=3: Removes the first 3 directory levels from the URL (e.g.,www.landesmuseum-mecklenburg.de/de/exhibitions/long/path/becomesexhibitions/long/path/). Adjust until your deepest paths stay under 200 characters (leaves buffer for Windows' 255 limit).- For your extra resources in
imagelist.txt, use this matching command to maintain the same structure:
wget -c -x -i imagelist.txt -P landesmuseum-mirror/ --restrict-file-names=windows
If you already have the mirrored files and don't want to re-download, use this two-step process to shorten paths with MD5 hashes and fix HTML links automatically:
Step 1: Rename long-path files to MD5 hashes
Save this as rename-long-files.sh, make it executable, and run it to generate hashed filenames and log path changes:
#!/bin/bash LOG_FILE="path-renames.log" OUTPUT_DIR="landesmuseum-mirror" MAX_PATH_LENGTH=200 # Find files with paths exceeding the limit, exclude directories find "$OUTPUT_DIR" -type f -printf "%p\n" | while read -r FILE; do PATH_LENGTH=${#FILE} if [ "$PATH_LENGTH" -gt "$MAX_PATH_LENGTH" ]; then # Generate MD5 hash of the file's full path HASH=$(echo -n "$FILE" | md5sum | awk '{print $1}') # Keep the original file extension EXT="${FILE##*.}" NEW_FILE="$OUTPUT_DIR/$HASH.$EXT" # Rename and log the change mv "$FILE" "$NEW_FILE" echo "$FILE|$NEW_FILE" >> "$LOG_FILE" echo "Renamed: $FILE -> $NEW_FILE" fi done
Execute with:
chmod +x rename-long-files.sh ./rename-long-files.sh
Step 2: Update HTML links to match new hashed paths
Save this as update-links.sh, make it executable, and run to batch replace old paths in all HTML files:
#!/bin/bash LOG_FILE="path-renames.log" OUTPUT_DIR="landesmuseum-mirror" # Read each log entry and replace old relative paths with new ones while IFS="|" read -r OLD_PATH NEW_PATH; do OLD_REL=$(echo "$OLD_PATH" | sed "s|$OUTPUT_DIR/||") NEW_REL=$(echo "$NEW_PATH" | sed "s|$OUTPUT_DIR/||") # Replace in all HTML files find "$OUTPUT_DIR" -name "*.html" -exec sed -i "s|$OLD_REL|$NEW_REL|g" {} \; done < "$LOG_FILE"
Execute with:
chmod +x update-links.sh ./update-links.sh
If you prefer DOS-style short names (e.g., longdirname → longdi~1), note this is less portable but works for Windows-only use:
Step 1: Generate DOS short names
First install winepath with sudo apt install wine, then run:
find landesmuseum-mirror -type d -o -type f | while read -r PATH; do SHORT_PATH=$(winepath -w "$PATH" | sed 's|\\\\|/|g' | awk -F/ '{for(i=1;i<=NF;i++){if(length($i)>8){sub(/^(.{6}).*/,"\\1~1",$i)}};print "/" $0}') echo "$PATH|$SHORT_PATH" >> dos-short-names.log done
Step 2: Update HTML links
Use a similar sed script as Solution 2 to replace full paths with short paths in all HTML files.
After running any of these solutions, transfer the landesmuseum-mirror directory to your Windows 7 machine and test with MiniWebserver— the long path errors should be resolved.
内容的提问来源于stack exchange,提问作者Raphael

