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 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 */
51 $file_data .= zip_entry_read(
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 */
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(
68 zip_entry_filesize($entry)
70 zip_entry_close($entry);
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 . '"';
83 $error_message = __('Error in ZIP archive:')
84 . ' ' . PMA_getZipError($zip_handle);
91 zip_close($zip_handle);
92 return (array('error' => $error_message, 'data' => $file_data));
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);
114 $entry = zip_read($zip_handle);
117 zip_close($zip_handle);
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)
131 $zip_handle = zip_open($file);
132 if (is_resource($zip_handle)) {
133 $entry = zip_read($zip_handle);
134 while (is_resource($entry)) {
136 $entry = zip_read($zip_handle);
138 zip_close($zip_handle);
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);
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
173 case ZIPARCHIVE
::ER_MULTIDISK
:
174 $message = 'Multi-disk zip archives not supported';
176 case ZIPARCHIVE
::ER_READ
:
177 $message = 'Read error';
179 case ZIPARCHIVE
::ER_CRC
:
180 $message = 'CRC error';
182 case ZIPARCHIVE
::ER_NOZIP
:
183 $message = 'Not a zip archive';
185 case ZIPARCHIVE
::ER_INCONS
:
186 $message = 'Zip archive inconsistent';