PHP Zip context options


Introduction

PHP's ZIP extension registers zip:// wrapper. PHP 7.2.0 onwards supports passwords for encrypted archives. There is only one Zip context option called password

Example

First create ZIP archive as follows:

<?php
$zip = new ZipArchive;
$zip->open('test.zip');
$zip->setPassword("MySecretPassword");
$zip->addFile('c:/xampp/php/test.txt', 'test.txt');
$zip->close();
>>

To read file from zip:// stream, use following code

<?php
$opts = array(
   'zip' => array(
      'password' => 'secret',
   ),
);
$context = stream_context_create($opts);
echo file_get_contents('zip://test.zip#test.txt', false, $context);
?>

Updated on: 21-Sep-2020

191 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements