How can I create password protected ZIP file

How do I save uploaded files with password protected ZIP file.

so I want to upload my files then I need to save them in password protection ZIP file

I think I need to make changes in processor.php. Could you help me @patrick

The file dmxConnectLib/lib/zip/Processor.php must be edited. I checked the PHP documentation and see that it is possible to password protect zip files since PHP version 7.2.

https://www.php.net/manual/en/ziparchive.setencryptionname.php

I have added it to the PHP file but I haven’t tested it, perhaps you can test it and improve when needed. The actions zip, zipdir, unzip and dir should now accept an extra options password. Creating a zip archive with password requires PHP 7.2.

Processor.zip (1.1 KB)

Thank you @patrick your quick solution ,

but when I try, I encounter 2 problems.

  1. I dont know how to assign the password (password will come from $_POST) in server connect UI

  2. If I don’t define a password, it gives me the error message as follows.

    1. code: 0
    2. file: “/var/www/vhosts/bendensonraya.com/httpdocs/dmxConnectLib/lib/zip/Processor.php”
    3. line: 74
    4. message: “syntax error, unexpected ‘return’ (T_RETURN)”
    5. trace: “#0 [internal function]: {closure}(‘lib\zip\Process…’)↵#1 /var/www/vhosts/bendensonraya.com/httpdocs/dmxConnectLib/modules/zip.php(27): spl_autoload_call(‘lib\zip\Process…’)↵#2 /var/www/vhosts/bendensonraya.com/httpdocs/dmxConnectLib/lib/App.php(163): modules\zip->zip(Object(stdClass), ‘newZip1’)↵#3 /var/www/vhosts/bendensonraya.com/httpdocs/dmxConnectLib/lib/App.php(128): lib\App->execSteps(Object(stdClass))↵#4 /var/www/vhosts/bendensonraya.com/httpdocs/dmxConnectLib/lib/App.php(98): lib\App->execSteps(Array)↵#5 /var/www/vhosts/bendensonraya.com/httpdocs/dmxConnectLib/lib/App.php(71): lib\App->exec(Object(stdClass))↵#6 /var/www/vhosts/bendensonraya.com/httpdocs/dmxConnect/api/bendensonraya/mainsite/kullanici_file_upload/kullanici_file_upload.php(8): lib\App->define(Object(stdClass))↵#7 {main}”

Oh, that was some old code, I first tried to add the password to the create, but later moved it to the action methods. You can remove line 71 and 72.

You can pass the password using the options in the json. You have to edit the action file by hand, Wappler doesn’t know about the new password option.

yes removed old codes now @patrick . But still getting server error 500

The $password argument should also be removed from the create function.

public static function create($zipfile) {

I cant see the problem … sorry @patrick

{
“name”: “newZip1”,
“module”: “zip”,
“action”: “zipdir”,
“options”: {
“zipfile”: “/userFiles/204/204.zip”,
“password”: “{{password}}”,
“path”: “/userFiles/204”
}
processor.php
Processor.zip (1.1 KB)

Her the full PHP code for Processor.php, I hope that I have no type errors this time.

<?php

namespace lib\zip;

class Processor
{
    public static function zip($options) {
        $zip = ZipProcessor::create($options->zipfile);
        if (isset($options->password)) {
            $zip->setPassword($options->password);
        }
        $zip->addFiles($options->files, isset($options->password));
        $zip->setArchiveComment($options->comment);
        $zip->save();

        return $options->zipfile;
    }

    public static function zipdir($options) {
        $zip = ZipProcessor::create($options->zipfile);
        if (isset($options->password)) {
            $zip->setPassword($options->password);
        }
        $zip->addDir($options->path, '', $options->recursive, isset($options->password));
        $zip->setArchiveComment($options->comment);
        $zip->save();

        return $options->zipfile;
    }

    public static function unzip($options) {
        $zip = ZipProcessor::read($options->zipfile);
        if (isset($options->password)) {
            $zip->setPassword($options->password);
        }
        $zip->extractTo($options->destination);
        $zip->close();

        return TRUE;
    }

    public static function dir($options) {
        $zip = ZipProcessor::read($options->zipfile);
        if (isset($options->password)) {
            $zip->setPassword($options->password);
        }
        $entries = $zip->entries();
        $zip->close();

        return $entries;
    }

    public static function comment($options) {
        $zip = ZipProcessor::read($options->zipfile);
        $comment = $zip->getArchiveComment();
        $zip->close();

        return $comment;
    }
}

class ZipProcessor extends \ZipArchive
{
    public static function create($zipfile) {
        $zip = new ZipProcessor();

		if ($zip->open($zipfile, self::CREATE | self::OVERWRITE) !== TRUE) {
			throw new \Exception('Error creating zipfile ' . $zipfile);
		}

        return $zip;
    }

    public static function read($zipfile) {
        $zip = new ZipProcessor();

		if ($zip->open($zipfile) !== TRUE) {
			throw new \Exception('Error opening zipfile ' . $zipfile);
		}

        return $zip;
    }

    public function addFiles($files, $usePassword = FALSE) {
        $success = TRUE;

        foreach ($files as $file) {
            $success = $success && $this->addFile($file, basename($file));
            if ($usePassword) {
                $success = $success && $this->setEncryptionName($file, ZipArchive::EM_AES_256);
            }
        }

        return $success;
    }

    public function addDir($path, $localpath = '', $recursive = FALSE, $usePassword = FALSE) {
		$success = TRUE;

		$iterator = new \DirectoryIterator($path);

		foreach ($iterator as $entry) {
			if ($entry->isDot()) {
				continue;
			}

			if ($recursive && $entry->isDir()) {
                $this->addEmptyDir($localpath . $entry->getBasename() . '/');
				$success = $success && $this->addDir($entry->getPathname(), $localpath . $entry->getBasename() . '/', TRUE);
			}

			if ($entry->isFile()) {
				$success = $success && $this->addFile($entry->getPathname(), $localpath . $entry->getFilename());
                if ($usePassword) {
                    $success = $success && $this->setEncryptionName($file, ZipArchive::EM_AES_256);
                }
			}
		}

		return $success;
    }

    public function entries() {
        $entries = array();

        for ($i = 0; $i < $this->numFiles; $i++) {
            $stat = $this->statIndex($i);
            $entries[] = array(
                'type' => substr($stat['name'], -1) == '/' ? 'dir' : 'file',
                'path' => $stat['name'],
                'size' => $stat['size'],
                'compressedSize' => $stat['comp_size'],
                'compressionMethod' => $stat['comp_method'] == 8 ? 'Deflate' : 'None',
                'lastModified' => $stat['mtime']
            );
        }

        return $entries;
    }

    public function save() {
        return $this->close();
    }
}

undefined error returned @patrick

  1. code: 0
  2. file: “/var/www/vhosts/bendensonraya.com/httpdocs/dmxConnectLib/lib/zip/Processor.php”
  3. line: 115
  4. message: “Call to undefined method lib\zip\ZipProcessor::setEncryptionName()”

Do you have PHP 7.2 or higher?

yes higher
PHP/7.2.24

Test the example from the the php documentation on your server.

https://www.php.net/manual/en/ziparchive.setencryptionname.php

It is possible that the ZIP extension for PHP is not compiled correctly. It needs to be compiled with libzip-dev >= 1.2.0.

1 Like

yes I’m looking for libzip for plesk

Hello @patrick,

Php version : 7.3.12
now I can upload any file and save it as ZIP file. but an encrypted zip file does not occur. (I cant set password to zip file)

Current Libzip Version 1.3.2

Processor.php

Serverconnect Steps and JSON detail

there isnt any error . can you help ?

Did you test with the sample from https://www.php.net/manual/en/ziparchive.setencryptionname.php

Yes example is working

In the zip step, shouldn’t it be {{upload1.path}} instead of just {{upload1}}

zip file created without password

Try changing in Processor.php:

$this->setEncryptionName($file, ZipArchive::EM_AES_256);

to:

$this->setEncryptionName(basename($file), ZipArchive::EM_AES_256);