bug80833.phpt 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. --TEST--
  2. Bug #80833 (ZipArchive::getStream doesn't use setPassword)
  3. --SKIPIF--
  4. <?php
  5. if (!extension_loaded('zip')) die("skip zip extension not available");
  6. if (!method_exists('ZipArchive', 'setEncryptionName')) die('skip encryption not supported');
  7. ?>
  8. --FILE--
  9. <?php
  10. $create_zip = new ZipArchive();
  11. $create_zip->open(__DIR__ . "/80833.zip", ZipArchive::CREATE);
  12. $create_zip->setPassword("default_password");
  13. $create_zip->addFromString("test.txt", "This is a test string.");
  14. $create_zip->setEncryptionName("test.txt", ZipArchive::EM_AES_256, "first_password");
  15. $create_zip->addFromString("test2.txt", "This is another test string.");
  16. $create_zip->setEncryptionName("test2.txt", ZipArchive::EM_AES_256, "second_password");
  17. $create_zip->close();
  18. // Stream API
  19. $o = [
  20. 'zip' => [
  21. 'password' => "first_password",
  22. ],
  23. ];
  24. $c = stream_context_create($o);
  25. var_dump(file_get_contents("zip://" . __DIR__ . "/80833.zip#test.txt", false, $c));
  26. // getStream method
  27. $extract_zip = new ZipArchive();
  28. $extract_zip->open(__DIR__ . "/80833.zip", ZipArchive::RDONLY);
  29. $extract_zip->setPassword("first_password");
  30. $file_stream = $extract_zip->getStream("test.txt");
  31. var_dump(stream_get_contents($file_stream));
  32. fclose($file_stream);
  33. $extract_zip->setPassword("second_password");
  34. $file_stream = $extract_zip->getStream("test2.txt");
  35. var_dump(stream_get_contents($file_stream));
  36. fclose($file_stream);
  37. // Archive close before the stream
  38. $extract_zip->setPassword("first_password");
  39. $file_stream = $extract_zip->getStream("test.txt");
  40. $extract_zip->close();
  41. var_dump(stream_get_contents($file_stream));
  42. fclose($file_stream);
  43. ?>
  44. --CLEAN--
  45. <?php
  46. @unlink(__DIR__ . "/80833.zip");
  47. ?>
  48. --EXPECTF--
  49. string(22) "This is a test string."
  50. string(22) "This is a test string."
  51. string(28) "This is another test string."
  52. Warning: stream_get_contents(): Zip stream error: Containing zip archive was closed in %s
  53. string(0) ""