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 $first_zip_entry = zip_read($zip_handle);
28 if (false === $first_zip_entry) {
29 $error_message = __('No files found inside ZIP archive!');
31 /* Is the the zip really an ODS file? */
32 $read = zip_entry_read($first_zip_entry);
33 $ods_mime = 'application/vnd.oasis.opendocument.spreadsheet';
34 if (!strcmp($ods_mime, $read)) {
35 $specific_entry = '/^content\.xml$/';
38 if (isset($specific_entry)) {
39 /* Return the correct contents, not just the first entry */
41 $entry = zip_read($zip_handle);
42 if (is_resource($entry)) {
43 if (preg_match($specific_entry, zip_entry_name($entry))) {
44 zip_entry_open($zip_handle, $entry, 'r');
45 $file_data = zip_entry_read(
47 zip_entry_filesize($entry)
49 zip_entry_close($entry);
54 * Either we have reached the end of the zip and still
55 * haven't found $specific_entry or there was a parsing
56 * error that we must display
58 if ($entry === false) {
59 $error_message = __('Error in ZIP archive:')
60 . ' Could not find "' . $specific_entry . '"';
62 $error_message = __('Error in ZIP archive:')
63 . ' ' . PMA_getZipError($zip_handle);
70 zip_entry_open($zip_handle, $first_zip_entry, 'r');
71 /* File pointer has already been moved,
72 * so include what was read above */
74 $file_data .= zip_entry_read(
76 zip_entry_filesize($first_zip_entry)
78 zip_entry_close($first_zip_entry);
82 $error_message = __('Error in ZIP archive:')
83 . ' ' . PMA_getZipError($zip_handle);
85 zip_close($zip_handle);
86 return (array('error' => $error_message, 'data' => $file_data));
90 * Returns the file name of the first file that matches the given $file_regexp.
92 * @param string $file_regexp regular expression for the file name to match
93 * @param string $file zip archive
95 * @return string the file name of the first file that matches the given regexp
97 function PMA_findFileFromZipArchive ($file_regexp, $file)
99 $zip_handle = zip_open($file);
100 if (is_resource($zip_handle)) {
101 $entry = zip_read($zip_handle);
102 while (is_resource($entry)) {
103 if (preg_match($file_regexp, zip_entry_name($entry))) {
104 $file_name = zip_entry_name($entry);
105 zip_close($zip_handle);
108 $entry = zip_read($zip_handle);
111 zip_close($zip_handle);
116 * Returns the number of files in the zip archive.
118 * @param string $file zip archive
120 * @return int the number of files in the zip archive
122 function PMA_getNoOfFilesInZip($file)
125 $zip_handle = zip_open($file);
126 if (is_resource($zip_handle)) {
127 $entry = zip_read($zip_handle);
128 while (is_resource($entry)) {
130 $entry = zip_read($zip_handle);
133 zip_close($zip_handle);
138 * Extracts a set of files from the given zip archive to a given destinations.
140 * @param string $zip_path path to the zip archive
141 * @param string $destination destination to extract files
142 * @param array $entries files in archive that should be extracted
144 * @return bool true on sucess, false otherwise
146 function PMA_zipExtract($zip_path, $destination, $entries)
148 $zip = new ZipArchive
;
149 if ($zip->open($zip_path) === true) {
150 $zip->extractTo($destination, $entries);
158 * Gets zip error message
160 * @param integer $code error code
162 * @return string error message
164 function PMA_getZipError($code)
166 // I don't think this needs translation
168 case ZIPARCHIVE
::ER_MULTIDISK
:
169 $message = 'Multi-disk zip archives not supported';
171 case ZIPARCHIVE
::ER_READ
:
172 $message = 'Read error';
174 case ZIPARCHIVE
::ER_CRC
:
175 $message = 'CRC error';
177 case ZIPARCHIVE
::ER_NOZIP
:
178 $message = 'Not a zip archive';
180 case ZIPARCHIVE
::ER_INCONS
:
181 $message = 'Zip archive inconsistent';