Compressing and extracting a Directory with all its files using PHP - php

How to compress or zip a directory and then extract alongside with its files and and sub directories using php script.
i have a directory which is needed to be compressed,then transferred to another server and then extracted into a new directory with new name given to it.
PATH.'/'.$TO.'/'.$DIR_WITH_DIR_NAME

First of all if you want to perform zipping and unzipping operations using php it is must to install php zip extension,as i am using Ubuntu as my OS and php7.0 on my server i installed it using this command.
sudo apt-get install php7.0-zip
For zipping parent directory and other sub directories along side with its files you can use this method given in approved answer.i have used it myself and its best to do so.
Transfer file using php cURL to other server.Now to unzip a folder in a new folder whichever you are trying to extract files in,first create that directory using this php code.
#mkdir(PATH.'/'.$TO.$NEW.'/'.$DIR_WITH_DIR_NAME,0777,true);
This will create the directory and give it all read/write permissions
now use this code to extract a directory
$zip = new ZipArchive;
if ($zip->open(PATH.'/'.$TO.$COMPRESSED.'/'.$FILE.'.zip') === TRUE) {
if(is_dir(PATH.'/'.$TO.$NEW.'/'.$DIR_WITH_DIR_NAME)){
$zip->extractTo(PATH.'/'.$TO.$NEW.'/'.$DIR_WITH_DIR_NAME);
}
$zip->close();
echo 'extracted';
} else {
echo 'error in file extracting';
}
That's it all of the files will be extracted into newly created directory enjoy happy coding.

Related

php unzip treat folder name as file name on Linux

I write a program to unzip a file using ZipArchive's extractTo() function only. It runs ok on Windows wherease in Linux, it treats folder name as part of file name
Users give me two zip files: onepiece.zip and file.zip. They both have structures with several top-level folders and in each folder, there're only one jpg and one txt file.
When I run my program to unzip onepiece.zip, it works fine both under Windows and Linux.
However, if I run my program with file.zip, it still works fine under Windows but it will unzip strange result under Linux as follows:
20200618212731318\20200618212731318-2-1afa34bb-0cf8-450c-aeb6-e0bf2b95d5dc-Source.jpg
20200618212731318\20200618212731318-2-1afa34bb-0cf8-450c-aeb6-e0bf2b95d5dc-Source.txt
20200618202449404\20200618202449404-2-d3d15882-0e3b-4c07-b614-af8dd649246c-Source.jpg
20200618202449404\20200618202449404-2-d3d15882-0e3b-4c07-b614-af8dd649246c-Source.txt
20200618202436965\20200618202436965-2-1d5c128a-a145-406b-95d5-66aee0b1eb11-Source.jpg
20200618202436965\20200618202436965-2-1d5c128a-a145-406b-95d5-66aee0b1eb11-Source.txt
20200618202453983\20200618202453983-2-cc16f9e9-fa95-408f-8579-b9455c097b84-Source.jpg
20200618202453983\20200618202453983-2-cc16f9e9-fa95-408f-8579-b9455c097b84-Source.txt
20200618202506672\20200618202506672-2-78b44d00-65c3-4030-aa35-fe13b9b225df-Source.jpg
20200618202506672\20200618202506672-2-78b44d00-65c3-4030-aa35-fe13b9b225df-Source.txt
20200618204333943\20200618204333943-2-d9426906-ed40-4828-b6a8-bda202a10fa0-Source.jpg
20200618204333943\20200618204333943-2-d9426906-ed40-4828-b6a8-bda202a10fa0-Source.txt
As you can see, the folder name becomes part of filename. I doubt the problem comes from the zipping tool that Users use. Is there any solution or workaround to solve this problem?
Note: On Windows, I'm using php 5.5.30. On Linux, it's php 5.5.9.
This is the codes of unzipping file. It's quite simple without any particular codes or functions:
// Unzip uploaded zip file
$zip = new ZipArchive;
$res = $zip->open($old_zipfile_path);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($old_unzip_dir);
$zip->close();
echo "<font color=brown>'$old_zipfile_fullname' extracted to $old_unzip_dir</font><br>";
} else {
echo "<font color=red>OOPS!! I couldn't open $old_zipfile_path</font><br>";
}
echo '<br>';

How to copy artifacts generated by a build (php web app) to a directory on the same server?

We're using TeamCity 9 on Ubunutu system.
I want to copy the the artifacts published in the build to a directory (defined in Apache config as virtual directory) so that our application would get deployed.
Currently, our artifacts are being published in
root/.BuildServer/system/artifacts/repo_name/build_name/some_number/some_hash_value directory.
I want to copy it to, let's say, \home\ubuntu\repo_name directory.
How should I do it?
You can do it in 2 ways
cp -R -f %teamcity.build.workingDir%/* /home/ubuntu/repo_name/
Create a vcs checkout rule in your build target of the form .+:=>custom_name.This checks out VCS contents into a directory named custom_name.Then you can copy it using this command
cp -R -f %teamcity.build.workingDir%/custom_name /home/ubuntu/repo_name/
In case you only want to copy the artefacts, you can generate all your artefacts in one sub-directory under the build directory and copy files from that direcotry to the target directory
How about setting artifact path explicitly in the build configuration? You can read more here: https://confluence.jetbrains.com/display/TCD9/Configuring+General+Settings#ConfiguringGeneralSettings-ArtifactPaths

php ZipArchive - adding only files not the parent folder

I am creating a zip archive in php and want to add some pdf files available in my pdfs_lib folder.I have successfully created the archive but there is a problem that inside the created zip archive, files get added into pdfs_lib folder, which is not what i want.
I want to add pdf files present in pdfs_lib folder directly into the archive instead of adding those files inside pdfs_lib folder in my created zip archive.
What i have tried is:
// Create Zip File
$zip = new ZipArchive;
$res = $zip->open("pdfs_lib/my_zip_archive.zip", ZipArchive::CREATE);
if ($res === TRUE)
{
// Add pdf file to zip archive
$zip->addFile("pdfs_lib/pdf_file_1.pdf");
$zip->addFile("pdfs_lib/pdf_file_2.pdf");
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
This code creates the zip archive successfully but whwen i unzip the archive it creates a pdfs_lib folder and unzip all files inside this newly created folder.
Any suggestion on how to create a zip archive without add pdf_lib folder in it.
Change the addFile calls to:
$zip->addFile("pdfs_lib/pdf_file_1.pdf", "pdf_file_1.pdf");
The second parameter is the 'localname', the name used in the zipfile itself. If you leave out the folderpath there, it will be added to the root in the zipfile.
A side not as an into: usually it is considered good practice if an archive (be it zip or any other archive format) unpacks into a clean folder instead of littering the contained files all over the place.
However if you really want to implement this without such folder, then you must rework your code. You have two alternatives:
you can change the working directory your php script is executed in into that folder where you keep the files in. In that case you do not need any path to add files to the zip archive, thus no folder is constructed inside the archive.
the addFile() method of phps zip extension allows to specify a second (optional) argument which allows to pass the target name of a passed file.

PHP UnRar.exe into a specific directory

Ive just found this script and it is exactly what im looking for, However how can i tell it to extract the files into a specific folder?
Here is the script
<?php
$winRAR = '"C:\Program Files\WinRAR\UnRAR.exe"';
$file="test.rar";
$do ="$winRAR e $file";
exec("$winRAR /?");
print_r($aOut);
exec($do,$aOut);
print_r($aOut);
?>
Im going to be extracting multiple files so i would like it to extract each archive into a a folder with the same name as the archive. So if the rar was called "Test" i want it to extract to a folder called /test/ and extract the files there?
Many Thanks in advance!
I suggest you to use the following command:
$do ="$winRAR x -ad $file $destinationPath";
Where $destinationPath is a destination path.
The "x" command will keep directory structure stored in archive in contrast with "e" command, which extracts files without subdirectories.
The "-ad" command line switch tells unrar to extract archive foo.rar to the $destinationPath/foo directory.
Command line commands and switches are listed in WinRAR help file, in section "Command line mode".
I think what you are trying to do may be easier using PHP's RAR extension. http://www.php.net/manual/en/book.rar.php

Extract the zip file using php script

I have a zip file and i used the following code to extract that zip and put all extracted file in another location.
$zip = new ZipArchive;
echo $zip;
if ($zip->open("$pwd/wordpress-3.4.2.zip") === TRUE) {
$zip->extractTo("$pwd/Repo/");
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
But i cant any extracted file whether it show 'ok' in browser. What is the mistake in the code i cant found. another thing to download the zip file from the "http://wordpress.org/latest.zip" site. i used the code written bellow. Here also i cant download the file.
$foo = system('wget http://www.myserver.com/file.txt ~',$output);
You could also use:
exec('wget http://wordpress.org/latest.zip -O temp.zip');
exec('unzip temp.zip -d /somedir');
It appears that the ZipArchive needs a reference to the library containing that class.
Also make sure that in your php.ini file, you have enabled the library that enables zip files functions to work on your server. (The zip extension is loaded by default under PHP 5.3)
Again, make sure the directory you want to extract the files has write permissions.

Categories