zip-uitpakken
Dit is de functie:
Code (php)
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
49
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
49
<?php
function extract_zip($zip_file, $to_path = '.') {
// Check if $to_path exists
if ( ! chdir($to_path)) {
return false;
}
$absolute_to_path = getcwd();
chdir(dirname(__FILE__));
// Open zip file
if ( ! $zip = zip_open($zip_file)) {
return false;
}
// Check each item in zip file
while ($entry = zip_read($zip)) {
// Check if it's not a directory
if (zip_entry_filesize($entry) != 0) {
$parts = explode('/', $zip_entry_name($entry));
$filename = array_pop($parts);
$path = count($parts) == 0 ? '.' : implode('/', $parts);
chdir($absolute_to_path);
// Create directory
if (! is_dir($path) && ! mkdir($path, 0777, true)) {
return false;
}
chdir($path);
// Save file
if (! zip_entry_open($zip, $entry)) {
return false;
}
if (file_put_contents($filename, zip_entry_read($entry)) === false) {
return false;
}
zip_entry_close($entry);
}
}
// close zip file
zip_close($zip);
// return true when no error occured
return true;
}
?>
function extract_zip($zip_file, $to_path = '.') {
// Check if $to_path exists
if ( ! chdir($to_path)) {
return false;
}
$absolute_to_path = getcwd();
chdir(dirname(__FILE__));
// Open zip file
if ( ! $zip = zip_open($zip_file)) {
return false;
}
// Check each item in zip file
while ($entry = zip_read($zip)) {
// Check if it's not a directory
if (zip_entry_filesize($entry) != 0) {
$parts = explode('/', $zip_entry_name($entry));
$filename = array_pop($parts);
$path = count($parts) == 0 ? '.' : implode('/', $parts);
chdir($absolute_to_path);
// Create directory
if (! is_dir($path) && ! mkdir($path, 0777, true)) {
return false;
}
chdir($path);
// Save file
if (! zip_entry_open($zip, $entry)) {
return false;
}
if (file_put_contents($filename, zip_entry_read($entry)) === false) {
return false;
}
zip_entry_close($entry);
}
}
// close zip file
zip_close($zip);
// return true when no error occured
return true;
}
?>
Je kunt de functie zo gebruiken:
Edit:
De efficiƫntie verbeterd.
Code (php)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
<?php
$filename = strrchr(zip_entry_name($entry), '/');
$path = str_replace($filename, '', zip_entry_name($entry));
$filename = substr($filename, 1);
if ($filename == '') {
$filename = $path;
$path = '.';
}
?>
$filename = strrchr(zip_entry_name($entry), '/');
$path = str_replace($filename, '', zip_entry_name($entry));
$filename = substr($filename, 1);
if ($filename == '') {
$filename = $path;
$path = '.';
}
?>
veranderd in: