Translation update done using Pootle.
[phpmyadmin.git] / libraries / zip_extension.lib.php
blob9aa2758df956e0afbf39e6c710d50479bc97a9c2
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 $file zip file
13 * @param string $specific_entry regular expression to match a file
15 * @return array ($error_message, $file_data); $error_message
16 * is empty if no error
18 function PMA_getZipContents($file, $specific_entry = null)
20 $error_message = '';
21 $file_data = '';
22 $zip_handle = zip_open($file);
23 if (is_resource($zip_handle)) {
24 $first_zip_entry = zip_read($zip_handle);
25 if (false === $first_zip_entry) {
26 $error_message = __('No files found inside ZIP archive!');
27 } else {
28 /* Is the the zip really an ODS file? */
29 $read = zip_entry_read($first_zip_entry);
30 $ods_mime = 'application/vnd.oasis.opendocument.spreadsheet';
31 if (!strcmp($ods_mime, $read)) {
32 $specific_entry = '/^content\.xml$/';
35 if (isset($specific_entry)) {
36 /* Return the correct contents, not just the first entry */
37 for ( ; ; ) {
38 $entry = zip_read($zip_handle);
39 if (is_resource($entry)) {
40 if (preg_match($specific_entry, zip_entry_name($entry))) {
41 zip_entry_open($zip_handle, $entry, 'r');
42 $file_data = zip_entry_read($entry, zip_entry_filesize($entry));
43 zip_entry_close($entry);
44 break;
46 } else {
47 /**
48 * Either we have reached the end of the zip and still
49 * haven't found $specific_entry or there was a parsing
50 * error that we must display
52 if ($entry === false) {
53 $error_message = __('Error in ZIP archive:') . ' Could not find "' . $specific_entry . '"';
54 } else {
55 $error_message = __('Error in ZIP archive:') . ' ' . PMA_getZipError($zip_handle);
58 break;
61 } else {
62 zip_entry_open($zip_handle, $first_zip_entry, 'r');
63 /* File pointer has already been moved, so include what was read above */
64 $file_data = $read;
65 $file_data .= zip_entry_read($first_zip_entry, zip_entry_filesize($first_zip_entry));
66 zip_entry_close($first_zip_entry);
69 } else {
70 $error_message = __('Error in ZIP archive:') . ' ' . PMA_getZipError($zip_handle);
72 zip_close($zip_handle);
73 return (array('error' => $error_message, 'data' => $file_data));
76 /**
77 * Returns the file name of the first file that matches the given $file_regexp.
79 * @param string $file_regexp regular expression for the file name to match
80 * @param string $file zip archive
82 * @return string the file name of the first file that matches the given regexp
84 function PMA_findFileFromZipArchive ($file_regexp, $file)
86 $zip_handle = zip_open($file);
87 $found = false;
88 if (is_resource($zip_handle)) {
89 $entry = zip_read($zip_handle);
90 while (is_resource($entry)) {
91 if (preg_match($file_regexp, zip_entry_name($entry))) {
92 $file_name = zip_entry_name($entry);
93 zip_close($zip_handle);
94 return $file_name;
96 $entry = zip_read($zip_handle);
99 zip_close($zip_handle);
100 return false;
104 * Returns the number of files in the zip archive.
106 * @param string $file zip archive
108 * @return int the number of files in the zip archive
110 function PMA_getNoOfFilesInZip($file)
112 $count = 0;
113 $zip_handle = zip_open($file);
114 $found = false;
115 if (is_resource($zip_handle)) {
116 $entry = zip_read($zip_handle);
117 while (is_resource($entry)) {
118 $count++;
119 $entry = zip_read($zip_handle);
122 zip_close($zip_handle);
123 return $count;
127 * Extracts a set of files from the given zip archive to a given destinations.
129 * @param string $zip_path path to the zip archive
130 * @param string $destination destination to extract files
131 * @param array $entries files in archive that should be extracted
133 * @return bool true on sucess, false otherwise
135 function PMA_zipExtract($zip_path, $destination, $entries)
137 $zip = new ZipArchive;
138 if ($zip->open($zip_path) === true) {
139 $zip->extractTo($destination, $entries);
140 $zip->close();
141 return true;
143 return false;
147 * Gets zip error message
149 * @param integer $code error code
151 * @return string error message
153 function PMA_getZipError($code)
155 // I don't think this needs translation
156 switch ($code) {
157 case ZIPARCHIVE::ER_MULTIDISK:
158 $message = 'Multi-disk zip archives not supported';
159 break;
160 case ZIPARCHIVE::ER_READ:
161 $message = 'Read error';
162 break;
163 case ZIPARCHIVE::ER_CRC:
164 $message = 'CRC error';
165 break;
166 case ZIPARCHIVE::ER_NOZIP:
167 $message = 'Not a zip archive';
168 break;
169 case ZIPARCHIVE::ER_INCONS:
170 $message = 'Zip archive inconsistent';
171 break;
172 default:
173 $message = $code;
175 return $message;