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

ZipArchive Problem

Discussion in 'XenForo Development Discussions' started by ragtek, Nov 8, 2010.

  1. ragtek

    ragtek Guest

    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$filePathZipArchive::CREATE );
    if (
    $resource === TRUE)
    {
        
    $zip->addFromString'test.txt''Hier steht ein beliebiger Text.' );
        
    $directory XenForo_Autoloader::getInstance()->getRootDir() . '\Ragtek\\' substr($addOnId6) . '\\';

        
    $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?
     
  2. Boothby

    Boothby Active Member

    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$filePathZipArchive::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'); 
     
  3. ragtek

    ragtek Guest

    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);
                    }
     

Share This Page