Unzip File On Google Drive Using PHP - php

I'm developing a web app where users could upload .zip file and storing it in the Google Drive. The upload part was easy but I have no clue how to unzip the file uploaded.
I found this but it's in Javascript and am still trying to find out how to unzip the file using PHP. Anyone knows how to accomplish this?

I guess you can use the ZipArchive class, i.e.:
$zip = new ZipArchive;
$res = $zip->open('myfile.zip');
if ($res === TRUE) {
$zip->extractTo('/extract/path/');
$zip->close();
echo 'ok!';
} else {
echo 'error!';
}

Didn't find a solution, I don't think unzipping files on Drive using PHP is possible by now. So instead of uploading .zip, I extract the zip to temp folder then loop to read the files. Here are the steps I do :
loop items in folderId as item
if item == folder
create folder on Drive
fetch folder id as folderId
loop(folderId)
else if item == file
file.parent = folderId
upload file
endif
endloop

Related

ZipArchive - Problem with the generated archive file

I've got a problem with the class : ZipArchive.
My ZIP file is well created and my folders and files are in the archive.
However, I've got 2 problems:
I can't extract a file from the generated archive unless if the file is located on the root of the archive;
If I extract the entire archive, the tree is deleted, the files are all at the same level, while the tree is good if I browse the archive with an archive manager;
I've tried to creating the folders first with $archive->addEmptyDir, but it doesn't change anything.
I think that It's an Index problem or something like this but I'm not sure.
Here's my code:
$archive = new ZipArchive;
foreach($files as $file_origin_path) {
if($error === FALSE) {
$error = !$archive->addFile($file_origin_path, str_ireplace($path, '', $file_origin_path));
}
}
$archive->close();
Would anyone have a way that would allow me to move forward?

Unable to extract zip file which generated using php

I have written the PHP script to generate the zip file. it's working fine when I use rar software to extract it but not getting extract with rar software. I can't ask to users to install rar software to extract downloaded zip file.
I don't know where i am commiting mistakes.
Here i attached error screen shot which i get when try to open zip file.
// Here is code snippet
$obj->create_zip($files_to_zip, $dir . '/download.zip');
// Code for create_zip function
//create the archive
$zip = new ZipArchive();
if ($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach ($valid_files as $file) {
$filearr = explode('/', $file);
$zip->addFile($file, end($filearr));
}
$zip->close();
If $valid_files is a glob'd array, use basename() instead of end(), your zip might not actually have added any files causing for it to be an invalid zip (however that would be visible in the size of the zip file).
Also try winrar/winzip/7zip and see what they return, microsoft's internal zip engine might not be up to date enough to open the zips.
I have also encountered this problem, using 7z solved the problem but we need to send the zip to somebody else so 7z is a nono.
I found that, in my case it is that the file path is too long:
When I use this:
$zip->addFile($files_path.'/people.txt');
And it generated a zip folder nested very deep e.g. ["/tmp/something/something1/something2/people.txt"]
So I need to use this instead
$zip->addFile($files_path.'/people.txt', 'people.txt');
Which generate a a zip folder with only 1 layer ["people.txt"], and Windows Zip read perfectly~
Hope this helps somebody that also have this problem!

Reading files from ZIP without extracting to disk

Is it possible to open a ZIP file on a server, read a file from its content and display it / send it to a client directly without extracting it to disk first ? I am talking about pdf's and images. Haven't found any hints in the php sites.
Well,there is a PHP Extension.
If you use the extractTo method, you would be able to extract a single file, checkout the documentation.
From the documentation, extracting two files:-
<?php
$zip = new ZipArchive;
$res = $zip->open('test_im.zip');
if ($res === TRUE) {
$zip->extractTo('/my/destination/dir/', array('pear_item.gif', 'testfromfile.php'));
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
You would need to provide an array of path inside the zip.

How to check content of zip folder before uploading?

I have gone through the various post regarding zip and to read zip that is extracting zip downloading it and then access it. But i want to check the format and name convention for the files enclosed in Zip file that is going to be uploaded. I am using drag drop feature here and planning that when user drop the zip file it should extract the file but don't store it any where and check that all the image uploaded in the zip are of same format and name convention are followed like img1.jpg, img2.jpg.
Any help is deeply appreciated. I am using codeignitor framework and is it possible to extract and manipulate zip using backbone or java script.
I don't think you can check the contents of the zip before uploading.. Here's a code , you can very well use to check if the content inside is legit. [In this example... Assuming img1.zip contains an image file img1.jpg]
Maybe a start
<?php
$zip = new ZipArchive;
$res = $zip->open('img1.zip');
if ($res) {
$legitImage=explode('.',$zip->statIndex(0)['name']);
if($legitImage[1]=='jpg')
{
echo "It's an image";
//do your operations
$zip->close();
}
else
{
unlink('img1.zip');//Delete the zip file since it does not contain image
}
}
?>

Codeigniter and uploading zip files

I am currently building a backend to a site using the codeigniter framework, I have hit a bit of a problem, I needing a way to allow the user to upload a zipped folder of images, on completing the form, zipped folder must be unzipped, and the files need to be moved to a folder else where on the server, have thumbnail version of each image created and have there file name add to the DB, and also if the images are for a content type that does not already exist then I need to make a directory with that content type.
I know there is an upload class in Codeigniter, but I am not sure that, that has the capabilities do what I need, I could really do with some advice please?
Thanks
As Jan pointed out, this is a broad question (like 3 or 4 questions). I'm not up to date with the CodeIgniter framework but to Unzip the files you can do something like this:
function Unzip($source, $destination)
{
if (extension_loaded('zip') === true)
{
if (file_exists($source) === true)
{
$zip = new ZipArchive();
if ($zip->open($source) === true)
{
$zip->extractTo($destination);
}
return $zip->close();
}
}
return false;
}
Unzip('/path/to/uploaded.zip', '/path/to/extract/');
You wont be able to do any of the image or file checking using the Upload class. The upload class will let you accept the file and check it is a ZIP but that's as far as it will go.
From there, unzip the file and just do some simple PHP on the files to check they are the right type and make your folders etc. I would put this logic in a new library to keep it separated correctly.

Categories