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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.