public static function ExtractEntry($zip, $entry) { zip_entry_open($zip, $entry); $data = zip_entry_read($entry, zip_entry_filesize($entry)); zip_entry_close($entry); return $data; }
public function unzip($file) { $zip = zip_open(realpath(".") . "/" . $file); if (!$zip) { return "Unable to proccess file '{$file}'"; } $e = ''; while ($zip_entry = zip_read($zip)) { $zdir = dirname(zip_entry_name($zip_entry)); $zname = zip_entry_name($zip_entry); if (!zip_entry_open($zip, $zip_entry, "r")) { $e .= "Unable to proccess file '{$zname}'"; continue; } if (!is_dir($zdir)) { mkdirr($zdir, 0777); } #print "{$zdir} | {$zname} \n"; $zip_fs = zip_entry_filesize($zip_entry); if (empty($zip_fs)) { continue; } $zz = zip_entry_read($zip_entry, $zip_fs); $z = fopen($zname, "w"); fwrite($z, $zz); fclose($z); zip_entry_close($zip_entry); } zip_close($zip); return $e; }
public function read_file_docx($filename) { $striped_content = ''; $content = ''; if (!$filename || !file_exists($filename)) { return false; } $zip = zip_open($filename); if (!$zip || is_numeric($zip)) { return false; } while ($zip_entry = zip_read($zip)) { if (zip_entry_open($zip, $zip_entry) == FALSE) { continue; } if (zip_entry_name($zip_entry) != "word/document.xml") { continue; } $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); zip_entry_close($zip_entry); } // end while zip_close($zip); //echo $content; //echo "<hr>"; //file_put_contents('1.xml', $content); $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content); $content = str_replace('</w:r></w:p>', "\r\n", $content); $striped_content = strip_tags($content); return $striped_content; }
function load_sheet($sheet_index) { $this->zip = zip_open($this->xlsx); if ($this->zip) { while ($zip_entry = zip_read($this->zip)) { if (zip_entry_name($zip_entry) == 'xl/worksheets/sheet' . $sheet_index . '.xml') { // 실제 로드되는 파일 if (zip_entry_open($this->zip, $zip_entry, "r")) { $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); $arr = simplexml_load_string($buf); $this->rows =& $arr->sheetData->row; $this->rowsize = sizeof($this->rows); // dimension 을 파싱해서 써도 되지만 구찬아서.; if ($this->rowsize > 0) { $this->colsize = (int) array_pop(explode(":", (string) $this->rows[0]['spans'])); // 1:7 이런식으로 값이 들어있음. } else { $this->colsize = 0; } zip_entry_close($zip_entry); } // if } } // while } // if this zip }
function unpackInto($file_path, $dir_path) { $zip = zip_open($file_path); if (!is_dir($dir_path)) { throw new Exception($dir_path . ' should be a directory but isn\'t.'); } if ($zip) { while ($zip_entry = zip_read($zip)) { zip_entry_open($zip, $zip_entry); if (substr(zip_entry_name($zip_entry), -1) == '/') { //this $zip_entry is a directory. create it. $zdir = substr(zip_entry_name($zip_entry), 0, -1); mkdir($dir_path . '/' . $zdir); } else { $file = basename(zip_entry_name($zip_entry)); $fp = fopen($dir_path . '/' . zip_entry_name($zip_entry), "w+"); //echo zip_entry_name($zip_entry); if (zip_entry_open($zip, $zip_entry, "r")) { $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); zip_entry_close($zip_entry); } fwrite($fp, $buf); fclose($fp); } } zip_close($zip); } }
function unzip($zipfile) { $zip = zip_open($zipfile); while ($zip_entry = zip_read($zip)) { zip_entry_open($zip, $zip_entry); if (substr(zip_entry_name($zip_entry), -1) == '/') { $zdir = substr(zip_entry_name($zip_entry), 0, -1); if (file_exists($zdir)) { trigger_error('Directory "<b>' . $zdir . '</b>" exists', E_USER_ERROR); return false; } mkdir($zdir); } else { $name = zip_entry_name($zip_entry); if (file_exists($name)) { trigger_error('File "<b>' . $name . '</b>" exists', E_USER_ERROR); return false; } $fopen = fopen($name, "w"); fwrite($fopen, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)), zip_entry_filesize($zip_entry)); } zip_entry_close($zip_entry); } zip_close($zip); return true; }
function unpackZip($file, $dir) { if ($zip = zip_open(getcwd() . $file)) { if ($zip) { while ($zip_entry = zip_read($zip)) { if (zip_entry_open($zip, $zip_entry, "r")) { $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); $dir_name = dirname(zip_entry_name($zip_entry)); if ($dir_name == "." || !is_dir($dir_name)) { $dir_op = $dir; foreach (explode("/", $dir_name) as $k) { $dir_op = $dir_op . $k; if (is_file($dir_op)) { unlink($dir_op); } if (!is_dir($dir_op)) { mkdir($dir_op); } $dir_op = $dir_op . "/"; } } $fp = fopen($dir . zip_entry_name($zip_entry), "w"); fwrite($fp, $buf); zip_entry_close($zip_entry); } else { return false; } } zip_close($zip); } } else { return false; } return true; }
public static function parse($filename) { $striped_content = ''; $content = ''; if (!$filename || !file_exists($filename)) { return false; } $zip = zip_open($filename); if (!$zip || is_numeric($zip)) { return false; } while ($zip_entry = zip_read($zip)) { if (zip_entry_open($zip, $zip_entry) == FALSE) { continue; } if (zip_entry_name($zip_entry) != "content.xml") { continue; } $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); zip_entry_close($zip_entry); } zip_close($zip); $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content); $content = str_replace('</w:r></w:p>', "\r\n", $content); $striped_content = strip_tags($content); return $striped_content; }
function unzip($file, $dir = 'unzip/') { if (!file_exists($dir)) { mkdir($dir, 0777); } $zip_handle = zip_open($file); if (is_resource($zip_handle)) { while ($zip_entry = zip_read($zip_handle)) { if ($zip_entry) { $zip_name = zip_entry_name($zip_entry); $zip_size = zip_entry_filesize($zip_entry); if ($zip_size == 0 && $zip_name[strlen($zip_name) - 1] == '/') { mkdir($dir . $zip_name, 0775); } else { @zip_entry_open($zip_handle, $zip_entry, 'r'); $fp = @fopen($dir . $zip_name, 'wb+'); @fwrite($fp, zip_entry_read($zip_entry, $zip_size), $zip_size); @fclose($fp); @chmod($dir . $zip_name, 0775); @zip_entry_close($zip_entry); } } } return true; } else { zip_close($zip_handle); return false; } }
private function read_docx() { $striped_content = ''; $content = ''; $zip = zip_open($this->filename); if (!$zip || is_numeric($zip)) { return false; } while ($zip_entry = zip_read($zip)) { if (zip_entry_open($zip, $zip_entry) == FALSE) { continue; } if (zip_entry_name($zip_entry) != "word/document.xml") { continue; } $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); zip_entry_close($zip_entry); } // end while zip_close($zip); $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content); $content = str_replace('</w:r></w:p>', "\r\n", $content); $striped_content = strip_tags($content); return $striped_content; }
public function odt_unzip($file, $save = false) { if (!function_exists('zip_open')) { die('NO ZIP FUNCTIONS DETECTED. Do you have the PECL ZIP extensions loaded?'); } if ($zip = zip_open($file)) { while ($zip_entry = zip_read($zip)) { $filename = zip_entry_name($zip_entry); if (zip_entry_name($zip_entry) == 'content.xml' and zip_entry_open($zip, $zip_entry, "r")) { $content = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); zip_entry_close($zip_entry); } if (preg_match('Pictures/', $filename) and !preg_match('Object', $filename) and zip_entry_open($zip, $zip_entry, "r")) { $img[$filename] = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); zip_entry_close($zip_entry); } } if (isset($content)) { if ($save == false) { return array($content, $img); } else { file_put_contents('content.xml', $content); if (is_array($img)) { if (!is_dir('Pictures')) { mkdir('Pictures'); } foreach ($img as $key => $val) { file_put_contents($key, $val); } } } } } }
/** * APP版本更新接口 * @date: 2016年1月10日 下午9:30:33 * * @author : Elliot * @param * : none * @return : */ public function version_get() { $dir = 'data'; $dh = @opendir($dir); $return = array(); while ($file = @readdir($dh)) { // 循环读取目录下的文件 if ($file != '.' and $file != '..') { $path = $dir . DIRECTORY_SEPARATOR . $file; // 设置目录,用于含有子目录的情况 if (is_file($path)) { $filetime[] = date("Y-m-d H:i:s", filemtime($path)); // 获取文件最近修改日期 $return[] = $dir . DIRECTORY_SEPARATOR . $file; } } } @closedir($dh); // 关闭目录流 array_multisort($filetime, SORT_DESC, SORT_STRING, $return); // 按时间排序 $file = current($return); $zip = zip_open($file); if ($zip) { while ($zip_entry = zip_read($zip)) { if (zip_entry_name($zip_entry) == self::INSTALLPACKETVERSIONFILE && zip_entry_open($zip, $zip_entry, "r")) { $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); zip_entry_close($zip_entry); } } zip_close($zip); } $result = array('version' => $buf, 'url' => 'http://' . $_SERVER['SERVER_NAME'] . DIRECTORY_SEPARATOR . $file); $this->response($result, 200); }
function extract_zip($zipfile, $dir) { if (function_exists('zip_open')) { $zip = zip_open($zipfile); if ($zip) { while ($zip_entry = zip_read($zip)) { if (zip_entry_filesize($zip_entry) > 0) { if (zip_entry_open($zip, $zip_entry, "r")) { $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); zip_entry_close($zip_entry); file_put($dir . '/' . zip_entry_name($zip_entry), $buf); } } else { dir_create($dir . '/' . zip_entry_name($zip_entry)); } } zip_close($zip); } } else { $array = $this->list_zip($zipfile); $count = count($array); $f = 0; $d = 0; for ($i = 0; $i < $count; $i++) { if ($array[$i]['folder'] == 0) { if ($this->extract_file($zipfile, $dir, $i) > 0) { $f++; } } else { $d++; } } } return true; }
function upload_article_handler(&$request, &$session, &$files) { $publication = Input::Get('Pub', 'int', 0); $issue = Input::Get('Issue', 'int', 0); $section = Input::Get('Section', 'int', 0); $language = Input::Get('Language', 'int', 0); $sLanguage = Input::Get('sLanguage', 'int', 0); $articleNumber = Input::Get('Article', 'int', 0); if (!Input::IsValid()) { echo "Input Error: Missing input"; return; } // Unzip the sxw file to get the content. $zip = zip_open($files["filename"]["tmp_name"]); if ($zip) { $xml = null; while ($zip_entry = zip_read($zip)) { if (zip_entry_name($zip_entry) == "content.xml") { if (zip_entry_open($zip, $zip_entry, "r")) { $xml = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); zip_entry_close($zip_entry); } } } zip_close($zip); if (!is_null($xml)) { // Write the XML to a file because the XSLT functions // require it to be in a file in order to be processed. $tmpXmlFilename = tempnam("/tmp", "ArticleImportXml"); $tmpXmlFile = fopen($tmpXmlFilename, "w"); fwrite($tmpXmlFile, $xml); fclose($tmpXmlFile); // Transform the OpenOffice document to DocBook format. $xsltProcessor = xslt_create(); $docbookXml = xslt_process($xsltProcessor, $tmpXmlFilename, "sxwToDocbook.xsl"); unlink($tmpXmlFilename); // Parse the docbook to get the data. $docBookParser = new DocBookParser(); $docBookParser->parseString($docbookXml, true); $article = new Article($articleNumber, $language); $article->setTitle($docBookParser->getTitle()); $article->setIntro($docBookParser->getIntro()); $article->setBody($docBookParser->getBody()); // Go back to the "Edit Article" page. header("Location: /$ADMIN/articles/edit.php?Pub=$publication&Issue=$issue&Section=$section&Article=$articleNumber&Language=$language&sLanguage=$sLanguage"); } // if (!is_null($xml)) } // if ($zip) // Some sort of error occurred - show the upload page again. include("index.php"); } // fn upload_article_handler
/** * Unzip the source_file in the destination dir * * @param string The path to the ZIP-file. * @param string The path where the zipfile should be unpacked, if false the directory of the zip-file is used * @param boolean Indicates if the files will be unpacked in a directory with the name of the zip-file (true) or not (false) (only if the destination directory is set to false!) * @param boolean Overwrite existing files (true) or not (false) * * @return boolean Succesful or not */ function unzip($src_file, $dest_dir = false, $create_zip_name_dir = true, $overwrite = true) { if (function_exists("zip_open")) { if (!is_resource(zip_open($src_file))) { $src_file = dirname($_SERVER['SCRIPT_FILENAME']) . "/" . $src_file; } if (is_resource($zip = zip_open($src_file))) { $splitter = $create_zip_name_dir === true ? "." : "/"; if ($dest_dir === false) { $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter)) . "/"; } // Create the directories to the destination dir if they don't already exist $this->create_dirs($dest_dir); // For every file in the zip-packet while ($zip_entry = zip_read($zip)) { // Now we're going to create the directories in the destination directories // If the file is not in the root dir $pos_last_slash = strrpos(zip_entry_name($zip_entry), "/"); if ($pos_last_slash !== false) { // Create the directory where the zip-entry should be saved (with a "/" at the end) $this->create_dirs($dest_dir . substr(zip_entry_name($zip_entry), 0, $pos_last_slash + 1)); } // Open the entry if (zip_entry_open($zip, $zip_entry, "r")) { // The name of the file to save on the disk $file_name = $dest_dir . zip_entry_name($zip_entry); // Check if the files should be overwritten or not if ($overwrite === true || $overwrite === false && !is_file($file_name)) { // Get the content of the zip entry $fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); if (!is_dir($file_name)) { file_put_contents($file_name, $fstream); } // Set the rights if (file_exists($file_name)) { chmod($file_name, 0777); echo "<span style=\"color:#1da319;\">file saved: </span>" . $file_name . "<br />"; } else { echo "<span style=\"color:red;\">file not found: </span>" . $file_name . "<br />"; } } // Close the entry zip_entry_close($zip_entry); } } // Close the zip-file zip_close($zip); } else { echo "No Zip Archive Found."; return false; } return true; } else { if (version_compare(phpversion(), "5.2.0", "<")) { $infoVersion = "(use PHP 5.2.0 or later)"; } echo "You need to install/enable the php_zip.dll extension {$infoVersion}"; } }
/** * les deux fonction suivante ont été récupéré dans les exemples de php.net puis adapté pour leed. * * Unzip the source_file in the destination dir * * @param string The path to the ZIP-file. * @param string The path where the zipfile should be unpacked, if false the directory of the zip-file is used * @param boolean Indicates if the files will be unpacked in a directory with the name of the zip-file (true) or not (false) (only if the destination directory is set to false!) * @param boolean Overwrite existing files (true) or not (false) * * @return boolean Succesful or not */ function unzip_leed($src_file, $dest_dir = false, $create_zip_name_dir = true, $overwrite = true) { if ($zip = zip_open($src_file)) { if ($zip) { $splitter = $create_zip_name_dir === true ? "." : "/"; if ($dest_dir === false) { $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter)) . "/"; } // Create the directories to the destination dir if they don't already exist create_dirs($dest_dir); // For every file in the zip-packet while ($zip_entry = zip_read($zip)) { // Now we're going to create the directories in the destination directories // If the file is not in the root dir $pos_last_slash = strrpos(zip_entry_name($zip_entry), "/"); if ($pos_last_slash !== false) { // Create the directory where the zip-entry should be saved (with a "/" at the end) $interne_dir = str_replace("Leed-master/", "", substr(zip_entry_name($zip_entry), 0, $pos_last_slash + 1)); $interne_dir = str_replace("Leed-multi_user/", "", $interne_dir); $interne_dir = str_replace("Leed-market-master/", "", $interne_dir); $interne_dir = str_replace("Leed-market-multi_user/", "", $interne_dir); $interne_dir = str_replace("Leed-dev/", "", $interne_dir); create_dirs($dest_dir . $interne_dir); } // Open the entry if (zip_entry_open($zip, $zip_entry, "r")) { // The name of the file to save on the disk $file_name = $dest_dir . zip_entry_name($zip_entry); // Check if the files should be overwritten or not if ($overwrite === true || $overwrite === false && !is_file($file_name)) { // Get the content of the zip entry $fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); $file_name = str_replace("Leed-master/", "", $file_name); $file_name = str_replace("Leed-multi_user/", "", $file_name); $file_name = str_replace("Leed-market-master/", "", $file_name); $file_name = str_replace("Leed-market-multi_user/", "", $file_name); $file_name = str_replace("Leed-dev/", "", $file_name); if (is_dir($file_name)) { echo "répertoire: " . $file_name . "<br />"; } else { if (file_put_contents($file_name, $fstream) === false) { echo "erreur copie: " . $file_name . "<br />"; } else { echo "copie: " . $file_name . "<br />"; } } } // Close the entry zip_entry_close($zip_entry); } } // Close the zip-file zip_close($zip); } } else { return false; } return true; }
/** * Gets zip file contents * * @param string $file zip file * @param string $specific_entry regular expression to match a file * * @return array ($error_message, $file_data); $error_message * is empty if no error */ function PMA_getZipContents($file, $specific_entry = null) { $error_message = ''; $file_data = ''; $zip_handle = zip_open($file); if (!is_resource($zip_handle)) { $error_message = __('Error in ZIP archive:') . ' ' . PMA_getZipError($zip_handle); zip_close($zip_handle); return array('error' => $error_message, 'data' => $file_data); } $first_zip_entry = zip_read($zip_handle); if (false === $first_zip_entry) { $error_message = __('No files found inside ZIP archive!'); zip_close($zip_handle); return array('error' => $error_message, 'data' => $file_data); } /* Is the the zip really an ODS file? */ $read = zip_entry_read($first_zip_entry); $ods_mime = 'application/vnd.oasis.opendocument.spreadsheet'; if (!strcmp($ods_mime, $read)) { $specific_entry = '/^content\\.xml$/'; } if (!isset($specific_entry)) { zip_entry_open($zip_handle, $first_zip_entry, 'r'); /* File pointer has already been moved, * so include what was read above */ $file_data = $read; $file_data .= zip_entry_read($first_zip_entry, zip_entry_filesize($first_zip_entry)); zip_entry_close($first_zip_entry); zip_close($zip_handle); return array('error' => $error_message, 'data' => $file_data); } /* Return the correct contents, not just the first entry */ for (;;) { $entry = zip_read($zip_handle); if (is_resource($entry)) { if (preg_match($specific_entry, zip_entry_name($entry))) { zip_entry_open($zip_handle, $entry, 'r'); $file_data = zip_entry_read($entry, zip_entry_filesize($entry)); zip_entry_close($entry); break; } } else { /** * Either we have reached the end of the zip and still * haven't found $specific_entry or there was a parsing * error that we must display */ if ($entry === false) { $error_message = __('Error in ZIP archive:') . ' Could not find "' . $specific_entry . '"'; } else { $error_message = __('Error in ZIP archive:') . ' ' . PMA_getZipError($zip_handle); } break; } } zip_close($zip_handle); return array('error' => $error_message, 'data' => $file_data); }
function extractZip($zipFile, $extract_path, $remove_path = '', $blacklist = '', $whitelist = '') { if ($zipFile == '' or $extract_path == '') { return false; } if (!file_exists($zipFile)) { return false; } $zip = zip_open($zipFile); $remove_path = addcslashes($remove_path, "/"); if (is_resource($zip)) { $i = 0; $extracted_files = array(); while ($zip_entry = zip_read($zip)) { $filename = zip_entry_name($zip_entry); $file_path = preg_replace("/{$remove_path}/", "", $filename); $dir_path = preg_replace("/{$remove_path}/", "", dirname($filename)); if (isset($blacklist) and is_array($blacklist) and in_array($file_path, $blacklist)) { continue; } if (isset($whitelist) and is_array($whitelist) and !in_array($filename, $whitelist)) { continue; } $completePath = $extract_path . $dir_path; $completeName = $extract_path . $file_path; // Walk through path to create non existing directories // This won't apply to empty directories ! They are created further below if (!file_exists($completePath) && preg_match('/^' . $remove_path . '/', dirname(zip_entry_name($zip_entry)))) { $tmp = PHP_OS == "WINNT" ? "" : DIRECTORY_SEPARATOR; foreach (explode('/', $completePath) as $k) { if ($k != "") { $tmp .= $k . DIRECTORY_SEPARATOR; if (!file_exists($tmp)) { mkdir($tmp, 0777); } } } } if (zip_entry_open($zip, $zip_entry, "r")) { if (preg_match('/^' . $remove_path . '/', dirname(zip_entry_name($zip_entry)))) { if (!preg_match("/\\/\$/", $completeName)) { if ($fd = fopen($completeName, 'w+')) { fwrite($fd, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry))); fclose($fd); $extracted_files[$i]['filename'] = zip_entry_name($zip_entry); $i++; } } } zip_entry_close($zip_entry); } } zip_close($zip); return $extracted_files; } return false; }
public function current() { if (!zip_entry_open($this->resource, $this->entry)) { throw new SException("Zip file entry can not be read"); } $buffer = zip_entry_read($this->entry, zip_entry_filesize($this->entry)); zip_entry_close($this->entry); return $buffer; }
function unzip($dir, $file, $verbose = 0) { $name = substr($file, 0, strrpos($file, '.')); $extension = substr($file, strrpos($file, '.')); $dir_path = "{$dir}{$name}"; $zip_path = "{$dir}{$file}"; $ERROR_MSGS[0] = "OK"; $ERROR_MSGS[1] = "Zip path {$zip_path} doesn't exists."; $ERROR_MSGS[2] = "Directory {$dir_path} for unzip the pack already exists, impossible continue."; $ERROR_MSGS[3] = "Error while opening the {$zip_path} file."; $ERROR = 0; echo "inside unzip method" . "\n dir = " . $dir_path . " zippath = " . $zip_path; if (file_exists($zip_path)) { echo "status: file exists " . $zip_path . "\n"; if (!file_exists($dir_path)) { echo "making directory " . $dir_path . "\n"; mkdir($dir_path); } if ($link = zip_open($zip_path)) { echo "inside if open"; while (($zip_entry = zip_read($link)) && !$ERROR) { if (zip_entry_open($link, $zip_entry, "r")) { $data = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); $dir_name = dirname(zip_entry_name($zip_entry)); $name = zip_entry_name($zip_entry); if ($name[strlen($name) - 1] == '/') { $base = "{$dir_path}/"; foreach (explode("/", $name) as $k) { $base .= "{$k}/"; if (!file_exists($base)) { mkdir($base); } } } else { $name = "{$dir_path}/{$name}"; #if ($verbose) echo "\n extracting: {$name}<br>"; $stream = fopen($name, "w"); fwrite($stream, $data); } zip_entry_close($zip_entry); } else { $ERROR = 4; } } zip_close($link); } else { $ERROR = "3"; } } else { $ERROR = 2; } #} #else # $ERROR = 1; return $ERROR_MSGS[$ERROR]; }
/** * @desc This method gets the required XML snippets from the file, and extracts the image assets * @return $this, modifies $this->xml, $this->assets */ public function import() { $imageAssets = array(); $zip = zip_open($this->wordUri); while ($zipEntry = zip_read($zip)) { $entryName = zip_entry_name($zipEntry); if (zip_entry_open($zip, $zipEntry) == FALSE) { continue; } # /media/ contains all assets if (strpos($entryName, 'word/media') !== false) { # Removes 'word/media' prefix $imageName = substr($entryName, 11); # Prevent EMF file extensions passing, as they are used by word rather than being manually placed if (!$this->_allowEmfImages) { if (substr($imageName, -3) == 'emf') { continue; } } # Place the image assets into an array for future reference $imageAssets[$imageName] = array('h' => 'auto', 'w' => 'auto', 'title' => $imageName, 'id' => null, 'data' => base64_encode(zip_entry_read($zipEntry, zip_entry_filesize($zipEntry)))); } # Get the image relationship xml structure if ($entryName == 'word/_rels/document.xml.rels') { $this->xml['image'] = zip_entry_read($zipEntry, zip_entry_filesize($zipEntry)); } # Get the document structure if ($entryName == 'word/document.xml') { $this->xml['structure'] = zip_entry_read($zipEntry, zip_entry_filesize($zipEntry)); } zip_entry_close($zipEntry); } zip_close($zip); # Apply relId's to the image array, so they can be attached to the structure if (isset($this->xml['image'])) { $dom = new \DOMDocument(); $dom->loadXML($this->xml['image'], LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING); $dom->encoding = 'utf-8'; $elements = $dom->getElementsByTagName('*'); foreach ($elements as $node) { if ($node->nodeName == 'Relationship') { $relationshipAttributes = $node->attributes; $relationId = $relationshipAttributes->item(0); $relationTarget = $relationshipAttributes->item(2); if (is_object($relationId) && is_object($relationTarget)) { if (strpos($relationTarget->nodeValue, 'media/') !== false) { $imageName = substr($relationTarget->nodeValue, 6); $imageAssets[$imageName]['id'] = $relationId->nodeValue; } } } } $this->images = $imageAssets; } return $this; }
function mediashareSourceZipAddFile(&$zip, &$zipEntry, &$args) { $dom = ZLanguage::getModuleDomain('mediashare'); // Read zip info and file data into buffer $zipSize = zip_entry_filesize($zipEntry); $zipName = zip_entry_name($zipEntry); if (!zip_entry_open($zip, $zipEntry, 'rb')) { return array(array('ok' => false, 'message' => __f('Could not open the ZIP: %s', "{$zipName}", $dom))); } $buffer = zip_entry_read($zipEntry, $zipSize); zip_entry_close($zipEntry); // Ensure sub-folder exists // Split name by slashes into folders and filename and create/verify the folders recursively $folders = explode('/', $zipName); $albumId = $args['albumId']; if (!($subFolderID = mediashareEnsureFolderExists($albumId, $folders, 0))) { return false; } $args['albumId'] = $subFolderID; // Get actual filename from folderlist (last item in the array) $imageName = $folders[sizeof($folders) - 1]; // Create tmp. file and copy image data into it $tmpdir = pnModGetVar('mediashare', 'tmpDirName'); $tmpfilename = tempnam($tmpdir, 'IMG'); if (!($f = fopen($tmpfilename, 'wb'))) { @unlink($tmpfilename); return false; } fwrite($f, $buffer); fclose($f); $args['mimeType'] = ''; if (function_exists('mime_content_type')) { $args['mimeType'] = mime_content_type($tmpfilename); if (empty($args['mimeType'])) { $args['mimeType'] = mime_content_type($imageName); } } if (empty($args['mimeType'])) { $args['mimeType'] = mediashareGetMimeType($imageName); } $args['uploadFilename'] = $tmpfilename; $args['fileSize'] = $zipSize; $args['filename'] = $imageName; $args['keywords'] = null; $args['description'] = null; // Create image (or add recursively zip archive) $result = pnModAPIFunc('mediashare', 'source_zip', 'addMediaItem', $args); if ($result === false) { $status = array('ok' => false, 'message' => LogUtil::getErrorMessagesText()); } else { $status = array('ok' => true, 'message' => $result['message'], 'mediaId' => $result['mediaId']); } $args['albumId'] = $albumId; return $status; }
/** * Unzip the source_file in the destination dir * * @param string The path to the ZIP-file. * @param string The path where the zipfile should be unpacked, if false the directory of the zip-file is used * @param boolean Indicates if the files will be unpacked in a directory with the name of the zip-file (true) or not (false) (only if the destination directory is set to false!) * @param boolean Overwrite existing files (true) or not (false) * * @return boolean Succesful or not */ function unzip($src_file, $dest_dir = false, $create_zip_name_dir = true, $overwrite = true) { if (!@function_exists('zip_open')) { return false; } if (!is_resource(zip_open($src_file))) { $src_file = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . $src_file; } if (!is_resource($zip = zip_open($src_file))) { return false; } $splitter = $create_zip_name_dir === true ? '.' : '/'; if ($dest_dir === false) { $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter)) . '/'; } // Create the directories to the destination dir if they don't already exist create_dirs($dest_dir); $response_folder = ''; // For every file in the zip-packet while ($zip_entry = zip_read($zip)) { // Now we're going to create the directories in the destination directories // If the file is not in the root dir $pos_last_slash = strrpos(zip_entry_name($zip_entry), '/'); if ($pos_last_slash !== false) { // Create the directory where the zip-entry should be saved (with a "/" at the end) $entry_folder = substr(zip_entry_name($zip_entry), 0, $pos_last_slash + 1); create_dirs($dest_dir . $entry_folder); if ($response_folder == '') { $response_folder = $entry_folder; } } // Open the entry if (zip_entry_open($zip, $zip_entry, 'r')) { // The name of the file to save on the disk $file_name = $dest_dir . zip_entry_name($zip_entry); // Check if the files should be overwritten or not if ($overwrite === true || $overwrite === false && !is_file($file_name)) { // Get the content of the zip entry $fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); if (!is_dir($file_name)) { file_put_contents($file_name, $fstream); } // Set the rights if (file_exists($file_name)) { chmod($file_name, 0777); } } // Close the entry zip_entry_close($zip_entry); } } // Close the zip-file zip_close($zip); return $response_folder; }
/** * @synopsis <wordpress|genesis> <version> * @subcommand wphash * */ function wordpresshash($args, $assoc_args) { $filecount = 0; $hashes = ""; $version = $args[1]; $is_wordpress = false; if ($args[0] == "wordpress") { WP_CLI::launch("wget -c -q http://wordpress.org/wordpress-" . $version . ".zip -P /tmp/ || echo -n"); $is_wordpress = true; } elseif ($args[0] == "genesis") { WP_CLI::launch("wget -c -q http://genesistheme.com/download/sdf98h9p08hsdf009jsdf/genesis." . $version . ".zip -P /tmp/ || echo -n"); } else { WP_CLI::warning("command usage: wp synth-ops wphash <wordpress|genesis> <version>"); exit; } $zip_file = $is_wordpress ? "/tmp/wordpress-" . $version . ".zip" : "/tmp/genesis." . $version . ".zip"; if (!file_exists($zip_file)) { WP_CLI::warning("Wrong " . $args[0] . " version OR server not available at this moment."); exit; } $zip = zip_open($zip_file); if (is_resource($zip)) { while ($zip_entry = zip_read($zip)) { zip_entry_open($zip, $zip_entry, "r"); $filename = null; if (substr(zip_entry_name($zip_entry), -1) == "/") { continue; } if (preg_match("/(farbtastic|install)-rtl\\.css/", zip_entry_name($zip_entry))) { continue; } if ($is_wordpress && preg_match("/(wp-admin|wp-includes)\\//", zip_entry_name($zip_entry))) { $filename = preg_replace("/^wordpress\\//", "", zip_entry_name($zip_entry)); } else { if (!$is_wordpress) { $filename = zip_entry_name($zip_entry); } } if (!is_null($filename)) { $hashes .= serialize($filename) . serialize(md5(zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)))); $filecount++; } zip_entry_close($zip_entry); } $hashes = "a:" . $filecount . ":{" . $hashes . "}"; $prefix = $is_wordpress ? "wp" : "genesis"; WP_CLI::launch("chmod -R 644 " . SYNTHESIS_CHILD_PLUGIN_DIR . "hashfiles/"); WP_CLI::launch("echo '" . $hashes . "' > " . SYNTHESIS_CHILD_PLUGIN_DIR . "hashfiles/" . $prefix . $version . ".php"); # uncomment to use the same owner than SYNTHESIS_CHILD_PLUGIN_DIR to the hashfiles directory #WP_CLI::launch( "chown -R $(stat -c %U " . SYNTHESIS_CHILD_PLUGIN_DIR . "):$(stat -c %U " . SYNTHESIS_CHILD_PLUGIN_DIR . ") " . SYNTHESIS_CHILD_PLUGIN_DIR . "hashfiles/" ); WP_CLI::launch("chmod -R 400 " . SYNTHESIS_CHILD_PLUGIN_DIR . "hashfiles/"); WP_CLI::success($args[0] . " " . $version . " hash file has been generated."); } zip_close($zip); }
public function _theme_installation() { $this->extension('json'); if (!empty($_FILES['theme']) && extension($_FILES['theme']['name']) == 'zip') { if ($zip = zip_open($_FILES['theme']['tmp_name'])) { $theme_name = NULL; while ($zip_entry = zip_read($zip)) { $entry_name = zip_entry_name($zip_entry); $is_dir = substr($entry_name, -1) == '/'; if (is_null($theme_name) && $is_dir) { $theme_name = substr($entry_name, 0, -1); } if ($theme_name && strpos($entry_name, $theme_name . '/') === 0) { if ($is_dir) { mkdir('./themes/' . $entry_name, 0777, TRUE); } else { if (zip_entry_open($zip, $zip_entry, 'r')) { $content = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); if ($entry_name == $theme_name . '/' . $theme_name . '.php' && file_exists('./themes/' . $entry_name)) { if (preg_match('/\\$version[ \\t]*?=[ \\t]*?([\'"])(.+?)\\1;/', $content, $match)) { $old_version = preg_replace('/[^\\d.]/', '', $this->load->theme($theme_name, FALSE)->version); $new_version = preg_replace('/[^\\d.]/', '', $match[2]); if ($cmp = version_compare($new_version, $old_version)) { $update = TRUE; } else { zip_entry_close($zip_entry); return json_encode(array('error' => $this($cmp == 0 ? 'already_installed_version' : 'not_newer_installed_version'))); } } } file_put_contents('./themes/' . $entry_name, $content); zip_entry_close($zip_entry); } } } } zip_close($zip); if ($theme_name && ($theme = $this->load->theme($theme_name, FALSE))) { if (empty($update)) { $theme->uninstall()->install(); } else { $this->db->insert('nf_settings_addons', array('name' => $theme_name, 'type' => 'theme', 'enable' => TRUE)); } return json_encode(array('success' => TRUE)); } else { if ($theme_name) { rmdir_all('./themes/' . $theme_name); } return json_encode(array('error' => $this('error_theme_install'))); } } } return json_encode(array('error' => $this('zip_file_required'))); }
public static function unzip($src_file, $dest_dir = false, $create_zip_name_dir = true, $overwrite = true) { if (!extension_loaded('zip')) { if (!dl('zip.so')) { exit('Zip Module could not be loaded.'); } } $files = array(); // Look for the resource and try something else... if (!is_resource(zip_open($src_file))) { $src_file = dirname($_SERVER['SCRIPT_FILENAME']) . "/" . $src_file; } if (!is_resource($zip = zip_open($src_file))) { return false; // No zip file found. } $splitter = $create_zip_name_dir === true ? "." : "/"; if ($dest_dir === false) { $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter)) . "/"; } // Create the directories to the destination dir if they don't already exist self::create_dirs($dest_dir); // For every file in the zip-packet while ($zip_entry = zip_read($zip)) { // If the file is not in the root dir $pos_last_slash = strrpos(zip_entry_name($zip_entry), "/"); if ($pos_last_slash !== false) { // Create the directory where the zip-entry should be saved (with a "/" at the end) self::create_dirs($dest_dir . substr(zip_entry_name($zip_entry), 0, $pos_last_slash + 1)); } // Open the entry if (zip_entry_open($zip, $zip_entry, "r")) { // The name of the file to save on the disk $file_name = $dest_dir . zip_entry_name($zip_entry); // Check if the files should be overwritten or not if ($overwrite === true || $overwrite === false && !is_file($file_name)) { // Get the content of the zip entry $fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); if (!is_dir($file_name)) { file_put_contents($file_name, $fstream); if (file_exists($file_name)) { chmod($file_name, 0777); $files[] = $file_name; } } // Close the entry zip_entry_close($zip_entry); } } } // Close the zip file zip_close($zip); return $files; }
/** * Extract a ZIP compressed file to a given path * @param string $archive Path to ZIP archive to extract * @param string $destination Path to extract archive into * @return boolean True if successful */ public static function extractZip($archive, $destination) { if (!function_exists('zip_open')) { throw new Exception("ZIP extension is not enabled on this installation of PHP. Recompile your installation of PHP with --enable-zip parameter."); } // echo "Extracting archive " . $archive . " to " . $destination. "<br>"; $zip = zip_open($archive); if (is_resource($zip)) { // Create the destination folder if (!mkdir($destination)) { self::$strLastError = "Unable to create extraction destination folder " . $destination; return false; } // Read files in the archive $createdFolders = array(); while ($file = zip_read($zip)) { if (is_resource($file)) { if (zip_entry_open($zip, $file, "r")) { if (substr(zip_entry_name($file), strlen(zip_entry_name($file)) - 1) != "/") { // echo zip_entry_name($file) . "<br>"; $folderStack = explode("/", zip_entry_name($file)); if (sizeof($folderStack) > 1) { for ($i = 0; $i < sizeof($folderStack) - 1; $i++) { $arraySubsection = array_slice($folderStack, 0, $i + 1); $item = implode("/", $arraySubsection); if (!in_array($item, $createdFolders)) { // echo "- Creating folder: " . $destination . $item . "<br>"; $createdFolders[] = $item; mkdir($destination . $item); } } } $strSectionToAppend = zip_entry_read($file, zip_entry_filesize($file)); $strSavePath = $destination . zip_entry_name($file); QFile::writeFile($strSavePath, $strSectionToAppend); zip_entry_close($file); } } else { self::$strLastError = "Unable to read zip entry"; return false; } } else { self::$strLastError = self::zipFileErrMsg($file); return false; } } zip_close($zip); } else { self::$strLastError = self::zipFileErrMsg($zip); return false; } return true; }
/** * Gets zip file contents * * @param string $file * @return array ($error_message, $file_data); $error_message * is empty if no error * @author lem9 */ function PMA_getZipContents($file) { $error_message = ''; $file_data = ''; $zip_handle = zip_open($file); if (is_resource($zip_handle)) { $first_zip_entry = zip_read($zip_handle); if (false === $first_zip_entry) { $error_message = $GLOBALS['strNoFilesFoundInZip']; } else { /* Is the the zip really an ODS file? */ $read = zip_entry_read($first_zip_entry); $ods_mime = 'application/vnd.oasis.opendocument.spreadsheet'; if (!strcmp($ods_mime, $read)) { /* Return the correct contents, not just the first entry */ for (;;) { $entry = zip_read($zip_handle); if (is_resource($entry)) { if (!strcmp('content.xml', zip_entry_name($entry))) { zip_entry_open($zip_handle, $entry, 'r'); $file_data = zip_entry_read($entry, zip_entry_filesize($entry)); zip_entry_close($entry); break; } } else { /** * Either we have reached the end of the zip and still * haven't found 'content.xml' or there was a parsing * error that we must display */ if ($entry === FALSE) { $error_message = $GLOBALS['strErrorInZipFile'] . ' Could not find "content.xml"'; } else { $error_message = $GLOBALS['strErrorInZipFile'] . ' ' . PMA_getZipError($zip_handle); } break; } } } else { zip_entry_open($zip_handle, $first_zip_entry, 'r'); /* File pointer has already been moved, so include what was read above */ $file_data = $read; $file_data .= zip_entry_read($first_zip_entry, zip_entry_filesize($first_zip_entry)); zip_entry_close($first_zip_entry); } } } else { $error_message = $GLOBALS['strErrorInZipFile'] . ' ' . PMA_getZipError($zip_handle); } zip_close($zip_handle); return array('error' => $error_message, 'data' => $file_data); }
private function browseZip() { $zip = zip_open($this->zip); $files = array(); $this->nameZip = NULL; while ($elementZip = zip_read($zip)) { $elementName = zip_entry_name($elementZip); $files[] = array($elementZip, $elementName); zip_entry_close($elementZip); } zip_close($zip); return $files; }
/** * 读取docx文档(word文档) * @param string $filename 文档绝对路径 * @return string|bool */ public static function readDocx($filename) { set_time_limit(0); $striped_content = ''; $content = ''; $ext = strtolower(substr(strrchr($filename, '.'), 1)); if ($ext != 'docx') { return ''; } if (!$filename || !file_exists($filename)) { return ''; } $zip = zip_open($filename); if (!$zip || is_numeric($zip)) { return ''; } while ($zip_entry = zip_read($zip)) { if (zip_entry_open($zip, $zip_entry) == FALSE) { continue; } if (zip_entry_name($zip_entry) != "word/document.xml") { continue; } $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); zip_entry_close($zip_entry); } zip_close($zip); $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content); $content = str_replace('</w:r></w:p>', "\r\n", $content); $striped_content = strip_tags($content); return $striped_content; }