From 0e20bc0fe57762468b8e46a5a0b1b86b7eb29de1 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Tue, 4 Feb 2025 14:14:44 +0400 Subject: [PATCH] [Fix] Zip spec compliance for exported backup file (#7) ## Motivation for the change, related issues Files in a zip file should only have relative urls as per spec. Right now, when exporting the backup for the site, we create a zip file with a leading slash in the files inside it. This got surfaced in Studio app when we switched to a diff zip library that strictly adheres to zip spec and PG exported zip files fail to process. Related to: https://github.com/Automattic/dotcom-forge/issues/10369 Note: There are other places where `ZipArchive` is used but this PR only modifies the location that impacts the site export. Screenshot 2025-01-30 at 11 44 32 Probably other places can use the same change, and I can follow that up in a separate PR, if you like. ## Testing Instructions (or ideally a Blueprint) Export zip of a site and examine the file paths inside it using the following script: ```php open('/Users/ashfame/Downloads/wordpress-playground-16.zip'); echo "=== ZIP Contents ===".PHP_EOL; for($i = 0; $i < $zip->numFiles; $i++) { echo $zip->getNameIndex($i) . PHP_EOL; } echo "=== End ZIP Contents ===".PHP_EOL; $zip->close(); ``` Prior to this PR, you would see file paths like: ``` /wp-content/themes/twentytwentyfive/patterns/more-posts.php ``` and with this PR you will see file paths without the leading slash: ``` wp-content/themes/twentytwentyfive/patterns/more-posts.php ``` --- packages/playground/blueprints/src/lib/steps/zip-wp-content.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/playground/blueprints/src/lib/steps/zip-wp-content.ts b/packages/playground/blueprints/src/lib/steps/zip-wp-content.ts index 01d8eec14b..b2fe27e28e 100644 --- a/packages/playground/blueprints/src/lib/steps/zip-wp-content.ts +++ b/packages/playground/blueprints/src/lib/steps/zip-wp-content.ts @@ -106,7 +106,8 @@ function zipDir($root, $output, $options = array()) $directory_path = $entry . '/'; array_push($directories, $directory_path); } else if (is_file($entry)) { - $zip->addFile($entry, substr($entry, strlen($zip_root))); + // ensure compliance with zip spec by only using relative paths for files + $zip->addFile($entry, ltrim(substr($entry, strlen($zip_root)), '/')); } } closedir($handle);