1. <?php
  2. /**
  3. * Compresses file contents for synchronous execution testing.
  4. *
  5. * @param string $filePath The path to the file to compress.
  6. * @return string The compressed file contents. Returns false on error.
  7. */
  8. function compactFileContents(string $filePath): string|false {
  9. if (!file_exists($filePath)) {
  10. return false; // File does not exist
  11. }
  12. $fileContents = file_get_contents($filePath);
  13. if ($fileContents === false) {
  14. return false; // Failed to read file
  15. }
  16. // Use gzcompress for synchronous compression
  17. $compressedContents = gzcompress($fileContents, 9); // 9 is the highest compression level
  18. if ($compressedContents === false) {
  19. return false; // Failed to compress
  20. }
  21. return $compressedContents;
  22. }
  23. //Example usage (replace with your file path)
  24. //$filePath = 'test.txt';
  25. //$compressedData = compactFileContents($filePath);
  26. //if ($compressedData !== false) {
  27. // echo "File compressed successfully.\n";
  28. // //Output the compressed data here or save it to a file.
  29. // echo $compressedData;
  30. //} else {
  31. // echo "File compression failed.\n";
  32. //}
  33. ?>

Add your comment