Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / zip_extension.lib.php
blob7eda57365c513c8e1b08506c68adff82e57610ec
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Interface for the zip extension
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Gets zip file contents
15 * @param string $file zip file
16 * @param string $specific_entry regular expression to match a file
18 * @return array ($error_message, $file_data); $error_message
19 * is empty if no error
21 function PMA_getZipContents($file, $specific_entry = null)
23 $error_message = '';
24 $file_data = '';
25 $zip_handle = zip_open($file);
26 if (!is_resource($zip_handle)) {
27 $error_message = __('Error in ZIP archive:')
28 . ' ' . PMA_getZipError($zip_handle);
29 return (array('error' => $error_message, 'data' => $file_data));
32 $first_zip_entry = zip_read($zip_handle);
33 if (false === $first_zip_entry) {
34 $error_message = __('No files found inside ZIP archive!');
35 zip_close($zip_handle);
36 return (array('error' => $error_message, 'data' => $file_data));
39 /* Is the the zip really an ODS file? */
40 $read = zip_entry_read($first_zip_entry);
41 $ods_mime = 'application/vnd.oasis.opendocument.spreadsheet';
42 if (!strcmp($ods_mime, $read)) {
43 $specific_entry = '/^content\.xml$/';
46 if (!isset($specific_entry)) {
47 zip_entry_open($zip_handle, $first_zip_entry, 'r');
48 /* File pointer has already been moved,
49 * so include what was read above */
50 $file_data = $read;
51 $file_data .= zip_entry_read(
52 $first_zip_entry,
53 zip_entry_filesize($first_zip_entry)
55 zip_entry_close($first_zip_entry);
56 zip_close($zip_handle);
57 return (array('error' => $error_message, 'data' => $file_data));
60 /* Return the correct contents, not just the first entry */
61 for ( ; ; ) {
62 $entry = zip_read($zip_handle);
63 if (is_resource($entry)) {
64 if (preg_match($specific_entry, zip_entry_name($entry))) {
65 zip_entry_open($zip_handle, $entry, 'r');
66 $file_data = zip_entry_read(
67 $entry,
68 zip_entry_filesize($entry)
70 zip_entry_close($entry);
71 break;
73 } else {
74 /**
75 * Either we have reached the end of the zip and still
76 * haven't found $specific_entry or there was a parsing
77 * error that we must display
79 if ($entry === false) {
80 $error_message = __('Error in ZIP archive:')
81 . ' Could not find "' . $specific_entry . '"';
82 } else {
83 $error_message = __('Error in ZIP archive:')
84 . ' ' . PMA_getZipError($zip_handle);
87 break;
91 zip_close($zip_handle);
92 return (array('error' => $error_message, 'data' => $file_data));
95 /**
96 * Returns the file name of the first file that matches the given $file_regexp.
98 * @param string $file_regexp regular expression for the file name to match
99 * @param string $file zip archive
101 * @return string the file name of the first file that matches the given regexp
103 function PMA_findFileFromZipArchive($file_regexp, $file)
105 $zip_handle = zip_open($file);
106 if (is_resource($zip_handle)) {
107 $entry = zip_read($zip_handle);
108 while (is_resource($entry)) {
109 if (preg_match($file_regexp, zip_entry_name($entry))) {
110 $file_name = zip_entry_name($entry);
111 zip_close($zip_handle);
112 return $file_name;
114 $entry = zip_read($zip_handle);
117 zip_close($zip_handle);
118 return false;
122 * Returns the number of files in the zip archive.
124 * @param string $file zip archive
126 * @return int the number of files in the zip archive
128 function PMA_getNoOfFilesInZip($file)
130 $count = 0;
131 $zip_handle = zip_open($file);
132 if (is_resource($zip_handle)) {
133 $entry = zip_read($zip_handle);
134 while (is_resource($entry)) {
135 $count++;
136 $entry = zip_read($zip_handle);
138 zip_close($zip_handle);
140 return $count;
144 * Extracts a set of files from the given zip archive to a given destinations.
146 * @param string $zip_path path to the zip archive
147 * @param string $entry file in the archive that should be extracted
149 * @return string|bool data on sucess, false otherwise
151 function PMA_zipExtract($zip_path, $entry)
153 $zip = new ZipArchive;
154 if ($zip->open($zip_path) === true) {
155 $result = $zip->getFromName($entry);
156 $zip->close();
157 return $result;
159 return false;
163 * Gets zip error message
165 * @param resource $code error code
167 * @return string error message
169 function PMA_getZipError($code)
171 // I don't think this needs translation
172 switch ($code) {
173 case ZIPARCHIVE::ER_MULTIDISK:
174 $message = 'Multi-disk zip archives not supported';
175 break;
176 case ZIPARCHIVE::ER_READ:
177 $message = 'Read error';
178 break;
179 case ZIPARCHIVE::ER_CRC:
180 $message = 'CRC error';
181 break;
182 case ZIPARCHIVE::ER_NOZIP:
183 $message = 'Not a zip archive';
184 break;
185 case ZIPARCHIVE::ER_INCONS:
186 $message = 'Zip archive inconsistent';
187 break;
188 default:
189 $message = $code;
191 return $message;