Use PHP ZipArchive instead of external zip command#122
Draft
Conversation
…upport Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix 'zip' command not recognized error on Windows 10
Use PHP ZipArchive instead of external zip command
Feb 25, 2026
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
…ip_archive() Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request replaces the external zip command with PHP's native ZipArchive class to create ZIP archives, improving cross-platform compatibility, particularly for Windows systems where the zip command may not be available.
Changes:
- Replaced shell command-based ZIP creation with native PHP
ZipArchiveimplementation - Added
ext-zipdependency to composer.json - Refactored
__invoke()method to branch between ZIP (using ZipArchive) and tar.gz (still using shell command) formats
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Dist_Archive_Command.php | Refactored ZIP archive creation to use ZipArchive; added create_zip_archive() and add_zip_entry() helper methods; restructured control flow to handle ZIP and tar.gz formats separately |
| composer.json | Added ext-zip extension requirement to ensure PHP ZIP support is available |
Comments suppressed due to low confidence (4)
src/Dist_Archive_Command.php:488
- If
ZipArchive::open()succeeds but an exception occurs during the file/directory iteration (lines 469-486), the ZipArchive will not be properly closed. This could leave the archive file in an inconsistent state. Consider wrapping the iteration in a try-catch block and ensuring$zip->close()is called in a finally block, or at minimum calling$zip->close()before returning false on any error path.
$zip = new ZipArchive();
$result = $zip->open( $archive_filepath, ZipArchive::CREATE );
if ( true !== $result ) {
return false;
}
if ( null === $included_files ) {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( $source_path, RecursiveDirectoryIterator::SKIP_DOTS ),
RecursiveIteratorIterator::SELF_FIRST
);
/**
* @var SplFileInfo $file
*/
foreach ( $iterator as $file ) {
$pathname = $file->getPathname();
$relative_path = substr( $pathname, strlen( $source_path ) );
$this->add_zip_entry( $zip, $pathname, $archive_output_dir_name . str_replace( '\\', '/', $relative_path ), $file->isDir() );
}
} else {
foreach ( $included_files as $relative_filepath ) {
$full_path = $source_path . $relative_filepath;
$this->add_zip_entry( $zip, $full_path, $archive_output_dir_name . str_replace( '\\', '/', $relative_filepath ), is_dir( $full_path ) );
}
}
return $zip->close();
src/Dist_Archive_Command.php:463
- Consider using
ZipArchive::OVERWRITEflag in addition toZipArchive::CREATEto handle file replacement more cleanly. The manual unlink call works but usingZipArchive::CREATE | ZipArchive::OVERWRITEis more idiomatic and handles edge cases better (such as the file being locked or in use). This would replace lines 458-460 with just using the combined flags on line 463.
if ( file_exists( $archive_filepath ) ) {
unlink( $archive_filepath );
}
$zip = new ZipArchive();
$result = $zip->open( $archive_filepath, ZipArchive::CREATE );
src/Dist_Archive_Command.php:505
- The
addFile()andaddEmptyDir()methods can fail and return false (e.g., if a file is unreadable or locked). These failures are not checked, which could result in incomplete archives being created without any warning. Consider checking the return values and either logging a warning or failing with an error message that identifies which specific file or directory could not be added.
if ( $is_dir ) {
$zip->addEmptyDir( $archive_path );
} else {
$zip->addFile( $full_path, $archive_path );
}
src/Dist_Archive_Command.php:137
- The error message "Failed to create ZIP archive." is not very helpful for debugging. The
ZipArchive::open()method returns different error codes (likeZipArchive::ER_EXISTS,ZipArchive::ER_INCONS,ZipArchive::ER_MEMORY, etc.) that could help users understand what went wrong. Consider capturing the error code from$resultand providing a more specific error message, or at least logging the error code for debugging purposes.
if ( true !== $this->create_zip_archive( $archive_absolute_filepath, $source_path, $archive_output_dir_name, $included_files ) ) {
WP_CLI::error( 'Failed to create ZIP archive.' );
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
zipcommand with PHP'sZipArchiveclass inDist_Archive_Command.phpcreate_zip_archive()private method that uses ZipArchive nativelyadd_zip_entry()helper to eliminate duplicated file/directory adding logic__invoke()to usecreate_zip_archive()for zip format, keeptarfor targzext-ziptocomposer.jsonrequireOriginal prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.