2 /* vim: set expandtab sw=4 ts=4 sts=4: */
5 * Interface for the zip extension
10 * Gets zip file contents
12 * @param string $file zip file
13 * @param string $specific_entry regular expression to match a file
15 * @return array ($error_message, $file_data); $error_message
16 * is empty if no error
18 function PMA_getZipContents($file, $specific_entry = null)
22 $zip_handle = zip_open($file);
23 if (is_resource($zip_handle)) {
24 $first_zip_entry = zip_read($zip_handle);
25 if (false === $first_zip_entry) {
26 $error_message = __('No files found inside ZIP archive!');
28 /* Is the the zip really an ODS file? */
29 $read = zip_entry_read($first_zip_entry);
30 $ods_mime = 'application/vnd.oasis.opendocument.spreadsheet';
31 if (!strcmp($ods_mime, $read)) {
32 $specific_entry = '/^content\.xml$/';
35 if (isset($specific_entry)) {
36 /* Return the correct contents, not just the first entry */
38 $entry = zip_read($zip_handle);
39 if (is_resource($entry)) {
40 if (preg_match($specific_entry, zip_entry_name($entry))) {
41 zip_entry_open($zip_handle, $entry, 'r');
42 $file_data = zip_entry_read($entry, zip_entry_filesize($entry));
43 zip_entry_close($entry);
48 * Either we have reached the end of the zip and still
49 * haven't found $specific_entry or there was a parsing
50 * error that we must display
52 if ($entry === false) {
53 $error_message = __('Error in ZIP archive:') . ' Could not find "' . $specific_entry . '"';
55 $error_message = __('Error in ZIP archive:') . ' ' . PMA_getZipError($zip_handle);
62 zip_entry_open($zip_handle, $first_zip_entry, 'r');
63 /* File pointer has already been moved, so include what was read above */
65 $file_data .= zip_entry_read($first_zip_entry, zip_entry_filesize($first_zip_entry));
66 zip_entry_close($first_zip_entry);
70 $error_message = __('Error in ZIP archive:') . ' ' . PMA_getZipError($zip_handle);
72 zip_close($zip_handle);
73 return (array('error' => $error_message, 'data' => $file_data));
77 * Returns the file name of the first file that matches the given $file_regexp.
79 * @param string $file_regexp regular expression for the file name to match
80 * @param string $file zip archive
82 * @return string the file name of the first file that matches the given regexp
84 function PMA_findFileFromZipArchive ($file_regexp, $file)
86 $zip_handle = zip_open($file);
88 if (is_resource($zip_handle)) {
89 $entry = zip_read($zip_handle);
90 while (is_resource($entry)) {
91 if (preg_match($file_regexp, zip_entry_name($entry))) {
92 $file_name = zip_entry_name($entry);
93 zip_close($zip_handle);
96 $entry = zip_read($zip_handle);
99 zip_close($zip_handle);
104 * Returns the number of files in the zip archive.
106 * @param string $file zip archive
108 * @return int the number of files in the zip archive
110 function PMA_getNoOfFilesInZip($file)
113 $zip_handle = zip_open($file);
115 if (is_resource($zip_handle)) {
116 $entry = zip_read($zip_handle);
117 while (is_resource($entry)) {
119 $entry = zip_read($zip_handle);
122 zip_close($zip_handle);
127 * Extracts a set of files from the given zip archive to a given destinations.
129 * @param string $zip_path path to the zip archive
130 * @param string $destination destination to extract files
131 * @param array $entries files in archive that should be extracted
133 * @return bool true on sucess, false otherwise
135 function PMA_zipExtract($zip_path, $destination, $entries)
137 $zip = new ZipArchive
;
138 if ($zip->open($zip_path) === true) {
139 $zip->extractTo($destination, $entries);
147 * Gets zip error message
149 * @param integer $code error code
151 * @return string error message
153 function PMA_getZipError($code)
155 // I don't think this needs translation
157 case ZIPARCHIVE
::ER_MULTIDISK
:
158 $message = 'Multi-disk zip archives not supported';
160 case ZIPARCHIVE
::ER_READ
:
161 $message = 'Read error';
163 case ZIPARCHIVE
::ER_CRC
:
164 $message = 'CRC error';
166 case ZIPARCHIVE
::ER_NOZIP
:
167 $message = 'Not a zip archive';
169 case ZIPARCHIVE
::ER_INCONS
:
170 $message = 'Zip archive inconsistent';