PHP Zip non-empty folder with all files and sub-folders - php

I need to zip non-empty folder with all files and sub-folders. I found lots of classes and functions, but all of them zip whole directory structure or something like that. But I need to zip only the content of given folder.
In advance thanks for answers

Related

zip renameName function is not renaming files in sub directories

I am compressing directories and sub directories using PHP zip library. Everything is working fine except renaming files under sub directories. This code is renaming the files successfully in root directory
$zip->addFromString(basename($file), $download_file);
$zip->renameName(basename($file), 'nicename.mp3');
but it is not renaming files in sub directory. Can anybody assist please ?
I have taken this code from Here

ci zip encoding class not working properly

I want to crate a zip file and download it.
So my code is as follow;
Since i am using ci zip encoding class
my folder structure is like
c:/xampp/htdocs/gallery/print/oid_1/1.jpg,2.jpg,3.jpg
My code
$path = getcwd().'/print/oid_1/';
$this->zip->read_dir($path,FALSE);
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('oid_1.zip');
Here is my problem,
l C:\Users\instinctclassic\Downloads\Compressed\oid_1.zip: The archive is either in unknown format or damaged so what does this means -does it means it was mistaken while making zip file or it was mistaken while downloading.For downloading mistake i have downloaded the zip file many time enough to be sure.
2 I repaired the downloaded zip file(oid_1) and extract it but the extracted folder structure is not ignored before the oid_1 folder as said in ci zip encoding class tutorial
$this->zip->read_dir($path, FALSE);
//This will create a ZIP with the folder "directory" inside, then all sub-folders stored correctly inside that, but will not include the folders /path/to/your.
3 Assuming my folder oid_1 already exists somewhere on server.
I know this question is previously asked but mine exist all the problem from making zip file to extracting.So i am sorry for this.
Thanks
Try directory path like this:
$path = getcwd().'\print\oid_1\';
this way path will be proper, because getcwd() will return you c:\xampp\htdocs\gallery

PCLZip - Is it posttible to rename a folder in an archive before extraction?

I've a situation where I am downloading a zip and need to rename one of it's folders before I extract it. Is this possible using PCLZip?
You can simply remove all the folder locations from each file using this PCLZIP_OPT_REMOVE_ALL_PATH
Regards.

Get a ZIP and extract contents OR file_get_contents individual files and then file_put_contents into folder

I'm writing a script that will grab several files and then copy them into a new directory. I've come up with two methods of doing this but want to know which is better.
1)
store several file names in an array and use file_get_contents in a loop to get those files.
same again, but using file_put_contents to copy these files to a new directory.
2)
Store these files needed in a .ZIP file by default
Use PHP to open this zip and extract the contents to the new folder.
I'm guessing that the ZIP method is better to use but I have no evidence to back that up. Can anyone advise me on what's best?
Store the files to copy in an array and use PHP's copy() function to copy the files to the desired directory.

Skip "root" directory when extracting using ZipArchive class?

I am attempting to use the ZipArchive class to unzip the contents of a zip containing a GitHub repository.
When you download a zip from GitHub, the zip contains a folder at the root of the zip named something like "project-2302392-20230"... Then within that folder are the actual contents of the repository.
Is it possible extract everything within that project folder without extracting the folder itself?
Here's one way to figure out the name of the unique top directory of the github zip archive.
use getArchiveComment() to get the ziparchive comment.
use strpos to strip away all characters except the first 7 and also add the git username and repo name like so '/USER-REPO-'.substr($comment, 0, 7);
You could also use regex, scandir, and a number of other ways.

Categories