Create a zip archive with a password - php

So, I creating a zip file with a password:
function createZip($fileName,$fileText,$zipFileName,$zipPassword)
{
shell_exec('zip -P '.$zipPassword.' '.$zipFileName.'.zip '.$fileName);
unlink($fileName);
return file_exists($zipFileName.'.zip');
}
$filex = "/backup/home/fyewhzjp/long_location_of_a_file/temp/data/map10/data.txt";
// $file_content = 'test';
$archive = "/backup/home/fyewhzjp/long_location_of_a_file/temp/data/map10/archive";
createZip($filex,$file_content,$archive,$pass);
And it works. I'm getting a archive.zip in my /temp/data/map folder on the website. But, when I open my archive I can see a bunch of folders, and data.txt at the end, let's say it will be
/backup/home/fyewhzjp/long_location_of_a_file/temp/data/map10/data.txt
So, I need to leave only data.txt in my folder, without other folders. How can I do it?

If anyone will face the same problem as I did, here is the solution:
Just add -jrq after zip in shell_exec like this:
shell_exec('zip -jrq -P '.$zipPassword.' '.$zipFileName.'.zip '.$fileName);
After that, full path will be ignored.

In addition to #Script47...
Pure PHP Available as of PHP 7.2.0 and PECL zip 1.14.0, respectively, if built against libzip ≥ 1.2.0.
<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
// Add files
$zip->addFromString('test.txt', 'file content goes here');
$zip->addFile('data.txt', 'entryname.txt');
// Set global (for each file) password
$zip->setPassword('your_password_here');
// This part will set that 'data.txt' will be encrypted with your password
$zip->setEncryptionName('data.txt', ZipArchive::EM_AES_128); // Have to encrypt each file in zip
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>

Rather than using shell_exec why don't you just use the ZipArchive class with the functions ZipArchiveOpen::open and ZipArchive::setPassword, it seems that that would make things a lot easier.
<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
$zip->addFromString('test.txt', 'file content goes here');
$zip->addFile('data.txt', 'entryname.txt');
$zip->setPassword('your_password_here');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
Note:
This function only sets the password to be used to decompress the archive; it does not turn a non-password-protected ZipArchive into a password-protected ZipArchive.

Related

how to unzip .zip file in codeigniter 4 (PHP)

I am trying to extract zip file in codeigniter 4... I have the code below
$file_name = session()->get('file_name');
// Zip file name
$filename = "/public/$file_name";
$zip = new \ZipArchive;
$res = $zip->open($filename);
if ($res === true) {
// Unzip path
$path = "/public";
// Extract file
$zip->extractTo($path);
$zip->close();
return 'Site Updated Successfully';
} else {
return "failed to update $filename!";
}
}
but I get this result: return "failed to update $filename!";
Please can someone help me fix the issue with the code.
This has nothing specifically to do with Codeigniter. It's just using PHP and ZipArchive. The code looks OK - be sure the file is really in the /public/ directory and that it is a zip archive. The PHP site has examples that are like $zip = new ZipArchive; (without the \ char); maybe try that.

Can't unzip file with php

I have a folder in my web server were I put zip files that I need to then unzip. I want to do that with php and this is what I have tried but it does not work:
<?php
$file = $_GET["file"];
$zip = new ZipArchive;
$res = $zip->open($file+'.zip');
$zip->extractTo('./');
$zip->close();
?>
The zip files are in the same folder as the php file, but when I go to the php page it does nothing.
By doing some testing I have found out that the script dies on the $zip = new ZipArchive; line
How can I manage this to work?
<?php
$fileName = $_GET['file']; // get file name in the URL param "file"
if (isset($fileName)) { // if $fileName php variable is set than
$zip = new ZipArchive; // create object
$res = $zip->open($fileName); // open archive
if ($res === TRUE) {
$zip->extractTo('./'); // extract contents to destination directory
$zip->close(); //close the archieve
echo 'Extracted file "'.$fileName.'"';
} else {
echo 'Cannot find the file name "'.$fileName.'" (the file name should include extension (.zip, ...))';
}
}
else {
echo 'Please set file name in the "file" param';
}
?>
Note:- For More Details Please refer https://www.php.net/manual/en/class.ziparchive.php
I have found the problem.
The code is fine, but the hosting service is not, and they do not have the ZIP extension available right now
Try this code. Also change $zip->open($file+".zip"); to $zip->open($file);.
+ (plus sign) is not concatenation operator in php
<?php
// $_GET["file"] is set to `a.zip`
$file = $_GET["file"];
$zip = new ZipArchive;
$res = $zip->open($file);
$zip->extractTo('./');
$zip->close();
?>

how to copy file content and directory data from zip archive using php

I am using php to open a zip file and read its content and then copy this content to some other location. I am stuck at one point that how to copy the file content(file is a json) and also have some images in other directories which I want to access.
$zip = zip_open(Input::file('file'));
$rzip = zip_read($zip);
$entr_open_zip = zip_entry_open($zip, $rzip, '');
if ($entr_open_zip) {
return "hello";
die;
} else {
return "hi";
die;
}
I have opened the file but don't know how to copy content or even how to open the directory and access files in that directory. Any help is appreciated. I will explain more if needed.Ignore the typos.
I have seen some questions related to it but not getting satisfactory solution.
ZipArchive in PHP have functions to handle .zip files. In your case to extract all contents to specific directory, extractTo function can make help.
Sample Code:
<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->extractTo('/my/destination/dir/');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>

This code compress a SQL file to zip using Php. I need to set password for the zip file produced

This code helps to make SQL file to a zip file in Php.
Here a SQL file is compressed to a zip file.
All I need is to set password for this.
Can I use PHP-java bridge to make this happen?
function dumpOutput() {
if (!class_exists('ZipArchive')) {
return array();
}
return array('zip' => 'ZIP');
}
function _zip($string, $state) {
// ZIP can be created without temporary file by gzcompress - see PEAR File_Archive
$this->data .= $string;
if ($state & PHP_OUTPUT_HANDLER_END) {
$zip = new ZipArchive;
$zipFile = tempnam("", "zip");
$zip->open($zipFile, ZipArchive::OVERWRITE); // php://output is not supported
$zip->addFromString($this->filename, $this->data);
$zip->close();
$return = file_get_contents($zipFile);
unlink($zipFile);
return $return;
}
return "";
}
It seems that the ZipArchive Class doesn't support password setting. It only supports to open a password protected zip file.
See the following page for more details.
http://php.net/manual/zh/ziparchive.setpassword.php

Get ZIP file from generated URL

I have a URL that, when requested, start the download of a .zip file. The URL looks something like this:
http://website.nl/servlets/ogexport?koppeling=WEBSITE&user=&password=&og=BOG&kantoor=**
How can I make sure that the zip file that gets generated in that proces get used in my PHP unzip script?
$file = 'testing.zip';
$path = pathinfo(realpath($file), PATHINFO_DIRNAME).'/xml';
// unzippen
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
$zip->extractTo($path);
$zip->close();
echo 'success';
// debug("ZIP bestand uitgepakt", 2);
} else {
// debug("ZIP niet uitgepakt", 2);
echo 'something went wrong.';
}
$xml_path = $path."/testing.xml";
echo $file.'<br>';
echo $xml_path;
Hoping for help!
I would recommend using php curl to download the remote file.Here is an example

Categories