Jump to content


Photo

zip help


  • Please log in to reply
4 replies to this topic

#1 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 05 September 2006 - 12:14 AM

I have a function that will search a diectory and enter all files and sub directories into a zip file. The problem is that it dosn't add any subfolders with the files in them. When I unzipped the file I found that there were two files with the .file extenstion that were supposed to be a directory.

here is the function.

<?php
include_once('includes/zip.class.php');
function zipfiles($dir) {
    $newzip = new createZip;
	$newzip -> addDirectory('main/');
    static $files = array();
    static $dirs = array();

    $handle = opendir($dir);

    while (($file = readdir($handle)) !== false) {
        if (!in_array($file, array('.', '..'))) {
            if (is_dir($path = ($dir . DIRECTORY_SEPARATOR . $file))) {
                $newzip -> addDirectory("main/" . $file); 
	   $dirs[] = $path;
                zipfiles($path);
            } else {
                $files[] = $path;
	   $contents = file_get_contents($path);
	   $newzip -> addFile($contents, "main/" . $file);
            }
        }
    }
    $zipname = "test.zip";
    $fp = fopen($zipname, "wb");
    $out = fwrite($fp, $newzip -> getZippedfile($zipname));
    fclose($fp);
    if (file_exists("./$zipfile") == true) {
       $result = "The zipfile ($zipname) was created";
   }else{
      $result = "The zipfile ($zipname) was not created";
   }
   return $result;		
}
$zip = zipfiles('.');
echo $zip;
?>

Here is the list of the files and folders in the directory

Array
(
[dirs] => Array
(
[0] => ./images
[1] => ./includes
[2] => ./includes/test
)

[files] => Array
(
[0] => ./db.php
[1] => ./functions.php
[2] => ./includes/class.php
[3] => ./includes/db.php
[4] => ./includes/test/test.htm
[5] => ./includes/zip.class.php
[6] => ./index.php
[7] => ./mail.php
[8] => ./test.pdf
[9] => ./test.php
[10] => ./test.txt
[11] => ./test2.php
[12] => ./test2.txt
[13] => ./test3.php
[14] => ./test4.php
[15] => ./test5.php
[16] => ./test6.php
[17] => ./whois.txt
[18] => ./whois2.txt
)

)

Any suggestions on how I can add the folders, subfolders and all files as they are found.

Thanks,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 05 September 2006 - 07:39 AM

I sort of got it working. I am able to create a zip file with all of the files and directories in it.

Here is the fixed code.

<?php
include_once("includes/zip.class.php");
function listfiles($dir, $zipdir) {
     static $files = array();
     static $dirs = array();
     static $zipdirs = array();
     static $zipfiles = array();
	
    $handle = opendir($dir);
	
    while (($file = readdir($handle)) !== false) {
        if (!in_array($file, array('.', '..'))) {
            if (is_dir($path = ($dir . DIRECTORY_SEPARATOR . $file))) {
	   $newdir = $zipdir . $file . "/";
	   $dirs[] = $path;
	   $zipdirs[] = $newdir;
                listfiles($path, $newdir);
            } else {
	   $newfile = $zipdir . $file;
                $files[] = "$path, $newfile";
            }
        }
    }
    return array($zipdirs, $files);
}
$list = listfiles('.', '');
list($zipdirs, $fs) = $list;
$newzip =  new createZip;
foreach ($zipdirs as $zipdir) {
   $newzip -> addDirectory($zipdir);
}
foreach ($fs as $f) {
    list ($file, $zipfile) = explode(",", $f);
    $contents = file_get_contents($file);
    $newzip -> addFile($contents, $zipfile);
}
$zipname = "test.zip";
$fp = fopen($zipname, "wb");
$out = fwrite($fp, $newzip -> getZippedfile($zipname));
fclose($fp);
?>

However, I am having an issue. When I have a directory that has a sub directory it is created twice. Like in the includes directory I have a sub folder called test.

When I created the zipfile I had two includes directories. One had the files that were orginaly in the includes directory and the other was empty.

Here is an example of what I mean.

The folders before the zip
1 images
2 inludes

and in side the zip
1 images
2 includes (the one with the files and sub directories)
3 inludes (a empty copy of the first one)

Any suggestion as to why this is happening or how I can search the zipfile and remove any empty duplicate directories?

Thanks,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 05 September 2006 - 08:56 AM

I tried adding this.

$list = listfiles('.', '');
list($zipdirs, $fs) = $list;
$newzip =  new createZip;
foreach ($zipdirs as $zipdir) {
   if (!in_array($zip_dir, $created)) {
      $newzip -> addDirectory($zipdir);
      $created[] = $zipdir;
   }
}
foreach ($fs as $f) {
    list ($file, $zipfile) = explode(",", $f);
    $contents = file_get_contents($file);
    $newzip -> addFile($contents, $zipfile);
}
$zipname = "test.zip";
$fp = fopen($zipname, "wb");
$out = fwrite($fp, $newzip -> getZippedfile($zipname));
fclose($fp);

this dosn't stop the dublicate file from being added.


I also tried this piece of code to see if it was my zip class that is causeing the trouble

<?php
include_once("includes/zip.class.php");
$newzip = new createZip;
$newzip -> addDirectory('test/');
$newzip -> addDirectory('test/test/');
$newzip -> addDirectory('test/test/test/');

$zipname = "test2.zip";
$fp = fopen($zipname, "wb");
$out = fwrite($fp $newzip -> getZippedfile($zipname));
fclose($fp);
?>


This works fine. Each directory is created without a double entry. So this means that it is somewhere in the  foreach loop. Any suggestions ??

Thanks,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 05 September 2006 - 06:00 PM

Anyone have any suggestions? I am beating my head against a wall on this one

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#5 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 06 September 2006 - 01:53 AM

I figured out what the problem was. If I try to create a subdirectory it will create an empty directory. However, if I just create the files, the directories will be created as well. So all I have to do is this.

$list = listfiles('.', '');
list($zipdirs, $fs) = $list;
$newzip =  new createZip;
foreach ($fs as $f) {
    list ($file, $zipfile) = explode(",", $f);
    $contents = file_get_contents($file);
    $newzip -> addFile($contents, $zipfile);
}
$zipname = "test.zip";
$fp = fopen($zipname, "wb");
$out = fwrite($fp, $newzip -> getZippedfile($zipname));
fclose($fp);

This will create all of the directories and files. Unless a directory is empty. I will work on that.


Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users