• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

ZipArchive Problem

R

ragtek

Guest
#1
Hi, i'm trying to create a zip archive on the fly
PHP:
           # $zip = new Ragtek_Helper_ZipArchive();
            $zip = new ZipArchive();
            $filePath = 'd:\addon.zip';
            
$resource = $zip->open( $filePath, ZipArchive::CREATE );
if ($resource === TRUE)
{
    $zip->addFromString( 'test.txt', 'Hier steht ein beliebiger Text.' );
    $directory = XenForo_Autoloader::getInstance()->getRootDir() . '\Ragtek\\' . substr($addOnId, 6) . '\\';

    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
        while ($files->valid())
        {
            $key = $files->key();
             $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
             Ragtek_Helper_Log::getInstance()->log($key . '  added');
             
             $files->next();
        }


   # $zip->addDirectory($directory);
    $zip->close();
The script creates the archive and adds the text.txt to the archive,
my logger shows that the directory content should be added, but it's not in the archive

Anybody know what's the problem?
 

Boothby

Active member
#2
Anybody know what's the problem?
Yes. :D

You're trying to add directories as files and that will not work. :) You have to check if $files->key() is a file or directory.
And you should define a starting dir in the zip-file or you will get the complete root-path.

PHP:
$zip = new ZipArchive();
$filePath = 'addon.zip';

$resource = $zip->open( $filePath, ZipArchive::CREATE );
if ($resource === TRUE)
{
    $zip->addFromString( 'test.txt', 'Hier steht ein beliebiger Text.' );

    $directory = '/this/is/a/rather/long/path'; #XenForo_Autoloader::getInstance()->getRootDir() . '\Ragtek\\' . substr($addOnId, 6) . '\\';
    $basedir = dirname($directory);

    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
        while ($files->valid())
        {
            $key = $files->key();
            $key1 = realpath($key);
            $key2 = str_replace($basedir, '', $key1 );

            if(is_file($key1))
            {
                 $zip->addFile($key1, $key2) or die ("ERROR: Could not add file: $key");
                 echo "$key1 :: $key2<br>"; #Ragtek_Helper_Log::getInstance()->log($key . '  added');
            }
             $files->next();
        }


   # $zip->addDirectory($directory);

    $zip->close();

}
else die('cannot open');
 
R

ragtek

Guest
#3
thx and i've foiund an other way

PHP:
                if ($element->isDir())
                {
                    $zip->addEmptyDir($dir);
                }
               elseif ($element->isFile())
                {
                    $file = $element->getPath() . '\\' . $element->getFilename();
                   $fileInArchiv = $dir . $element->getFilename();
                    $zip->addFile($file, $fileInArchiv);
                }