blob: 9ef7dd84975b78b5d36eeea5a1fad577dbf1a7ba (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
<?php
error_reporting(E_ALL);
if (!extension_loaded('zip')) {
dl('zip.so');
}
$name = __DIR__ . '/encrypted.zip';
$pass = 'secret';
$file = 'foo.php';
echo "== Create with per file password\n";
$zip = new ZipArchive;
$zip->open($name, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE);
$zip->addFile(__FILE__, $file);
$zip->setEncryptionName($file, ZipArchive::EM_AES_256, $pass);
$zip->close();
echo "== Create with global password\n";
$zip = new ZipArchive;
$zip->open($name, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE);
$zip->setPassword($pass);
$zip->addFile(__FILE__, $file);
$zip->setEncryptionName($file, ZipArchive::EM_AES_256);
$zip->close();
echo "== Stat\n";
$zip->open($name);
print_r($zip->statName($file));
echo "== Read\n";
$zip->setPassword($pass);
$text = $zip->getFromName($file);
printf("Size = %d\n", strlen($text));
$zip->close();
echo "== Stream with context\n";
$ctx = stream_context_create(array(
'zip' => array(
'password' => $pass
)
));
$text = file_get_contents("zip://$name#$file", false, $ctx);
printf("Size = %d\n", strlen($text));
|