Jump to content

Recommended Posts

hi,

i need to compress or zip a folder or directory and here is my script

 

$dir = "test";
$dir2 = scandir($dir);
$zip = new ZipArchive; 
$r = $zip->open('test.zip', ZipArchive::CREATE); 

foreach($dir2 as $item){
if($item == "." || $item == ".."){
	//do nothing
}else{
	$r = $zip->addFile($item,'item');
	echo $item;
}
}

 

but it's not zipping my folder

 

but when i tried this one

$zip = new ZipArchive; 
$r = $zip->open('test.zip', ZipArchive::CREATE); 
$r = $zip->addFile('testfile','testfile');

its working.

 

i cant make the array working in ziparchive function

 

please advice

 

tia

Link to comment
https://forums.phpfreaks.com/topic/176007-ziparchive/
Share on other sites

<?php

class Zipper extends ZipArchive {
   
public function addDir($path) {
    print 'adding ' . $path . '<br>';
    $this->addEmptyDir($path);
    $nodes = glob($path . '/*');
    foreach ($nodes as $node) {
        print $node . '<br>';
        if (is_dir($node)) {
            $this->addDir($node);
        } else if (is_file($node))  {
            $this->addFile($node);
        }
    }
}
   
} // class Zipper 

 

this is code straight from the  addFile method's reference page the code was made public by "peter at boring dot ch" its not my code, but it would solve your problem :)

Link to comment
https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-927417
Share on other sites

its not working

:shrug:

<?php
$dir = "test"; //folder with 2 files

class Zipper extends ZipArchive {
   
public function addDir($path) {
    print 'adding ' . $path . '<br>';
    $this->addEmptyDir($path);
    $nodes = glob($path . '/*');
    foreach ($nodes as $node) {
        print $node . '<br>';
        if (is_dir($node)) {
            $this->addDir($node);
        } else if (is_file($node))  {
            $this->addFile($node);
        }
    }
}
   
}
$zip = new Zipper();
$zip->addDir($dir);
?>

Warning: ZipArchive::addEmptyDir() [function.ZipArchive-addEmptyDir]: Invalid or unitialized Zip object in C:\xampp\htdocs\canpages\test\testzip.php on line 23
test/test1.php

 

line 23 is    $this->addEmptyDir($path);

Link to comment
https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-927422
Share on other sites

hi,

the zipping of file is ok but it also zips the root folder

:(

class Zipper extends ZipArchive {
   
public function addDir($path) {
    //print 'adding ' . $path . '<br>';
    $this->addEmptyDir($path);
    $nodes = glob($path . '/*');
    foreach ($nodes as $node) {
        //print $node . '<br>';
        if (is_dir($node)) {
            $this->addDir($node);
        } else if (is_file($node))  {
            $this->addFile($node);
        }
    }
}
}

$dir = "\\\\asecasiagsd\\AGS\\AGS\\WIP\\RECSCHART\\";
		$folder = "\\\\asecasiagsd\\AGS\\AGS\\WIP\\RECSCHART\\".$artid;
		$dest = $dir;
		$zip = new Zipper;
		$zip->open($dest.$artid.'.zip',Zipper::CREATE);
		$zip->addDir($dir.$artid);
		$zip->close();

 

when i unzip it, it has a directory of ags/wip/recschart/$artid/all files

i need the folder $artid/all files only

 

please help

 

TA

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-928375
Share on other sites

I've modified the code above that I gave you from the poster on php.net and have come up with something pretty sweet, hope it solves your problem :)

 

<?php
class Zipper extends ZipArchive {
   
public function addIntoDir($path,$name = false,$into = false) {
	$path = trim($path,'/');
	if ($name === false) $name = $path;
	$this->addEmptyDir($e = ($into.'/'.$name));
	$nodes = glob($path . '/*');
	foreach ($nodes as $node) {
		$x = explode('/', $node);
		if (is_dir($node)) {
			$this->addIntoDir($node,$x[count($x) - 1],$path);
		} else if (is_file($node))  {
			$this->addFile($node,$path.'/'.$x[count($x) - 1]);
		}
	}
}
}
$z = new Zipper;
$z->open('zipToMake.zip',Zipper::CREATE);
$z->addIntoDir('PathToTheDirectoryToZip','NameOfDirectoryInsideZipFile');
?>

Link to comment
https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-928835
Share on other sites

sorry to ask but is this the correct usage for the script?

$dir = "//asecasiagsd/AGS/AGS/WIP/RECSCHART//";
			$folder = $artid;

$z = new Zipper;
$z->open($dir.$artid.".zip",Zipper::CREATE);
$z->addIntoDir($dir,$folder);
$z->close();

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-928855
Share on other sites

$dir = "d:\\asecmenu\sample\sample";
$z = new Zipper;
$z->open('d:\\asecmenu\sample\zipToMake.zip',Zipper::CREATE);
$z->addIntoDir($dir,$dir);

 

i tried this in my localhost, and it is zipping the all folder i indicated in $dir from asecmenu\sample\sample\*.*

so the zip file has three folders, asecmenu, sample and sample, but i want is just the last sample folder...

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-928938
Share on other sites

hi,

i need to zip a file and download it

<?php
//function to zip and force download the files using PHP
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
  //create the object
  $zip = new ZipArchive();
  //create the file and throw the error if unsuccessful
  if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
    exit("cannot open <$archive_file_name>\n");
  }

  //add each files of $file_name array to archive
  foreach($file_names as $files)
  {
    $zip->addFile($file_path.$files,$files);
  }
  $zip->close();

  //then send the headers to foce download the zip file
  header("Content-type: application/zip");
  header("Content-Disposition: attachment; filename=$archive_file_name");
  header("Pragma: no-cache");
  header("Expires: 0");
  readfile("$archive_file_name");
  exit;
}
$file = "AG3437081ETB0902004";
$dir = scandir($file);
  $file_names = $dir;
  $archive_file_name = $file."2.zip";
  $file_path = "C:/xampp/htdocs/laboratory/compress/".$file."/";
  echo $file_path;
  zipFilesAndDownload($file_names,$archive_file_name,$file_path);
?>

 

i dont know whats wrong with my script

 

can anyone please kindly check it.

 

TIA

Link to comment
https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-931383
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.