ZipArchives stores absolute paths

血红的双手。 提交于 2019-12-22 09:05:56

问题


Can I zip files using relative paths?

For example:

$zip->addFile('c:/wamp/www/foo/file.txt');

the ZIP should have a directory structure like:

foo
 -> file.txt

and not:

wamp
 -> www
     -> foo
         -> file.txt

like it is by default...

ps: my full code is here (I'm using ZipArchive to compress contents of a directory into a zip file)


回答1:


See the addFile() function definition, you can override the archive filename:

$zip->addFile('/path/to/index.txt', 'newname.txt');



回答2:


If you are trying to recursively add all subfolders and files of a folder, you can try the code below (I modified this code/note in the php manual).

class Zipper extends ZipArchive {
    public function addDir($path, $parent_dir = '') {
        if($parent_dir != ''){
            $this->addEmptyDir($parent_dir);
            $parent_dir .= '/';
            print '<br>adding dir ' . $parent_dir . '<br>';
        }
        $nodes = glob($path . '/*');
        foreach ($nodes as $node) {
            if (is_dir($node)) {
                $this->addDir($node, $parent_dir.basename($node));
            }
            else if (is_file($node))  {
                $this->addFile($node, $parent_dir.basename($node));
                print 'adding file '.$parent_dir.basename($node) . '<br>';
            }
        }
    }
} // class Zipper

So basically what this does is it does not include the directories (absolute path) before the actual directory/folder that you want zipped but instead only starts from the actual folder (relative path) you want zipped.




回答3:


Here is a modified version of Paolo's script in order to also include dot files like .htaccess, and it should also be a bit faster since I replaced glob by opendir as adviced here.

<?php

$password = 'set_a_password'; // password to avoid listing your files to anybody

if (strcmp(md5($_GET['password']), md5($password))) die();

// Make sure the script can handle large folders/files
ini_set('max_execution_time', 600);
ini_set('memory_limit','1024M');

//path to directory to scan
if (!empty($_GET['path'])) {
    $fullpath = realpath($_GET['path']); // append path if set in GET
} else { // else by default, current directory
    $fullpath = realpath(dirname(__FILE__)); // current directory where the script resides
}

$directory = basename($fullpath); // parent directry name (not fullpath)
$zipfilepath = $fullpath.'/'.$directory.'_'.date('Y-m-d_His').'.zip';

$zip = new Zipper();

if ($zip->open($zipfilepath, ZipArchive::CREATE)!==TRUE) {
    exit("cannot open/create zip <$zipfilepath>\n");
}

$past = time();

$zip->addDir($fullpath);

$zip->close();

print("<br /><hr />All done! Zipfile saved into ".$zipfilepath);
print('<br />Done in '.(time() - $past).' seconds.');

class Zipper extends ZipArchive { 

    // Thank's to Paolo for this great snippet: http://stackoverflow.com/a/17440780/1121352
    // Modified by LRQ3000
    public function addDir($path, $parent_dir = '') {
        if($parent_dir != '' and $parent_dir != '.' and $parent_dir != './') {
            $this->addEmptyDir($parent_dir);
            $parent_dir .= '/';
            print '<br />--> ' . $parent_dir . '<br />';
        }

        $dir = opendir($path);
        if (empty($dir)) return; // skip if no files in folder
        while(($node = readdir($dir)) !== false) {
            if ( $node == '.' or $node == '..' ) continue; // avoid these special directories, but not .htaccess (except with GLOB which anyway do not show dot files)
            $nodepath = $parent_dir.basename($node); // with opendir
            if (is_dir($nodepath)) {
                $this->addDir($nodepath, $parent_dir.basename($node));
            } elseif (is_file($nodepath)) {
                $this->addFile($nodepath, $parent_dir.basename($node));
                print $parent_dir.basename($node).'<br />';
            }
        }
    }
} // class Zipper 

?>

This is a standalone script, just copy/paste it into a .php file (eg: zipall.php) and open it in your browser (eg: zipall.php?password=set_a_password , if you don't set the correct password, the page will stay blank for security). You must use a FTP account to retrieve the zip file afterwards, this is also a security measure.



来源:https://stackoverflow.com/questions/12902387/ziparchives-stores-absolute-paths

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!