Yii2 : Arabic language filename not support in `addFromString` of ZipArchive - php

I have used ZipArchive for creating zip. but i have problem with filename assign in Arabic language that is not supported.
I want to get filename like 21_بينل.pdf but i getting 21_.pdf.
I have refer following question.
PHP ZipArchive non-English filenames return funky filenames within archive
but still not working.
My sample code:
<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
$fileName = $v->certificateLetter->fileName.'/'.$v->dslStu->stu_unique_id.'_'.'بينل';
$zip->addFromString($fileName.'.pdf', $letterString);
$zip->close();
?>
Thanks in Advance.

Try this:
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
$fileName = $v->certificateLetter->fileName.'/'.$v->dslStu->stu_unique_id.'_'.'بينل';
iconv('CP1256', 'utf-8', $fileName).PHP_EOL;
$zip->addFromString($fileName.'.pdf', $letterString);
$zip->close();

Related

PHP : Why extracted zip filename is changed for Japanese characters in lhaplus software?

I am working on zip file creation and downloading using PHP (Laravel).
Zip created and download works well. But the problem is in lhaplus software. When extracting the zip file using lhaplus, it has been changing the file name. But in another extractor like 7zip, WinRAR works well. In lhaplus software shows "03510063220_00016501tñ+sÉìtñ+sÉì_spt.pdf" but it should be 03510063220_00016501社名社名_spt.pdf.
Please help in this case. Any suggestion is appreciated.
The code is given below:
$date_time = static::getCurrentTime();
$zip_name = $date_time."_spt";
$zip = new ZipArchive;
if(!file_exists('zip/downloadedZip')){
mkdir('zip/downloadedZip',0777,true);
}
$zipFileName = 'zip/downloadedZip/'.$zip_name.'.zip';
if ($zip->open(public_path($zipFileName), ZipArchive::CREATE) === TRUE){
$fileName = "pdf/03510063220_00016501社名社名_spt.pdf";
$baseName = "03510063220_00016501社名社名_spt.pdf";
if (!$zip->addFile($fileName, mb_convert_encoding($baseName, "SJIS", "UTF-8"))) {
throw new \RuntimeException(sprintf('Add file failed: %s', $fileName));
}
$zip->close();
}
return URL($zipFileName);

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();
?>

Trying to Use PHP's Zip Archive - Extract To Doesn't Seem to be Working

Would appreciate some help on this. I've been trying to write a PHP script that Unzips a zip file that has been created using PHP's in-built Zip Archive extension.
The zipping-up process has been very straight forward but now I'm trying to unzip this to a particular folder and it doesn't seem to be working. The only thing it does is create the folder I've asked it to extract to. No files appear in that folder.
I've had no error messages.
Thanks in Advance. Here's my code:
<?php
$root = str_replace('public_html', '', $_SERVER["DOCUMENT_ROOT"]);
$path = $root.'scripts.zip';
$zip = new ZipArchive;
$zipped = $zip->open($path, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
$folder = $root.'public_html/scripts/';
if ($zipped) {
$extract = $zip->extractTo($folder);
if ($extract){
echo 'Zip File Extracted';
}
$zip->close();
}
?>
I would modify your code to look like the one below.
What you were doing was creating a new zip file, not extracting the old one. ZIPARCHIVE::CREATE instructs the code to create a zip, ZIPARCHIVE::OVERWRITE tells it to overwrite existing files in the zip.
So you are not extracting anything from the zip. This code below should work
<?php
$root = str_replace('public_html', '', $_SERVER["DOCUMENT_ROOT"]);
$path = $root.'scripts.zip';
$zip = new ZipArchive;
$zipped = $zip->open($path); /*I have removed the ::CREATE & ::OVERWRITE methods*/
$folder = $root.'public_html/scripts/';
if ($zipped) {
$extract = $zip->extractTo($folder);
if ($extract){
echo 'Zip File Extracted';
}
$zip->close();
}
?>
Hope that helps

zip files contain UTF-8 content

I'm using this function to zip files.
function zip($path, $file){
$zipname = getFilename($path.$file).'.zip';
$zip = new ZipArchive();
$zip->open($path.$zipname, ZipArchive::CREATE);
$zip->addFile($path.$file, $file);
$zip->close();
return $zipname;
}
Lately, I found that it has an error with files contain utf-8 content (filename also). The result is something similar to:
Thùc hiÖn c«ng v¨n sè 775-CV/QU, ngµy 30 th¸ng 11 n¨m 2009 cña
QuËn uû §å S¬n vÒ viÖc chuÈn bÞ b¸o c¸o t×nh h×nh
How could I fix it?

PHP + ZipArchive + Russian language

i have the followed code
$zip = new ZipArchive;
$res = $zip->open('tmp/articles.zip');
if ($res === TRUE) {
$zip->extractTo('tmp/');
$zip->close();
}
It works fine for archives with english filenames, but if i made archieve with russian characters, i have unreadable filenames. What should I do?
UPD: It doesn't work correctly when i use "unzip" from bash too.
Look at this code:
$z = new ZipArchive();
$res = $z->open('C:\Temp\Temp.zip');
if ($res)
{
$z->extractTo('C:\Temp\Temp');
$z->close();
}
$files = scandir('C:\Temp\Temp');
foreach ($files as $filename)
{
print iconv('cp866', 'utf-8', $filename).PHP_EOL;
}
This code prints normal filenames.
So, WinRar uses old ms-dos charset 'cp866' for Cyrillic.
Hope you can change this code to create rename algorithm :)
This method not always works. I've found myself installing an external library like 7zip to do this job and it soves the problem. It's hard to know the encoding charset used for a file in windows.
In my case I had to do this for avoiding errors in filenames after extraction.
Here are my variables:
// Extract file.
$tmp_dir = uniqid();
$zip_uri = "test.zip";
$destination_dir = "C:\\Users\\user\\AppData\\Local\\Temp\\$tmp_dir";
This is the ZipArchive method:
$zip = new ZipArchive();
$zip->open($zip_uri);
if (!$zip->extractTo($destination_dir)) {
die("Error extracting files.");
}
$zip->close();
This method not always worked, but the following does work beautifully (using 7Zip):
exec("C:\\7zip\\7za.exe e $zip_uri -o$destination_dir");
Hope it helps somebody to not spend hours trying with to figure out the encoding charset of a particular zipped file.

Categories