Jump to content

How can I compress a whole directory?


shedokan

Recommended Posts

How can I compress a whole directory?

I tried creating this, but doesn't works:

<?php
class gz{
function adddir($name)
{
	$fr=$name;
	$this->datasec[]=$fr;
	$cdrec=$name; 
	$this->ctrl_dir[]=$cdrec; 
}
function addfile($data,$name)
{
	$zdata=gzcompress($data);
	$fr=$name;
	$fr .=$zdata;
	$this->datasec[]=$fr;

	$cdrec=$name;
	$this->ctrl_dir[]=$cdrec;
}

function file() {
	$data=implode('',$this->datasec);   
	$ctrldir=implode('',$this->ctrl_dir);   
	return $data.$ctrldir;
}  
}
$gz=new gz();
header('Content-type: application/octet-stream');
header('Content-disposition: attachment; filename=test.gz');
$file[1]=implode('', file('index.php'));

//$gz->adddir('dir/');
$gz->addfile($file[1],'index.php');
echo $gz->file();
?>

only says invalid archive.

please help, I need it for backup.

Link to comment
https://forums.phpfreaks.com/topic/119297-how-can-i-compress-a-whole-directory/
Share on other sites

use zip (http://be.php.net/manual/en/ref.zip.php) instead of zlib just iterate over the directory and add your files using the addFile() method, i suggest you write a recursive function for this, because you probably want subdirectories to be included.

 

other compression methods can be found here: http://be.php.net/manual/en/refs.compression.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.