PHP Classes

Cannot adding all files

Recommend this page to a friend!

      Zip Stream  >  All threads  >  Cannot adding all files  >  (Un) Subscribe thread alerts  
Subject:Cannot adding all files
Summary:generated zip file is corrupt..
Messages:3
Author:Szenogradi Norbert
Date:2011-08-16 08:49:42
Update:2011-08-16 12:13:44
 

  1. Cannot adding all files   Reply   Report abuse  
Picture of Szenogradi Norbert Szenogradi Norbert - 2011-08-16 08:49:43
Hi,


I wrote a small script to generate zip file from selected dir.

Dir contain max 5 depth dir/subdirs.

The php script:
pastebin.com/WBYYgfQk

The size is maybe ok?!, but generated zip file is corrupt.
I can't open it.

Can you help me what is wrong ?

My system is Ubuntu Linux.

  2. Re: Cannot adding all files   Reply   Report abuse  
Picture of Asbjorn Grandt Asbjorn Grandt - 2011-08-16 10:30:34 - In reply to message 1 from Szenogradi Norbert
There are a few problems with your script.

One, the path given as the Zip path must be valid, meaning no double or leading '/' etc. Your script does add a few extra slashes at times.
You can use ZipStream::getRelativePath($path) to help clean those. That function is identical to my RelativePath class also here on PHPClasses. Additionally ZipStream::pathJoin($path1, $path2); will also work better than just "{$path1}/{$path2}".

Second, all files are added the number of times matching the depth of the file, meaning a file in the 5th subdirectory is added 5 times with your script. You are traversing the tree structure fully for each directory, because you are starting by building the directory tree itself.

Instead, you can use ZipStream to add a full directory tree.

$zip->addDirectoryContent($realPath, $zipPath, $recursive)
i.e.
$zip->addDirectoryContent("testing/", "testing/", TRUE);

You can omit the $recursive parameter, as it defaults to TRUE.


I'll have to look into hardening the functions against the invalid paths as well as try to keep track of duplicates.

  3. Re: Cannot adding all files   Reply   Report abuse  
Picture of Szenogradi Norbert Szenogradi Norbert - 2011-08-16 12:13:44 - In reply to message 2 from Asbjorn Grandt
Thanks, My script check subdir tree in dirs and not present then adding addDirectoryContent...

I'm integrating except list for addDirectoryContent function and my script is done.

Thanks for help and for class!
Great job.