5

I used this code {from : Unzip a file with php}

$zip = new ZipArchive;
$res = $zip->open('file.zip');
if ($res === TRUE) {
  $zip->extractTo('/myzips/extract_path/');
  $zip->close();
  echo 'woot!';
} else {
  echo 'doh!';
}

but it doesn't work with multi-part zip files. The names of my files are

file.zip
file.z01
file.z02 
This's for example :)

*Note : I can't run shell commands because I use shared hosting {Alternate to PHP exec() function}

  • To my knowledge `ZipArchive` in PHP can make multi-part zip files, but I don't think it has the ability to open one. I would say use the shell, but you've excluded that option. Another option is to simulate linux `cat` with PHP so it merges all files into one file, then it shouldn't be a problem. – Xorifelse Jul 18 '18 at 19:36
  • 1
    @Xorifelse Ok, reopened – RiggsFolly Jul 18 '18 at 19:41
  • @Xorifelse How can I do that ? Does it work in shared hosting? Do you have any link to be my guide? Sorry, because I am beginner and English isn't my tongue... – Mohamed Sallam Jul 18 '18 at 19:47
  • Gotta be honest here, I've looked around a bit couldn't find a direct solution other then merging the files to one file using cat. The [source code of Linux cat](https://git.savannah.gnu.org/cgit/coreutils.git/plain/src/cat.c). I didn't see a PHP function of it in the short while I looked (doesn't mean there isn't any), so.. trial and error and start scripting! (only focus on the part that doesn't use options/modifiers) (or move web host). – Xorifelse Jul 18 '18 at 19:50

1 Answers1

1

Here's how I did it... In my case, I had, say:

  • file.zip.001
  • ...
  • file.zip.006

This setup was a result of running the command 7z a -tzip -v5M Split/file file.zip, which basically just split the file up into concatenate-able files of about 5M in size...

This code merged them together, special thanks to this answer for some help:

<?php

for ($i = 1; $i <= 6; ++$i)
{
  $content = file_get_contents("file.zip.00" . $i);
  file_put_contents("file.zip", $content, FILE_APPEND);
}

echo "Merged! Make sure to delete...";

?>

Obviously, make sure "file.zip" doesn't exist or is empty beforehand...

Then, to unzip it, I ran this:

<?php

$zip = new ZipArchive;
if ($zip->open("file.zip") === TRUE)
{
  $zip->extractTo("folder/");
  $zip->close();
  echo "Unzipped!";
}
else
{
  echo "Failed...";
}

?>
Andrew
  • 5,839
  • 1
  • 51
  • 72