Một PHP class tạo file ZIP

September 15th, 2008

Tôi đang phải làm  chức năng nén/giải nén trong dự án hiện tại. Giải nén thì đã làm xong rồi, còn nén nữa là xong. Zip library của PHP hỗ trợ chức năng zip hơi bị kém, cái cơ bản nhất là cũng ngại phải viết => search & thấy class này khá nhẹ để nén 1 folder.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
	require ("zipfile.inc.php");
 
	$zipfile = new zipfile();
 
	rec_listFiles('myFolder');
 
	header("Content-type: application/octet-stream");
	header("Content-disposition: attachment; filename=zipfile.zip");
	echo $zipfile->file();
 
	function rec_listFiles( $from = '.')
	{
		global $zipfile, $filedata;
 
		if(! is_dir($from))
			return false;
 
		if( $dh = opendir($from))
		{
			while( false !== ($file = readdir($dh)))
			{
				// Skip '.' and '..'
				if( $file == '.' || $file == '..')
					continue;
				$path = $from . '/' . $file;
				if( is_dir($path) ) {
					$zipfile->add_dir($path);
					rec_listFiles($path);
				}
				else {
					$filedata = implode("", file($path));
					$zipfile->add_file($filedata, $path);
				}
			}
			closedir($dh);
		}
	}
?>

Tạm đáp ứng yêu cầu :)

Source: http://www.devco.net/archives/2005/05/24/creating_zip_files_with_php.php

4 Responses to “Một PHP class tạo file ZIP”

  1. PHPFan says:

    Cho hỏi:
    require (“zipfile.inc.php”);

    zipfile.inc.php -> ở đâu???

  2. Cuối bài có chữ Source to thế cơ mà (:|

  3. Amalea says:

    hey guys thanks alot for all the insight. really liked the section. and iam starting to give it a shot. if you receive any different nice books or places on the content, love to hear from you. thanks again.

  4. Movias says:

    Bạn gửi đoạn unzip lên cho mình xem với. Thanks

Leave a Reply