xref: /php-src/ext/zip/tests/oo_addfile.phpt (revision 0893b4be)
1--TEST--
2ziparchive::addFile() function
3--EXTENSIONS--
4zip
5--FILE--
6<?php
7
8$dirname = __DIR__ . '/';
9include $dirname . 'utils.inc';
10$file = $dirname . 'oo_addfile.zip';
11
12copy($dirname . 'test.zip', $file);
13
14$zip = new ZipArchive;
15if (!$zip->open($file)) {
16    exit('failed');
17}
18var_dump($zip->lastId);
19if (!$zip->addFile($dirname . 'utils.inc', 'test.php')) {
20    echo "failed\n";
21}
22var_dump($zip->lastId);
23if (!$zip->addFile($dirname . 'utils.inc', 'mini.txt', 12, 34)) {
24    echo "failed\n";
25}
26var_dump($zip->lastId);
27if (!$zip->addFile($dirname . 'utils.inc', 'other.txt', 0, ZipArchive::LENGTH_TO_END, ZipArchive::FL_OPEN_FILE_NOW)) {
28    echo "failed\n";
29}
30var_dump($zip->lastId);
31$del = $dirname . '__tmp_oo_addfile.txt';
32file_put_contents($del, 'foo');
33if (!$zip->addFile($del, 'deleted.txt', 0, 0, ZipArchive::FL_OPEN_FILE_NOW)) {
34    echo "failed\n";
35}
36if (substr(PHP_OS, 0, 3) != 'WIN') {
37	unlink($del);
38}
39var_dump($zip->lastId);
40if ($zip->status == ZIPARCHIVE::ER_OK) {
41    if (!verify_entries($zip, [
42        "bar",
43        "foobar/",
44        "foobar/baz",
45        "entry1.txt",
46        "test.php",
47        "mini.txt",
48        "other.txt",
49        "deleted.txt"
50    ])) {
51        echo "failed\n";
52    } else {
53        echo "OK\n";
54    }
55    $zip->close();
56} else {
57    echo "failed\n";
58}
59if (!$zip->open($file)) {
60    exit('failed');
61}
62var_dump(strlen($zip->getFromName('test.php')) == filesize($dirname . 'utils.inc'));
63var_dump(strlen($zip->getFromName('mini.txt')) == 34);
64var_dump(strlen($zip->getFromName('other.txt')) == filesize($dirname . 'utils.inc'));
65var_dump($zip->getFromName('deleted.txt') === "foo");
66@unlink($file);
67@unlink($del);
68?>
69--EXPECT--
70int(-1)
71int(4)
72int(5)
73int(6)
74int(7)
75OK
76bool(true)
77bool(true)
78bool(true)
79bool(true)
80