I have no idea if this is possible since there is no information available on the internet. I spend two hours searching without any result.
I am trying to zip a directory without knowing the file names that are in the directory. I tried many methods but they include the whole path, I only want the files in the zip.
I have tried the following:
$zipFile = "./testZip.zip";
$zipArchive = new ZipArchive();
if (!$zipArchive->open($zipFile, ZIPARCHIVE::OVERWRITE))
die("Failed to create archive\n");
$zipArchive->addGlob("./*.txt");
if (!$zipArchive->status == ZIPARCHIVE::ER_OK)
echo "Failed to write files to zip\n";
$zipArchive->close();
and:
$zip = new ZipArchive;
$zip->open('myzip.zip', ZipArchive::CREATE);
foreach (glob("target_folder/*") as $file) {
$zip->addFile($file);
if ($file != 'target_folder/important.txt') unlink($file);
}
$zip->close();
Related
I have a problem.
I searched the internet for a solution, but nothing works well. I'm trying to make a script that will take files from a folder and threw them into a zip archive.
But it doesn't work ..
I tried through different paths, but it's only me mixed up and nothing came of it.
Here is the current code that is according to me the easiest and should workbut doesn't do that ..
Can you help me?
function archivebackup(){
$zip = new ZipArchive;
if ($zip->open('Mail.zip', ZipArchive::CREATE) === TRUE){
foreach (new DirectoryIterator('Mails/') as $fileInfo) {
$fileName = $fileInfo->getFilename();
// echo $fileName ."<br>";
$zip->addFile($fileName);
}
$zip->close();
}
}
It seems you're not including files in your current working directory and getFilename() only returns a filename without a path.
Do the following:
function archivebackup(){
$zip = new ZipArchive;
if ($zip->open('Mail.zip', ZipArchive::CREATE) === TRUE){
foreach (new DirectoryIterator('Mails/') as $fileInfo) {
if (in_array($fileInfo->getFilename(), [ ".", ".." ]) { continue; }
$fileName = $fileInfo->getPathname();
$zip->addFile($fileName);
}
$zip->close();
}
}
I want create a zip in a specific Directory. Actually i can create a folder with this code:
function createZip($array)
{
$files = $array;
$zipname = 'ftpack.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
print_r($array);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
}
But this code create a zip in the principal directory, i want create in the at /zipDownload directory.
Just pass the full path:
$zipname = '/path/to/ftpack.zip';
Also make sure that the folder /path/to is writable by PHP.
i will create a Zip Archive with the PHP Class. But it still not working.
It comes no fail and the response is 1. He don't create the Zip-File.
$zip = new ZipArchive;
$res = $zip->open('qr_img/'.'ab387bas.zip'.'', ZipArchive::CREATE);
if ($res === TRUE) {
$zip->addFile($file, 'screen.png');
$zip->close();
}
know's everyone the answer ?
$file_names is the array of files which are you want to add in the zip.
function zipFile($file_names,$archive_file_name)
{
$zip = new ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::OVERWRITE )!==TRUE) {
}
//add each files of $file_name array to archive
foreach($file_names as $files)
{
// $zip->addFile($files,$files);
$zip->addFromString(basename($files), file_get_contents($files));
}
$zip->close();
}
Trying to get import/export functionality.
With a folder containing a file
Zip the folder in PHP using ZipArchive
Unzip the zipped folder using ZipArchive
New folder is created, but the file inside is missing
function unArchive(){
$zip = new ZipArchive;
$res = $zip->open('zipTest.zip');
if ($res === TRUE) {
echo 'ok';
$zip->extractTo("testfolder2");
$zip->close();
} else {
echo 'failed, code:' . $res;
}
}
function Zip($source, $destination){
if (!extension_loaded('zip') || !file_exists($source)){
return false;
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
$source = str_replace('\\', '/', realpath($source));
if (is_dir($source) === true){
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file){
$file = str_replace('\\', '/', realpath($file));
if (is_dir($file) === true){
$zip->addEmptyDir( str_replace($source . '/', '', $file . '/') );
}else if (is_file($file) === true){
$zip->addFromString( str_replace($source . '/', '', $file), file_get_contents($file));
}
}
}else if (is_file($source) === true){
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}
Zip( "testfolder/" , "./zipTest.zip" );
unArchive();
Code works fine for single files.
With a file zipped using ZipArchive, I can manually unzip using WinRAR, then rezip using WinRAR, and then ZipArchive can successfully unzip that file.
The problem is that ZipArchive won't extract the archive that it directly zipped.
I can also see in Notepad++ that the ZipArchive zip is different from the WinRAR zip, and the file size is different. I can successfully call winrar as a system command in php for the desired functionality, but this seems like a bad idea.
Is there some reason ZipArchive creates zip files that it can't extract?
Error message: ZipArchive::extractTo(): Invalid argument in [file] on line [$zip->extractTo("testfolder2");]
The RecursiveIteratorIterator returns the pseudo-files "." and, that's important here, ".." - the parent directory. When you use addEmptyDir to add the parent directory, the ZipArchive will add it (somehow)! You may even try "$zip->addEmptyDir("../something")" and it will not complain.
Now, usually, zip files don't behave this way. When you extract them, you expect all contents to be in the folder you extract them, not in the parent folders. This is why ordinary ZIP extraction programs do not show it.
The PHP ZipArchive will extract even these folders, however. Then, this error will somehow occur, because you tried to include the "." or ".." directories.
Add this at the beginning of the foreach cycle to resolve this:
if (basename($file) === ".") { continue;}
if (basename($file) === "..") { continue; }
Test case:
<?php
$archive = new ZipArchive();
$archive->open('c.zip',ZipArchive::CREATE);
$archive->addFile('a.txt');
$archive->addFile('b.txt');
$archive->addEmptyDir("../../down");
$archive->close();
$archive2 = new ZipArchive();
$archive2->open('c.zip');
$archive2->extractTo('here');
$archive2->close();
I tested this only on Windows.
I have some PHP code that I am using to try and zip a folder. The folder has two subfolders in it and several individual files.
Here is the code: -
<?php
$src = $_POST['srcin'];
$dst = $_POST['dstin'];
$zip = new ZipArchive;
$zip->open($dst, ZipArchive::CREATE);
if (false !== ($dir = opendir($src)))
{
while (false !== ($file = readdir($dir)))
{
if ($file != '.' && $file != '..')
{
$ans = DIRECTORY_SEPARATOR;
$zip->addFile($src.DIRECTORY_SEPARATOR.$file);
}
}
}
else
{
die('Can\'t read dir');
}
$zip->close();
echo json_encode('Folder Compressed');
?>
The input values are: -
srcin = "TestFolder"
dstin = "TestFolder.zip".
What is happening is that I am getting a zip file. However, the subfolders are being created as files.
I got the above code from searching this forum on how to ZIP a folder, yet I cannot see anything mentioned regarding subfolders not being zipped properly.
Any help is much appreciated.
Thanks
Martin
You should create a directory with addEmptyDir before you add a file to it.
Here is an example(see top comment) how to archive a directory recursively