Home Backend Development PHP Tutorial php关于zip的应用

php关于zip的应用

Jun 13, 2016 am 10:54 AM
path zip

php关于zip的使用

/**	 * 	 * 创建压缩包	 * @param array $arr 需要压缩的文件部分路径和文件名,形如 array(	 *   '/file/upload/1.php',	 *   '/file/upload/2011/1.jpg',	 * )	 * 	 * @return string 压缩包的部分路径和文件名,形如/file/uplaod/zip/123.zip	 */	public static function zip($arr)	{	    //首先我建立一个目录	    $save_path = '/file/upload/zip' . Dat::getHourPath();	    $path = APPLICATION_PATH . '/destoon' . $save_path;	    Sys::createDir($path);	    	    $zipname = strval(time()) . mt_rand(100000, 999999) . '.zip';	    $zip_fullname = $path . '/' . $zipname;	    $zip_obj = new ZipArchive();        if ($zip_obj->open($zip_fullname, ZIPARCHIVE::CREATE) !== true) {             return false;         }         foreach ($arr as $value) {         //  $zip->addFile($path, mb_convert_encoding ( basename($path), 'GBK'  ,'UTF-8'));             $zip_obj->addFile( APPLICATION_PATH . '/destoon' . $value, basename($value) );         }         $zip_obj->close();        return $save_path . '/' . $zipname;	    	}		/**	 * 解压缩	 * 	 * @param string $zipname 压缩包的部分路径和文件名,形如'/file/upload/1.zip'	 * @param string $path 解压后的文件夹全路径	 *      * @return array 解压后的文件数组,部分路径和文件名,形如array(     *     '/file/1.php',     *     '/file/12.png',     * )	 */	public static function unzip($zipname , $path='')	{	    if (!$path ) {	        $path = '/file/tempzip_folder/' .               strval(time()) . mt_rand(100000, 999999);	                $newpath = APPLICATION_PATH . '/destoon' . $path; 	    }	    else {	        $newpath = $path;	        $path = preg_replace('#^.+/destoon(.+)$#', '$1', $newpath);	    }	            Sys::createDir($newpath);                    $filename = APPLICATION_PATH . '/destoon' . $zipname;        //第一件事是解压        $zip = new ZipArchive();        $arr = array();        if ($zip->open($filename) === true) {            $zip->extractTo($newpath);            $zip->close();                        $temp = scandir($newpath);            foreach ($temp as $value) {                if ($value != '.' && $value != '..') {                    $arr[] = $path . '/' . basename($value);                }            }        }        return $arr;	    	}			
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Steps to set the PATH environment variable of the Linux system Steps to set the PATH environment variable of the Linux system Feb 18, 2024 pm 05:40 PM

How to set the PATH environment variable in Linux systems In Linux systems, the PATH environment variable is used to specify the path where the system searches for executable files on the command line. Correctly setting the PATH environment variable allows us to execute system commands and custom commands at any location. This article will introduce how to set the PATH environment variable in a Linux system and provide detailed code examples. View the current PATH environment variable. Execute the following command in the terminal to view the current PATH environment variable: echo$P

Best Guide to Compressing HTML Files to ZIP Best Guide to Compressing HTML Files to ZIP Apr 09, 2024 pm 04:09 PM

Compressing HTML files into ZIP can improve page loading speed. Methods include: using online tools (such as FileOptimizer, TinyPNG) using command line tools (such as gzip, 7-zip) using Node.js scripts (using the zlib module)

How to use linux compression zip command How to use linux compression zip command Oct 08, 2023 pm 01:25 PM

The zip command is a very useful compression tool in Linux systems. By using the zip command, you can easily compress files and directories into a zip file and save storage space and facilitate transfer. The basic syntax of the zip command is "zip [options] [compressed file name] [file or directory to be compressed]".

Detailed explanation of decompression file command (zip) under centos7 Detailed explanation of decompression file command (zip) under centos7 Jan 07, 2024 pm 06:30 PM

1. The compressed folder is a zip file [root@cgls]#zip-rmydata.zipmydata2. Unzip mydata.zip into the mydatabak directory [root@cgls]#unzipmydata.zip-dmydatabak3.mydata01 folder and mydata02.txt are compressed into mydata.zip[root@cgls]#zipmydata.zipmydata01mydata02.txt4. Decompress the mydata.zip file directly [root@cgls]#unzipmydata.zip5. View myd

How to set the path environment variable How to set the path environment variable Sep 04, 2023 am 11:53 AM

Method to set the path environment variable: 1. Windows system, open "System Properties", click the "Properties" option, click "Advanced System Settings", in the "System Properties" window, select the "Advanced" tab, and then click "Environment Variables" " button, find and click "Path" to edit and save; 2. For Linux systems, open the terminal, open your bash configuration file, add "export PATH=$PATH: file path" at the end of the file and save it; 3. For MacOS system, the operation is the same as above.

Which one is lossless, 7z or zip? Which one is lossless, 7z or zip? Jan 19, 2021 pm 06:37 PM

7z and zip are both lossless compressions. 7z is a mainstream and efficient compression format with a very high compression ratio; the ZIP file format is a file format for data compression and document storage. 7z has a higher compression ratio, followed by zip; the zip format is more common and has a wide range of technologies, and the windows operating system supports the zip format by default.

How to use Zip function in Java for file compression How to use Zip function in Java for file compression Jun 26, 2023 pm 02:10 PM

Compressing files is a common operation that can save disk space and network transmission time, and Java provides the Zip function for file compression. This article will show how to use the Zip function in Java for file compression through a detailed introduction and example demonstration. 1. Introduction to the Zip function The Zip function is a compression and packaging tool library provided in Java. This function can be used to compress files or folders into a Zip format file. ZipOutputStr is mainly used in the Zip function

How to correctly set the PATH environment variable in Linux How to correctly set the PATH environment variable in Linux Feb 22, 2024 pm 08:57 PM

How to correctly set the PATH environment variable in Linux In the Linux operating system, environment variables are one of the important mechanisms used to store system-level configuration information. Among them, the PATH environment variable is used to specify the directories in which the system searches for executable files. Correctly setting the PATH environment variable is a key step to ensure the normal operation of the system. This article will introduce how to correctly set the PATH environment variable in Linux and provide specific code examples. 1. Check the current PATH environment variable and enter the following command in the terminal

See all articles