2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Interface for the zip extension
8 if (! defined('PHPMYADMIN')) {
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)
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 zip_close($zip_handle);
30 return (array('error' => $error_message, 'data' => $file_data));
33 $first_zip_entry = zip_read($zip_handle);
34 if (false === $first_zip_entry) {
35 $error_message = __('No files found inside ZIP archive!');
36 zip_close($zip_handle);
37 return (array('error' => $error_message, 'data' => $file_data));
40 /* Is the the zip really an ODS file? */
41 $read = zip_entry_read($first_zip_entry);
42 $ods_mime = 'application/vnd.oasis.opendocument.spreadsheet';
43 if (!strcmp($ods_mime, $read)) {
44 $specific_entry = '/^content\.xml$/';
47 if (!isset($specific_entry)) {
48 zip_entry_open($zip_handle, $first_zip_entry, 'r');
49 /* File pointer has already been moved,
50 * so include what was read above */
52 $file_data .= zip_entry_read(
54 zip_entry_filesize($first_zip_entry)
56 zip_entry_close($first_zip_entry);
57 zip_close($zip_handle);
58 return (array('error' => $error_message, 'data' => $file_data));
61 /* Return the correct contents, not just the first entry */
63 $entry = zip_read($zip_handle);
64 if (is_resource($entry)) {
65 if (preg_match($specific_entry, zip_entry_name($entry))) {
66 zip_entry_open($zip_handle, $entry, 'r');
67 $file_data = zip_entry_read(
69 zip_entry_filesize($entry)
71 zip_entry_close($entry);
76 * Either we have reached the end of the zip and still
77 * haven't found $specific_entry or there was a parsing
78 * error that we must display
80 if ($entry === false) {
81 $error_message = __('Error in ZIP archive:')
82 . ' Could not find "' . $specific_entry . '"';
84 $error_message = __('Error in ZIP archive:')
85 . ' ' . PMA_getZipError($zip_handle);
92 zip_close($zip_handle);
93 return (array('error' => $error_message, 'data' => $file_data));
97 * Returns the file name of the first file that matches the given $file_regexp.
99 * @param string $file_regexp regular expression for the file name to match
100 * @param string $file zip archive
102 * @return string the file name of the first file that matches the given regexp
104 function PMA_findFileFromZipArchive($file_regexp, $file)
106 $zip_handle = zip_open($file);
107 if (is_resource($zip_handle)) {
108 $entry = zip_read($zip_handle);
109 while (is_resource($entry)) {
110 if (preg_match($file_regexp, zip_entry_name($entry))) {
111 $file_name = zip_entry_name($entry);
112 zip_close($zip_handle);
115 $entry = zip_read($zip_handle);
118 zip_close($zip_handle);
123 * Returns the number of files in the zip archive.
125 * @param string $file zip archive
127 * @return int the number of files in the zip archive
129 function PMA_getNoOfFilesInZip($file)
132 $zip_handle = zip_open($file);
133 if (is_resource($zip_handle)) {
134 $entry = zip_read($zip_handle);
135 while (is_resource($entry)) {
137 $entry = zip_read($zip_handle);
140 zip_close($zip_handle);
145 * Extracts a set of files from the given zip archive to a given destinations.
147 * @param string $zip_path path to the zip archive
148 * @param string $destination destination to extract files
149 * @param array $entries files in archive that should be extracted
151 * @return bool true on success, false otherwise
153 function PMA_zipExtract($zip_path, $destination, $entries)
155 $zip = new ZipArchive
;
156 if ($zip->open($zip_path) === true) {
157 $zip->extractTo($destination, $entries);
165 * Gets zip error message
167 * @param resource $code error code
169 * @return string error message
171 function PMA_getZipError($code)
173 // I don't think this needs translation
175 case ZIPARCHIVE
::ER_MULTIDISK
:
176 $message = 'Multi-disk zip archives not supported';
178 case ZIPARCHIVE
::ER_READ
:
179 $message = 'Read error';
181 case ZIPARCHIVE
::ER_CRC
:
182 $message = 'CRC error';
184 case ZIPARCHIVE
::ER_NOZIP
:
185 $message = 'Not a zip archive';
187 case ZIPARCHIVE
::ER_INCONS
:
188 $message = 'Zip archive inconsistent';