Nasıl [ardışık] PHP bir dizin Zip için? | Netgez.com
SORU
26 AĞUSTOS 2009, ÇARŞAMBA


Nasıl [ardışık] PHP bir dizin Zip için?

Dizin gibi bir ÅŸey:

home/
    file1.html
    file2.html
Another_Dir/
    file8.html
    Sub_Dir/
        file19.html

Aynı PHP Zip sınıfı http://trac.seagullproject.org/browser/branches/0.6-bugfix/lib/other/Zip.php PHPMyAdmin kullanılan kullanıyorum . Sadece bir dosya yerine bir dizin zip için nasıl emin değilim. Şimdiye kadar var:

$aFiles = $this->da->getDirTree($target);
/* $aFiles is something like, path => filetime
Array
(
    [home] => 
    [home/file1.html] => 1251280379
    [home/file2.html] => 1251280377
    etc...
)

*/
$zip = & new Zip();
foreach( $aFiles as $fileLocation => $time ){
    $file = $target . "/" . $fileLocation;
    if ( is_file($file) ){
        $buffer = file_get_contents($file);
        $zip->addFile($buffer, $fileLocation);
    }
}
THEN_SOME_PHP_CLASS::toDownloadData($zip); // this bit works ok

ama aldığım karşılık gelen indirilen zip dosyasını açmak için çalışın "işleme izin verilmedi"

Bu hata sadece dosya Tamam unzips komut satırı üzerinden halletmek zaman benim mac üzerinde açmak için çalıştığımda olmuyor. İndir hakkında belirli bir içerik türü yollamanız gerekiyor, şu anda 'application/zip'

CEVAP
26 AĞUSTOS 2009, ÇARŞAMBA


Burada herhangi bir dosya ya da dizin özyinelemeli olarak sıkıştırabilir basit bir fonksiyon, sadece zip uzantılı gerektiğini yüklenecek.

function Zip($source, $destination)
{
    if (!extension_loaded('zip') || !file_exists($source)) {
        return false;
    }

    $zip = new ZipArchive();
    if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
        return false;
    }

    $source = str_replace('\\', '/', realpath($source));

    if (is_dir($source) === true)
    {
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);

        foreach ($files as $file)
        {
            $file = str_replace('\\', '/', $file);

            // Ignore "." and ".." folders
            if( in_array(substr($file, strrpos($file, '/') 1), array('.', '..')) )
                continue;

            $file = realpath($file);

            if (is_dir($file) === true)
            {
                $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
            }
            else if (is_file($file) === true)
            {
                $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
            }
        }
    }
    else if (is_file($source) === true)
    {
        $zip->addFromString(basename($source), file_get_contents($source));
    }

    return $zip->close();
}

Bu gibi diyoruz:

Zip('/folder/to/compress/', './compressed.zip');

Bunu PaylaÅŸ:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • 24 Канал

    24 КанаÐ

    5 ÅžUBAT 2006
  • Greater Than Gatsby Photoshop Actions & Lightroom Presets

    Greater Than

    11 ÅžUBAT 2013
  • OVERWERK

    OVERWERK

    6 Temmuz 2010