Compress with gzip a created XML file - php

For creating xml-files I use the DOMObject. But all the created xml-files are big and would cause a heavy bandwidth.
This is the way I save the xml-file
$xml->save($filename);
How could I add a gzip compression?
EDIT:
This snippet doesn't work because it creates an empty file
$gz = gzopen($filename,'w');
gzwrite($gz, $xml);
gzclose($gz);

Here is just a .zip implementation.
$file = '...';
$folder = 'folder';
$zip = new ZipArchive();
$fileName = "some_file.zip";
if ($zip->open($fileName, ZIPARCHIVE::CREATE)!==TRUE) {
throw new Exception('Can not create zip file.');
}
$zip->addEmptyDir($folder);
if (file_exists($file)) {
$zip->addFile($file, $folder . "/" . basename($file));
}
$zip->close();

Related

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

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.

PHP Download Multiple Files in Zip folder [duplicate]

How can I download multiple files as a zip-file using php?
You can use the ZipArchive class to create a ZIP file and stream it to the client. Something like:
$files = array('readme.txt', 'test.html', 'image.gif');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
and to stream it:
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
The second line forces the browser to present a download box to the user and prompts the name filename.zip. The third line is optional but certain (mainly older) browsers have issues in certain cases without the content size being specified.
This is a working example of making ZIPs 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();
Create a zip file, then download the file, by setting the header, read the zip contents and output the file.
http://www.php.net/manual/en/function.ziparchive-addfile.php
http://php.net/manual/en/function.header.php
You are ready to do with php zip lib,
and can use zend zip lib too,
<?PHP
// create object
$zip = new ZipArchive();
// open archive
if ($zip->open('app-0.09.zip') !== TRUE) {
die ("Could not open archive");
}
// get number of files in archive
$numFiles = $zip->numFiles;
// iterate over file list
// print details of each file
for ($x=0; $x<$numFiles; $x++) {
$file = $zip->statIndex($x);
printf("%s (%d bytes)", $file['name'], $file['size']);
print "
";
}
// close archive
$zip->close();
?>
http://devzone.zend.com/985/dynamically-creating-compressed-zip-archives-with-php/
and there is also php pear lib for this
http://www.php.net/manual/en/class.ziparchive.php

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)

Symfony cannot open the downloaded zip file

I'm trying to make a zip file download. So I try to make the code like this :
$zip = new ZipArchive();
$create = $zip->open($zipName, ZipArchive::CREATE);
if ($create === TRUE) { // check if the zip file is created
$basePath = $this->container->getParameter('kernel.root_dir').'/../marks/';
foreach ($listToken as $token) {
$file = $repoMarks->findByToken($token);
if($file) {
$fileName = $file[0]->getNameOnServer();
$filePath = $basePath . $fileName;
$root = realpath($this->container->getParameter('kernel.root_dir') . '/../marks');
$filePath = $root . '/' . $fileName;
if (file_exists($filePath)) {
$zip->addFile($filePath, $fileName);
}
}
}
$zip->close();
$root = realpath($this->container->getParameter('kernel.root_dir') . '/../marks');
$zipFilePath = $root . '/' . $zipName;
// prepare BinaryFileResponse
$response = new BinaryFileResponse($zipFilePath);
$response->trustXSendfileTypeHeader();
$response->headers->set('Cache-Control', 'public');
$response->headers->set('Content-type', 'application/zip');
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_INLINE,
$zipName,
iconv('UTF-8', 'ASCII//TRANSLIT', $zipName)
);
return $response;
}
I think it was successful. But, when I tried to open the zip file, There is an error like this An error occurred while loading the archive.
then I tried to make the code like this
$zip = new ZipArchive();
$create = $zip->open($zipName, ZipArchive::CREATE);
if ($create === TRUE) { // check if the zip file is created
$basePath = $this->container->getParameter('kernel.root_dir').'/../marks/';
foreach ($listToken as $token) {
$file = $repoMarks->findByToken($token);
if($file) {
$fileName = $file[0]->getNameOnServer();
$filePath = $basePath . $fileName;
$root = realpath($this->container->getParameter('kernel.root_dir') . '/../marks');
$filePath = $root . '/' . $fileName;
if (file_exists($filePath)) {
$zip->addFile($filePath, $fileName);
}
}
}
$zip->close();
header('Content-Type', 'application/zip');
header('Content-disposition: attachment; filename="' . $zipName . '"');
header('Content-Length: ' . filesize($zipName));
readfile($zipName);
}
but I got nothing. The same thing also happen when i change it to this :
$zip = new ZipArchive();
$create = $zip->open($zipName, ZipArchive::CREATE);
if ($create === TRUE) { // check if the zip file is created
$basePath = $this->container->getParameter('kernel.root_dir').'/../marks/';
foreach ($listToken as $token) {
$file = $repoMarks->findByToken($token);
if($file) {
$fileName = $file[0]->getNameOnServer();
$filePath = $basePath . $fileName;
$root = realpath($this->container->getParameter('kernel.root_dir') . '/../marks');
$filePath = $root . '/' . $fileName;
if (file_exists($filePath)) {
$zip->addFile($filePath, $fileName);
}
}
}
$zip->close();
header("HTTP/1.1 303"); // 303 is technically correct for this type of redirect
header("Location: http://{$_SERVER['HTTP_HOST']}/" . $fileName);
}
is there anyone who can help me to solve this download zip file problem?
An error occurred while loading the archive. occured in your clients side is because :
Your client doesn't have any application to open zip file.
zip file corrupt or missing extension.
There is possibility you never updated / fresh install. Try to
update it sudo apt-get update
Make sure your downloader app (like IDM or flareget) is working good. (I have problem with this, and when I disable the downloader app, it works) -By Asker
It is problem with client side, (connection or program error) or with the file it self. Try open the file using another PC.

Download multiple files as a zip-file using php

How can I download multiple files as a zip-file using php?
You can use the ZipArchive class to create a ZIP file and stream it to the client. Something like:
$files = array('readme.txt', 'test.html', 'image.gif');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
and to stream it:
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
The second line forces the browser to present a download box to the user and prompts the name filename.zip. The third line is optional but certain (mainly older) browsers have issues in certain cases without the content size being specified.
This is a working example of making ZIPs 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();
Create a zip file, then download the file, by setting the header, read the zip contents and output the file.
http://www.php.net/manual/en/function.ziparchive-addfile.php
http://php.net/manual/en/function.header.php
You are ready to do with php zip lib,
and can use zend zip lib too,
<?PHP
// create object
$zip = new ZipArchive();
// open archive
if ($zip->open('app-0.09.zip') !== TRUE) {
die ("Could not open archive");
}
// get number of files in archive
$numFiles = $zip->numFiles;
// iterate over file list
// print details of each file
for ($x=0; $x<$numFiles; $x++) {
$file = $zip->statIndex($x);
printf("%s (%d bytes)", $file['name'], $file['size']);
print "
";
}
// close archive
$zip->close();
?>
http://devzone.zend.com/985/dynamically-creating-compressed-zip-archives-with-php/
and there is also php pear lib for this
http://www.php.net/manual/en/class.ziparchive.php

Categories