wget fails to mirror .zip files when the server returns HTML error pages instead of binary data, often due to missing headers, strict access controls, or incorrect URL patterns. To fix this, you must inspect the actual response content, adjust your command with appropriate flags like --header and --trust-server-names, and verify that the mirror depth matches the file location.
Diagnosing the Response Content
The most common reason wget appears to "fail" while mirroring is that it successfully downloads a file, but that file is not the expected binary archive. Instead, it is an HTML page containing an error message, such as "403 Forbidden," "Access Denied," or a login form. Because wget treats the downloaded content as a generic file, it may save it asindex.html or name it based on the URL path, leading you to believe the .zip file was mirrored when it was actually rejected by the server.
To diagnose this issue, you should first test the download manually without mirroring. Use the following command to download a single known .zip file and inspect its size and type:
wget -O test.zip https://example.com/path/to/file.zip
file test.zip
If the output of the file command indicates that the result is an HTML document or text file rather than a ZIP archive, the server is blocking the request or redirecting it. This confirms that the issue is not with wget's ability to handle binary data, but with the HTTP response received from the target website.
Adjusting User-Agent and Headers
Many modern websites employ bot protection mechanisms that block requests lacking standard browser identifiers. By default, wget identifies itself as "Wget/1.21" (or similar), which some security filters flag as suspicious traffic. If the server rejects this identifier, it may return an HTML error page instead of the requested resource. You can simulate a legitimate browser request by adding a customUser-Agent header to your wget command. For example:
wget --user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" \
--mirror https://example.com/downloads
Additionally, if the website requires specific cookies or authentication tokens for downloading large archives, you must include them using the --header option. You can extract these headers from your browser's developer tools network tab and pass them directly to wget. This step is crucial when dealing with protected repositories where direct anonymous access is disabled for bulk downloads.
Refining Mirror Parameters
The--mirror flag in wget is a shorthand for several other options, including -r (recursive), -N (timestamping), -l inf (infinite recursion depth), and --page-requisites. The infinite recursion depth can cause wget to follow links into directories that do not contain the desired .zip files, potentially hitting rate limits or triggering anti-bot measures.
If you know the specific directory structure where the .zip files reside, limit the recursion depth explicitly. For instance, if the files are only two levels deep, use -l 2 instead of relying on the default infinite depth. This reduces noise and prevents wget from wasting resources on irrelevant pages.
Furthermore, ensure that wget is correctly interpreting the URLs. Some servers serve files via dynamic routes that require specific query parameters. Using --trust-server-names can help wget preserve the original filename provided by the server, which is particularly useful when the URL path does not match the actual file name.
wget --mirror --level=2 --trust-server-names \
--user-agent="Mozilla/5.0" https://example.com/archive
For more complex scenarios involving multiple domains or subdirectories, consider reviewing related troubleshooting guides for similar command-line tools. See WSL wget Command Downloads HTML Instead of CSV for insights on handling unexpected content types during downloads.
Handling Authentication and Redirects
If the website requires login credentials, wget cannot automatically fill out forms. You must either pre-authenticate and save the session cookies or use the--http-user and --http-password flags if basic authentication is supported. Note that many modern sites use OAuth or token-based auth, which wget does not support natively. In such cases, manual extraction of valid session cookies is required.
Redirects can also interfere with mirroring. If the initial URL redirects to a login page, wget may follow the redirect and download the HTML login form instead of the .zip file. Use the --max-redirects option to limit how many redirects wget follows, preventing it from getting stuck in authentication loops.
wget --max-redirect=3 --mirror https://example.com/files
By combining these adjustments—inspecting responses, spoofing user agents, limiting recursion, and managing sessions—you can resolve most issues related to wget failing to mirror binary archives. Always verify the downloaded files using the file command to ensure integrity before proceeding with further automation.
Frequently Asked Questions
Why does wget save my .zip file as index.html?
This happens when the server returns an HTML error page instead of the binary file, often due to a 403 Forbidden status. Wget saves whatever content it receives, so if the response is HTML, it will be saved with the default name derived from the URL path.Can wget bypass Cloudflare or other WAF protections?
No, wget cannot bypass advanced Web Application Firewalls that require JavaScript execution or CAPTCHA solving. However, mimicking a real browser user-agent and respecting rate limits may reduce the likelihood of being blocked by simpler filtering rules.How do I specify a password for FTP mirrors?
You can provide credentials directly in the URL using the formatftp://username:password@host/path, or use the --ftp-user and --ftp-password flags. Ensure the password is properly encoded if it contains special characters.
What if the .zip files are split across multiple pages?
Use the--level option to control recursion depth, ensuring wget explores enough directories to find all parts. You may also need to adjust --accept to specifically target .zip extensions, ignoring other file types to speed up the process.
Is there a way to resume interrupted .zip downloads?
Yes, wget supports resuming downloads with the-c flag. This is useful if the connection drops during a large mirror operation, allowing you to restart from where it left off without re-downloading completed chunks.