PDA

View Full Version : سوال: فشرده سازی چند فایل با PHP



amirrezaq
چهارشنبه 02 بهمن 1392, 23:43 عصر
با عرض سلام و خسته نباشید
من چندتا فایل یا مثلا چندتا پوشه دارم
چگونه باید این فایل ها رو با PHP به فرمت zip در بیارم؟؟؟
با تشکر

2undercover
پنج شنبه 03 بهمن 1392, 09:01 صبح
http://stackoverflow.com/questions/3972342/php-compress-files-into-zip

amirrezaq
پنج شنبه 03 بهمن 1392, 11:14 صبح
عزیز من اینو خوندم قبلا
ولی خیلی زبانم قویه چیزی نفهمیدم!
اگر میشه خودتون بگید!

MRmoon
پنج شنبه 03 بهمن 1392, 11:28 صبح
لینک اصلی:


http://davidwalsh.name/create-zip-php

این تابع برای ساخت:


/* creates a compressed zip file */ function create_zip($files = array(),$destination = '',$overwrite = false) { //if the zip file already exists and overwrite is false, return false if(file_exists($destination) && !$overwrite) { return false; } //vars $valid_files = array(); //if files were passed in... if(is_array($files)) { //cycle through each file foreach($files as $file) { //make sure the file exists if(file_exists($file)) { $valid_files[] = $file; } } } //if we have good files... if(count($valid_files)) { //create the archive $zip = new ZipArchive(); if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { return false; } //add the files foreach($valid_files as $file) { $zip->addFile($file,$file); } //debug //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status; //close the zip -- done! $zip->close(); //check to make sure the file exists return file_exists($destination); } else { return false; } }

استفاده‌:


$files_to_zip = array( 'preload-images/1.jpg', 'preload-images/2.jpg', 'preload-images/5.jpg', 'kwicks/ringo.gif', 'rod.jpg', 'reddit.gif' ); //if true, good; if false, zip creation failed $result = create_zip($files_to_zip,'my-archive.zip');

حالا مثلا برای فایل های mp3 یک صفحه:


<?PHP
$files_to_zip = glob('path/to/mp3/files/*.mp3');
//if true, good; if false, zip creation failed
$result = create_zip($files_to_zip,'my-mp3s.zip');

MRmoon
پنج شنبه 03 بهمن 1392, 11:29 صبح
واقعا تو همین لینک من نیازی به استفاده از زبان انگلیسی ندیدم!!!!!

فقط باید معنی
Sample Usage



رو میفهمیدی:بامزه:


حالا یا شما کلا با سرچ کردن مشکل دارین یا اصلا جست و جو نکردین.

amirrezaq
پنج شنبه 03 بهمن 1392, 11:39 صبح
نه من جستجو کردم حال نداشتم بخونم!
ولی ممنون!

2undercover
پنج شنبه 03 بهمن 1392, 11:57 صبح
با استفاده از کلاس ZipArchive و متد addGlob:

متغیر path نشان دهنده آدرس و نام فایل فشرده شده خواهد بود.

سپس با استفاده از متد open و فلگ OVERWRITE یک فایل جدید ایجاد و اگر از قبل موجود بود جایگزین می کنیم.

بعد با متد addGlob بر اساس یک الگو آدرس فایل هایی که قرار فشرده کنیم رو اضافه می کنیم.

در نهایت چک می کنیم ببینیم که اگر وضعیت OK نبود یک خطا چاپ میشه و در غیر این صورت کار رو با فایل فشرده تمام می کنیم:


$path = dirname(__FILE__)."/myMP3s.zip";
$zipArchive = new ZipArchive();

if (!$zipArchive->open($path, ZIPARCHIVE::OVERWRITE))
die("Failed to create archive\n");

$zipArchive->addGlob(dirname(__FILE__)."/*.mp3");
if (!$zipArchive->status == ZIPARCHIVE::ER_OK)
echo "Failed to write local files to zip\n";

$zipArchive->close();