Skip to content

ZipArchive::close(): Read error: Is a directory #17012

@remco-pc

Description

@remco-pc

Description

I am getting this error and want to know more about it (what am i doing wrong here) no valid search results

source is a directory and target a destination (with the .zip extension) and i believe (going to check now, but this is a readonly directory).

function function_zip_archive(Parse $parse, Data $data){
    $object = $parse->object();
    $source = App::parameter($object, 'archive', 1);
    $target = App::parameter($object, 'archive', 2);
    $limit = $parse->limit();
    $parse->limit([
        'function' => [
            'date'
        ]
    ]);
    try {
        $target = $parse->compile($target, [], $data);
        $parse->setLimit($limit);
    } catch (Exception $exception) {
        echo $exception->getMessage() . PHP_EOL;
        return;
    }
    if(
        Dir::is($source) &&
        !File::exist($target)
    ){
        $dir = new Dir();
        $read = $dir->read($source, true);
        $host = [];
        if(!is_array($read)){
            return null;
        }
        foreach($read as $file){
            $host[] = $file;
        }
        $dir = Dir::name($target);
        if(
            $dir &&
            !in_array(
                $dir,
                [
                    $object->config('ds')
                ],
                true
            )
        ){
            Dir::create($dir);
        }
        $zip = new \ZipArchive();
        $zip->open($target, \ZipArchive::CREATE);
        foreach($host as $file){
            $location = false;
            if(substr($file->url, 0, 1) === $object->config('ds')){
                $location = substr($file->url, 1);
            } else {
                $location = $file->url;
            }
            if(!empty($location)){
                if($file->type === Dir::TYPE){
                    $zip->addEmptyDir($location);
                } else {
                    $zip->addFile($source, $location);
                }
            }
            d($location);
        }
        $zip->close();
        echo $target;
    }
    elseif(
        File::is($source) &&
        !File::exist($target)
    ) {
        $dir = Dir::name($target);
        if(
            $dir &&
            !in_array(
                $dir,
                [
                    $object->config('ds')
                ],
                true
            )
        ){
            Dir::create($dir);
        }
        $zip = new \ZipArchive();
        $zip->open($target, \ZipArchive::CREATE);
        $location = false;
        if(substr($source, 0, 1) === $object->config('ds')){
            $location = substr($source, 1);
        } else {
            $location = $source;
        }
        if(!empty($location)){
            $zip->addFile($source, $location);
        }
        $zip->close();
        echo $target;
    }
}

### PHP Version

 8.3.11

### Operating System

Debian 12

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions