Translation update done using Pootle.
[phpmyadmin/ammaryasirr.git] / libraries / zip_extension.lib.php
blob7bfa84875fb663aa257988be2297491740e3273b
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 /**
5 * Interface for the zip extension
6 * @package phpMyAdmin
7 */
9 /**
10 * Gets zip file contents
12 * @param string $specific_entry regular expression to match a file
13 * @return array ($error_message, $file_data); $error_message
14 * is empty if no error
17 function PMA_getZipContents($file, $specific_entry = null)
19 $error_message = '';
20 $file_data = '';
21 $zip_handle = zip_open($file);
22 if (is_resource($zip_handle)) {
23 $first_zip_entry = zip_read($zip_handle);
24 if (false === $first_zip_entry) {
25 $error_message = __('No files found inside ZIP archive!');
26 } else {
27 /* Is the the zip really an ODS file? */
28 $read = zip_entry_read($first_zip_entry);
29 $ods_mime = 'application/vnd.oasis.opendocument.spreadsheet';
30 if (!strcmp($ods_mime, $read)) {
31 $specific_entry = '/^content\.xml$/';
34 if (isset($specific_entry)) {
35 /* Return the correct contents, not just the first entry */
36 for ( ; ; ) {
37 $entry = zip_read($zip_handle);
38 if (is_resource($entry)) {
39 if (preg_match($specific_entry, zip_entry_name($entry))) {
40 zip_entry_open($zip_handle, $entry, 'r');
41 $file_data = zip_entry_read($entry, zip_entry_filesize($entry));
42 zip_entry_close($entry);
43 break;
45 } else {
46 /**
47 * Either we have reached the end of the zip and still
48 * haven't found $specific_entry or there was a parsing
49 * error that we must display
51 if ($entry === false) {
52 $error_message = __('Error in ZIP archive:') . ' Could not find "' . $specific_entry . '"';
53 } else {
54 $error_message = __('Error in ZIP archive:') . ' ' . PMA_getZipError($zip_handle);
57 break;
60 } else {
61 zip_entry_open($zip_handle, $first_zip_entry, 'r');
62 /* File pointer has already been moved, so include what was read above */
63 $file_data = $read;
64 $file_data .= zip_entry_read($first_zip_entry, zip_entry_filesize($first_zip_entry));
65 zip_entry_close($first_zip_entry);
68 } else {
69 $error_message = __('Error in ZIP archive:') . ' ' . PMA_getZipError($zip_handle);
71 zip_close($zip_handle);
72 return (array('error' => $error_message, 'data' => $file_data));
75 /**
76 * Returns the file name of the first file that matches the given $file_regexp.
78 * @param string $file_regexp regular expression for the file name to match
79 * @param string $file zip archive
81 function PMA_findFileFromZipArchive ($file_regexp, $file)
83 $zip_handle = zip_open($file);
84 $found = false;
85 if (is_resource($zip_handle)) {
86 $entry = zip_read($zip_handle);
87 while (is_resource($entry)) {
88 if (preg_match($file_regexp, zip_entry_name($entry))) {
89 $file_name = zip_entry_name($entry);
90 zip_close($zip_handle);
91 return $file_name;
93 $entry = zip_read($zip_handle);
96 zip_close($zip_handle);
97 return false;
101 * Returns the number of files in the zip archive.
103 * @param string $file
105 function PMA_getNoOfFilesInZip($file)
107 $count = 0;
108 $zip_handle = zip_open($file);
109 $found = false;
110 if (is_resource($zip_handle)) {
111 $entry = zip_read($zip_handle);
112 while (is_resource($entry)) {
113 $count++;
114 $entry = zip_read($zip_handle);
117 zip_close($zip_handle);
118 return $count;
122 * Extracts a set of files from the given zip archive to a given destinations.
124 * @param string $zip_path
125 * @param string $destination
126 * @param array $entries
128 function PMA_zipExtract($zip_path, $destination, $entries) {
129 $zip = new ZipArchive;
130 if ($zip->open($zip_path) === true) {
131 $zip->extractTo($destination, $entries);
132 $zip->close();
133 return true;
135 return false;
139 * Gets zip error message
141 * @param integer error code
142 * @return string error message
144 function PMA_getZipError($code)
146 // I don't think this needs translation
147 switch ($code) {
148 case ZIPARCHIVE::ER_MULTIDISK:
149 $message = 'Multi-disk zip archives not supported';
150 break;
151 case ZIPARCHIVE::ER_READ:
152 $message = 'Read error';
153 break;
154 case ZIPARCHIVE::ER_CRC:
155 $message = 'CRC error';
156 break;
157 case ZIPARCHIVE::ER_NOZIP:
158 $message = 'Not a zip archive';
159 break;
160 case ZIPARCHIVE::ER_INCONS:
161 $message = 'Zip archive inconsistent';
162 break;
163 default:
164 $message = $code;
166 return $message;