PHP

Zip folder dengan menggunakan PHP

Adakalanya kita membutuhkan script untuk mendowload beberapa file menjadi satu file zip untuk mempermudah dalam melakukan download pada halaman website.

Berikut cara membuat script untuk proses pengabungan file menjadi satu file zip :

<?php
$directory = "Directory_Name";
$zip_file_name = "Nama_File.zip";
$download_file = true;

class FlxZipArchive extends ZipArchive {
  /** Add a Dir with Files and Subdirs to the archive;;;;; @param string $location Real Location;;;;  @param string $name Name in Archive;;; @author Nicolas Heimann;;;; @access private  **/

    public function addDir($location, $name) {
      $this->addEmptyDir($name);
        $this->addDirDo($location, $name);
   } // EO addDir;


    /**  Add Files & Dirs to archive;;;; @param string $location Real Location;  @param string $name Name in Archive;;;;;; @author Nicolas Heimann
     * @access private   **/
    private function addDirDo($location, $name) {
        $name .= '/';
      $location .= '/';

        // Read all Files in Dir
        $dir = opendir ($location);
        while ($file = readdir($dir))
        {
            if ($file == '.' || $file == '..') continue;
            // Rekursiv, If dir: FlxZipArchive::addDir(), else ::File();
            $do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
            $this->$do($location . $file, $name . $file);
        }
    } // EO addDirDo();
}

$zip_achive = new FlxZipArchive;
$$zip_achive_open = $zip_achive->open($zip_file_name, ZipArchive::CREATE);
if($$zip_achive_open === TRUE)
{
    $zip_achive->addDir($directory, basename($directory));
    $zip_achive->close();
} else  { 
    $download_file = false;
    echo 'Could not create a zip archive';
}

if ($download_file)
{
    ob_get_clean();
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private", false);
    header("Content-Type: application/zip");
    header("Content-Disposition: attachment; filename=" . basename($zip_file_name) . ";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . filesize($zip_file_name));
    readfile($zip_file_name);
    unlink($zip_file_name);
}

?>

 

Selamat mencoba, terima kasih

Kenapa Perlu Zip Folder di PHP?

Kompresi folder menjadi format ZIP sangat berguna untuk:

  • Backup data - Mengkompres folder sebelum diunduh atau dikirim
  • Distribusi file - Mengirim banyak file dalam satu paket
  • Penyimpanan - Menghemat ruang disk

Menggunakan ZipArchive Class

<?php
$zip = new ZipArchive();
$filename = 'backup_' . date('Y-m-d') . '.zip';

if ($zip->open($filename, ZipArchive::CREATE) === TRUE) {
    $folder = 'path/to/folder';
    $files = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($folder),
        RecursiveIteratorIterator::LEAVES_ONLY
    );
    
    foreach ($files as $file) {
        if (!$file->isDir()) {
            $filePath = $file->getRealPath();
            $relativePath = substr($filePath, strlen($folder) + 1);
            $zip->addFile($filePath, $relativePath);
        }
    }
    
    $zip->close();
    echo "Zip berhasil: $filename";
} else {
    echo "Gagal membuat zip";
}
?>

Tips dan Best Practices

  • Password protection - Gunakan opsi enkripsi untuk file sensitif
  • Progress tracking - Untuk folder besar, tampilkan progress ke user
  • Error handling - Tangani file yang locked atau permission denied
  • Kompresi level - Sesuaikan level kompresi antara ukuran dan kecepatan


0 Comments


Leave a Reply

Comments with links or spam keywords will be rejected.
Scroll to Top