0

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>';
Scott Chu
  • 972
  • 14
  • 26
  • I only call the extractTo() of ZipArchive. I'll put on the codes tomorrow if you think it's relevant. But I think the problem comes from the backslash. – Scott Chu Jun 23 '20 at 14:26
  • I find the similar problem. May try its answer: https://stackoverflow.com/questions/32698752/linux-php-extractto-returns-whole-path-instead-of-the-file-structure – Scott Chu Jun 24 '20 at 02:28
  • It makes sense if your source files are on windows server and you are running the script to extrac files on linux client. Is this your situation? You can check by var_dump($old_zipfile_path) and var_dump($old_unzip_dir) : if those paths includes backslashes it is the point. – GabrieleMartini Jun 25 '20 at 07:24

0 Answers0