Search code examples
phpcodeigniterzip

Issue with Arabic file names while compress with Codeigniter zip library


I have certain files to compress and some of them have Arabic names, Upon extraction, they come out corrupted. My PHP version is 7.2.18 and I am using the Codeigniter 3.1 framework.

Actual file name : كلمة العينة.docx Compressed filename : +â+ä+à+¬_+º+ä+¦+è+å+¬.docx

public function generate_zip() {
        $this->load->library('zip');

        $zip_files = $this->document_model->get_documents();

        foreach ($zip_files as $zip_file) {     
            $this->zip->read_file('uploads/documents/' . $zip_file->document_file_name);
        }
        $this->zip->download('temp.zip');
    }

Solution

  • Hi Please try with the below code.

    /* creates a compressed zip file */
    function create_zip($files = array(),$destination = '',$overwrite = false) {
        //if the zip file already exists and overwrite is false, return false
        if(file_exists($destination) && !$overwrite) { return false; }
        //vars
        $valid_files = array();
        //if files were passed in...
        if(is_array($files)) {
            //cycle through each file
            foreach($files as $file) {
                //make sure the file exists
                if(file_exists($file)) {
                    $valid_files[] = $file;
                }
            }
        }
        //if we have good files...
        if(count($valid_files)) {
            //create the archive
            $zip = new ZipArchive();
            if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
                return false;
            }
            //add the files
            foreach($valid_files as $file) {
                $zip->addFile($file,$file);
            }
            //debug
            //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
    
            //close the zip -- done!
            $zip->close();
    
            //check to make sure the file exists
            return file_exists($destination);
        }
        else
        {
            return false;
        }
    }
    
    $files_to_zip = array(
        'عينة.txt',
    );
    //if true, good; if false, zip creation failed
    $result = create_zip($files_to_zip,'my-archive.zip');