Jump to content

zip help


tomfmason

Recommended Posts

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.

[code]
<?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;
?>[/code]

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

[code]
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
)

)
[/code]

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

Thanks,
Tom
Link to comment
Share on other sites

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.

[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);
?>[/code]

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
Link to comment
Share on other sites

I tried adding this.

[code=php:0]
$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);
[/code]

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

[code]
<?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);
?>
[/code]

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
Link to comment
Share on other sites

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.

[code=php:0]
$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);
[/code]

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

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.