Can't create a zip archive with utf8-named cyrillic file - php

I try to create zip-archive with file фыфыфы.cs next way:
$zip = new ZipArchive;
if ($zip->open($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . '111.zip', ZIPARCHIVE::CREATE) === TRUE) {
$zip->addFile($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'фыфыфы.cs');
$zip->close();
echo 'ok';
} else {
echo 'error';
}
But the archive wasn't created. I tried to view the number of files in the archive after addFile call with:
die($zip->numFiles);
But I got 0.
When I renamed file to zzz.cs, the archive was created successfully. What's wrong?
I use Windows 8 at my PC.
I tried this solution, but it didn't work for me. A got the empty $encoded_filename.

for fix this I had to encode the $filename yet again to a different encoding, CP858.
Example:
$filename = "фыфыфы.cs";
//encode to windows-1252 to save to the filesystem
$encoded_filename = iconv("UTF-8","Windows-1252//IGNORE",$filename);
file_put_contents($encoded_filename, "text");
//put in a zip file
$zip = new ZipArchive();
$zip->open('test.zip', ZIPARCHIVE::CREATE);
//encode as CP858 to save into zip file
$zip->addFile($encoded_filename, iconv("UTF-8", "CP858//IGNORE", $filename));
$zip->close();
You can implement this method for your code.

Related

cannot add xml file to zip laravel

I want to create a zip folder that includes an xml file and download it. I can download zip but it is empty.
I can create xml and zip :
By the way, $result is the result of ArrayToXml::convert(array,....)
$zip = new ZipArchive;
$fileName = 'example-'.time().'.xml';
$zipName = 'example'.time().'.zip';
if ($zip->open(public_path("storage/zips/".$zipName), ZipArchive::CREATE) === TRUE)
{
Storage::disk('public')->put('/files/'.$fileName, $result);
$zip->addFile(public_path("storage/files/".$fileName), $result);
Storage::disk('public')->put('/zips/'.$zipName, $zip);
$zip->close();
}
return response()->download(storage_path('app\public\zips\\'.$zipName));
How to add xml file to zip. I am new to laravel please help
Have a look at the Madzipper package. Makes working with zip files very easy.
You can create a zipfile as follows:
$fileName = 'example-'.time() . '.xml';
$zip = 'public/zips/example' . time() . '.zip';
Madzipper::make($zip)->addString($fileName, $result)->close();
And then return the path to $zip when downloading the file.
Please have a look at the docs for more info.

Symfony 2: Download multiple images files in a Zip one

I'd like download multiple files in a zip but it doesn't work: the downloaded zip folder is still empty and is like opt/lampp/htdocs/MYPROJECT/... which one I'd like simple like /filename.zip
my controller, I just get all the file names from the DB and concatenate them with the directory name where they are physically saved:
$response = new Response();
// Get the entity manager
$em = $this->getDoctrine()->getManager();
//Collect all the result from the database and zip all the files related
$files = $em->getRepository('IballotCmsBundle:Result')->findAll();
$directory = __DIR__.'/../../../../web/uploads/pinkSheet';
//$directory = __DIR__.'/../temp/de.jpg';
$filename = 'all.zip';
$zip = new ZipArchive;
$zip->open($filename, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($directory.'/'.$file->getImage());
}
$zip->close();
$response->headers->set('Cache-Control', 'private');
$response->headers->set('Content-type', mime_content_type($filename));
$response->headers->set('Content-Disposition', 'attachment; filename="' . basename($filename) . '"');
$response->headers->set('Content-length', filesize($filename));
// Send headers before outputting anything
$response->sendHeaders();
$response->setContent(readfile($filename));
return $response;
I think it has to do with this line of code:
$filename = 'all.zip';
$zip = new ZipArchive;
$zip->open($filename, ZipArchive::CREATE);
You are trying to create a zip file called all.zip but you haven't specified the actual path. $filename should probably be something like this:
$filename = '/absolute/path/to/all.zip'
As for the structure of the zip file you'll need to read the docs which has a great example:
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->addFile('/path/to/index.txt', 'newname.txt');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
This will add the /path/to/index.txt file to the test.zip with the name newname.txt instead of /path/to/index.txt. You can use the second argument to control the directory (have a look at the comments in the docs for more info)

Zip file add and rename with php

I have this piece of code..,everything works fine ,but an issue with renaming
$zip = new ZipArchive();
$zipPath = 'images/userfiles/'.$company_details->company_name.'_products.zip';
$emptydir = $company_details->company_name.'_product_logos';
if ($zip->open($zipPath, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)) {
$new_filename = substr($my_file, strrpos($my_file, '/') + 1);
$zip->addFile($my_file, $new_filename);
$zip->addEmptyDir($emptydir);
foreach($parts_list as $pl) {
if(!empty($pl['part_image'])){
if (file_exists($_SERVER['DOCUMENT_ROOT'] . $baseurl . '/images/group-logo/' . $pl['part_image'])) {
$img_file = 'images/group-logo/' . $pl['part_image'];
$new_filename2 = substr($img_file, strrpos($img_file, '/') + 1);
$zip->addFile($img_file,$emptydir . '/' . $new_filename2);
/*********the problem here******
Is there any way to rename the file that i added on the previous line to something else ,already tried zip rename and renameindex but not working
for example i want to rename the file $new_filename2 to $new_filename2.'something' with out affecting the original file's name
P.S the files to be renamed are inside another folder in the zip
*******************************/
}
}
}
$zip->close();
}
Since you do not want to effect the original file I would think that you are going to need to include a copy statement. Something like;
$file = '$new_filename2';
$newfile = '$new_filename2.zip';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
Okay i figured it out
Zip creates a lock on the file..you cant rename on the fly ,close it and rename again
$zip->addFile('.....');
....
$zip->close();
and open zip again and rename

PHP ZipArchive download working but not adding files to zip

I know there is many questions regarding the ZipArchive and how it works but after following all these I still can't get my program to work.
I don't know where i'm going wrong because I have die methods in and none of them are getting activated.
$path = "/export/scripts/CLOUD/logs/web/Private_Testbox/Jan_28_2013_16_20_44_atvts78_rollout_config/";
$fileName = explode('/', $path);
$zipname = $fileName[count($fileName) - 2];
$zip = new ZipArchive;
$handle = opendir($path);
//Check whether Zip can be opened
if ($zip->open($zipname, ZIPARCHIVE::CREATE) !== TRUE) {
die("Could not open archive");
}
//Add all files to an array
while ($file = readdir($handle))
{
$zip->addFile($path, $file);
}
closedir($handle);
$zip->close();
//Send zip folder
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipname . '.zip');
readfile($zip);
This file downloads the zip folder but there is never anything in it.
Thanks for help in advance.
You forgot a die() on the ->addFile() call, which means you're assuming that the files actually got added.
readfile() does not return the full path to a file in a directory, only the actual "local" filename, which means you're adding files which don't exist in the script's current working directory. Try:
$zip->addFile($file, $path . $file);
^^^^^^^^^^^^^^^^^^^^^
instead. As it stands now, your code is using $path as the filename to embed in the zip, and directories cannot be used as filenames. And without the $path . $file, addFile is looking in the wrong spot, e.g. it's the equivalent of getcwd() . $file instead.

Dynamically creating zip with php

What is the easiest way of creating dynamically zip files with php?
For example,
I have these files on the server,
Root -> Folder 1 -> file1.wav
Root -> Folder 2 -> file2.jpg
I want to create a zip file contains these 2 files and allow user to download it.
any help ?
Thx in advance
See the ZipArchive class; it has what you need.
http://www.php.net/manual/en/class.ziparchive.php
Example from PHP.Net:
<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->addFile('/path/to/index.txt', 'newname.txt');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
After zipping it, you can output a mime-type header and output the file:
header('Content-type: application/zip');
readfile('test.zip');
this is the working example of making zip in php
$zip = new ZipArchive();
$zip_name = time().".zip"; // Zip name
$zip->open($zip_name, ZipArchive::CREATE);
foreach ($files as $file) {
echo $path = "uploadpdf/".$file;
if(file_exists($path)){
$zip->addFromString(basename($path), file_get_contents($path));
}
else{
echo"file does not exist";
}
}
$zip->close();

Categories