php Operation zip Displays the picture in the compressed package without uncompressing the package

  • 2021-08-21 19:56:55
  • OfStack

PHP operation ZIP compressed package file basic method everyone should know how to do (if you don't know the basic method of PHP operation ZIP at the bottom of this article), then how to directly read the compressed package file without decompression, such as directly displaying the compressed package picture in the page?

The following will show you how to show the picture file in the compressed package directly without decompressing under 1


$zip_path = '12.zip';// Actual path of compressed packet 
$zip = zip_open($zip_path);// Utilization zip_open Function to open the compressed package 
while ($re = zip_read($zip)) {// Read the files in the package in turn 
 if (!zip_entry_filesize($re)) break;// If the file size is 0 Exit operation 
 $entry_zp = zip_entry_open($zip,$re,'rb');// Read the files in the package 
 $ext = pathinfo(zip_entry_name ($re),PATHINFO_EXTENSION);// Get the picture file extension 
 $buf = zip_entry_read($re,zip_entry_filesize($re));// Read a file 2 Binary data 
 echo sprintf('<img src="data:image/%s;base64,%s">', $ext, base64_encode($buf));// Utilization base64_encode Function converts the read to 2 Binary data and input and output to the page 
 zip_entry_close($re);// Close a file in an open compressed package 
}
zip_close($zip);// Close the compressed package file 

The most important part of this code is to use base64_encode to convert the binary data of the picture file into the browser-readable Base64 picture data

Note: When using the ZIP FILE family functions of PHP, make sure that the php_zip. dll extension library is open in your PHP. ini file and that php_zip. dll is available in the ext folder in the PHP installation directory (I am using the Windows system). If php_zip. dll does not exist, it can be obtained from the following PHP official PECL expansion package station

PHP Official PECL Expansion Packet Station Address: http://pecl.php.net/package/zip

Select the compressed package corresponding to your PHP version;
Under Windows, you can directly drag and drop the php_zip. dll file in the compressed package to the ext folder in the PHP installation directory.
The Linux system needs to be compiled before modifying the configuration file of PHP

Attached is the basic function of PHP to operate Zip File

函数 描述 PHP
zip_close() 关闭 ZIP 文件。 4
zip_entry_close() 关闭 ZIP 文件中的1个项目。 4
zip_entry_compressedsize() 返回 ZIP 文件中的1个项目的被压缩尺寸。 4
zip_entry_compressionmethod() 返回 ZIP 文件中的1个项目的压缩方法。 4
zip_entry_filesize() 返回 ZIP 文件中的1个项目的实际文件尺寸。 4
zip_entry_name() 返回 ZIP 文件中的1个项目的名称。 4
zip_entry_open() 打开 ZIP 文件中的1个项目以供读取。 4
zip_entry_read() 读取 ZIP 文件中的1个打开的项目。 4
zip_open() 打开 ZIP 文件。 4
zip_read() 读取 ZIP 文件中的下1个项目。 4


Related articles: