2 /* vim: set expandtab sw=4 ts=4 sts=4: */
5 * Interface for the zip extension
10 * Gets zip file contents
12 * @param string $specific_entry regular expression to match a file
13 * @return array ($error_message, $file_data); $error_message
14 * is empty if no error
17 function PMA_getZipContents($file, $specific_entry = null)
21 $zip_handle = zip_open($file);
22 if (is_resource($zip_handle)) {
23 $first_zip_entry = zip_read($zip_handle);
24 if (false === $first_zip_entry) {
25 $error_message = __('No files found inside ZIP archive!');
27 /* Is the the zip really an ODS file? */
28 $read = zip_entry_read($first_zip_entry);
29 $ods_mime = 'application/vnd.oasis.opendocument.spreadsheet';
30 if (!strcmp($ods_mime, $read)) {
31 $specific_entry = '/^content\.xml$/';
34 if (isset($specific_entry)) {
35 /* Return the correct contents, not just the first entry */
37 $entry = zip_read($zip_handle);
38 if (is_resource($entry)) {
39 if (preg_match($specific_entry, zip_entry_name($entry))) {
40 zip_entry_open($zip_handle, $entry, 'r');
41 $file_data = zip_entry_read($entry, zip_entry_filesize($entry));
42 zip_entry_close($entry);
47 * Either we have reached the end of the zip and still
48 * haven't found $specific_entry or there was a parsing
49 * error that we must display
51 if ($entry === false) {
52 $error_message = __('Error in ZIP archive:') . ' Could not find "' . $specific_entry . '"';
54 $error_message = __('Error in ZIP archive:') . ' ' . PMA_getZipError($zip_handle);
61 zip_entry_open($zip_handle, $first_zip_entry, 'r');
62 /* File pointer has already been moved, so include what was read above */
64 $file_data .= zip_entry_read($first_zip_entry, zip_entry_filesize($first_zip_entry));
65 zip_entry_close($first_zip_entry);
69 $error_message = __('Error in ZIP archive:') . ' ' . PMA_getZipError($zip_handle);
71 zip_close($zip_handle);
72 return (array('error' => $error_message, 'data' => $file_data));
76 * Returns the file name of the first file that matches the given $file_regexp.
78 * @param string $file_regexp regular expression for the file name to match
79 * @param string $file zip archive
81 function PMA_findFileFromZipArchive ($file_regexp, $file)
83 $zip_handle = zip_open($file);
85 if (is_resource($zip_handle)) {
86 $entry = zip_read($zip_handle);
87 while (is_resource($entry)) {
88 if (preg_match($file_regexp, zip_entry_name($entry))) {
89 $file_name = zip_entry_name($entry);
90 zip_close($zip_handle);
93 $entry = zip_read($zip_handle);
96 zip_close($zip_handle);
101 * Returns the number of files in the zip archive.
103 * @param string $file
105 function PMA_getNoOfFilesInZip($file)
108 $zip_handle = zip_open($file);
110 if (is_resource($zip_handle)) {
111 $entry = zip_read($zip_handle);
112 while (is_resource($entry)) {
114 $entry = zip_read($zip_handle);
117 zip_close($zip_handle);
122 * Extracts a set of files from the given zip archive to a given destinations.
124 * @param string $zip_path
125 * @param string $destination
126 * @param array $entries
128 function PMA_zipExtract($zip_path, $destination, $entries) {
129 $zip = new ZipArchive
;
130 if ($zip->open($zip_path) === true) {
131 $zip->extractTo($destination, $entries);
139 * Gets zip error message
141 * @param integer error code
142 * @return string error message
144 function PMA_getZipError($code)
146 // I don't think this needs translation
148 case ZIPARCHIVE
::ER_MULTIDISK
:
149 $message = 'Multi-disk zip archives not supported';
151 case ZIPARCHIVE
::ER_READ
:
152 $message = 'Read error';
154 case ZIPARCHIVE
::ER_CRC
:
155 $message = 'CRC error';
157 case ZIPARCHIVE
::ER_NOZIP
:
158 $message = 'Not a zip archive';
160 case ZIPARCHIVE
::ER_INCONS
:
161 $message = 'Zip archive inconsistent';