I have a folder with files, and at the click of a button from an application, I would like to create a zipped version of the folder. I understand that it's possible to create a tar.gz version on a UNIX system by passing in the command exec(tar -cvf foldername destination_filename).
Question: Is it possible to create a zipped file on a UNIX system? If it is possible, what's the logic/command behind it?
Most *nix systems will support the following commnad:
zip -r destination_filename.zip foldername
Use php zip extension? http://www.php.net/manual/en/class.ziparchive.php
And i guess you'll find useful classes from phpclasses too: http://www.phpclasses.org/search.html?words=zip&x=0&y=0&go_search=1
You need to pass the -z option to tar to have it also zip the file:
tar -czf foo.tar.gz foo/
Make sure you are in a place that the webserver has write privileges. For example, you may need chdir into the parent folder of the folder you wish to zip, and then get a temp file name in /tmp, and then create the command to zip to that temp file name.
It sounds like you have the rest figured out!
http://www.gnu.org/software/tar/manual/tar.html
Related
I have a Symfony 2 project with an upload folder (uploads works well). I'm creating a ZIP file after the upload which I put inside the same folder as the uploaded file (so the archive is created from the PHP script).
But i cannot extract the archive afterwards under MAC OS. The error is the following ("100" beeing a folder inside the upload folder):
What does that mean in MAC OS ? I suspect this is a directory permission issue. If i move and try to extract the ZIP on my Desktop it does not work either. I don't know in which direction to look. I've noticed if i try to do the same thing on another PHP project on which i DIDN't mess with the permissions required when installing symfony, the script and ZIP extraction works...
Updates :
Folder/files permissions :
Sudo unzip result :
Any ideas ? Thanks in advance,
Got it working ! NEVER trim() a ZIP file contents before saving it somewhere on your server. It seems to corrupt the archive.
I have a little problem, I have a large 41gb file on a server and I need to extract it..
How would i go about it, the file is in a tar.gz format and it will take 24hr on a godaddy server and then it stops for some reason
I need to exclude a folder name data this contains the bulk of the data 40.9gb the rest is just php.
home/xxx/public_html/xxx.com.au/data << this is the folder I don't need
I have been searching google and other sites for day's but it doesn't work..
shell_exec('tar xvf xxx_backup_20140921.tar.gz'); this is the command I use I have even used the 'k' to skip files and it dont work
I have used the -exclude command but nothing,
Try this:
shell_exec("tar xzvf xxx_backup_20140921.tar.gz --exclude='home/xxx/public_html/xxx.com.au/data'");
This should prevent the path listed (relative to the root of the archive) from being extracted.
I have 16631 files hosted in a webserver, 2719 of them are text files that contains a list of specific files located on the server.
Using PHP, is possible to create a ZIP for each text file?
could be the text file name as ZIP file name?
I want to keep the same directory structure in the ZIP file.
Thanks in advance.
Taking the suggestion from Jeff Hines, you could use the ZipArchive class to create the zip.
In Pseudo code
Get List of Text Files in your folder
Read Text file and get list of files to add to zip
Using ZipArchive add each listed file to your new zip file.
Write flag to specify you've done this file.
You might need to run it on 10 files at a time since it may time out.
hey guys , im dealing with a script that it should compress and folder and make it ready in 3 versions :
7z
zip
tar.gz
surely i did found a way on how to make a zip file out of a folder in php by using a zip compression class
http://www.phpclasses.org/package/2322-PHP-Create-ZIP-file-archives-and-serve-for-download.html
but for 7z and tar.gz format no idea on how to do the compression
is there any way to make an 7z or tar.gz compressed file !?
specially 7z format
I suggest you install the relevant programs on the server, and call them from php via exec() to compress your files for you. Why bother rewriting the compression code when it's already been written?
Assuming Linux and having these (basic) tools already available:
exec("zip -r archive_name.zip directory_to_compress");
exec("tar -zcvf archive_name.tar.gz directory_to_compress");
For 7zip, you can check out this project on SourceForge.
I have designed an online editor with which a user can create files and folders on the server. Suppose a user creates the below files and folders.
/docs/abc.txt
/docs/def.txt
/docs/work/assignment.txt
I want to write a PHP script to combine the files into a single ZIP file so that it makes it easier for the user to download them with one click.
The only purpose is that I should get a single file with the directory hierarchy maintained which the user can download and uncompress on his system to get the original files and directory structure back. I don't care even if the ZIP file is not compressed.
Note that I am using a shared server and do not have access to execute external commands.
ZipArchive class is what you need. Using this you can add directories and file to an archive.
This is a common requirement and has been solved. Try and check out this class: http://www.phpclasses.org/browse/package/2322.html
More tutorials at:
http://davidwalsh.name/create-zip-php <-- this has a similar example if you scroll down
http://www.w3schools.com/php/php_ref_zip.asp
Assuming you have permission to execute an external command, you could use exec to run an external command-line capable ZIP program such as 7-Zip.