stechstudio / laravel-zipstream

Easily create Zip files on-the-fly and provide a streaming download
MIT License
413 stars 58 forks source link

Files inside the zip are corrupted #118

Closed damarinov1 closed 1 week ago

damarinov1 commented 3 weeks ago

Laravel 10 PHP 8.2

Hi,

I am updating a project that is using your library since version 2.4. Now, I am updating up to Laravel 10 and updated your library to the latest version (5.0.2).

I am using the library to zip XLSX files that are generated from reports. For some reason, after the update, now, the files inside the zip are not stored correctly and corrupted. The zip size is 632 bytes and the files inside are 71 bytes. Here's how I am using it:

// Method declaration and some code above
                $zip = Zip::create($zipFileName);

                for ($i = 1; $i <= $batch; $i++) {
                    $batchFileName = $i . '-' . $fileName;

                    if ($this->reportsStorage->exists($batchFileName)) {
                        $zip->add($this->reportsStorage->url($batchFileName), $batchFileName);
                    }
                }                

                $path = config('filesystems.disks.' . config('filesystems.disk_names.reports') . '.root');
                $zip->saveTo($path);
// Some code below

I've tried the same exact logic from above, manually in tinker. The zip was perfect. For some reason, during code execution something is happening that I cannot catch.

Any ideas, suggestions what could I be missing? Some config?

Edit: I saw that it doesn't like Spatie's media library. In what sense they are in conflict? Here, in this example these files are not in any way using the media library.

Thanks in advance.

Edit2: I've edited the path to the file. Before I was sending relative url to the files, for example storage/reports/test.xlsx and I've changed it to full url (https://localhost/storage/reports/test.xlsx)

And now when I get to save the zip I've got this error

Typed property STS\ZipStream\Models\File::$filesize must not be accessed before initialization 

What does this error mean? Any help would be appreaciated

lorenzoalulithos commented 1 week ago

I Have The same issue

Typed property STS\ZipStream\Models\File::$filesize must not be accessed before initialization

I update this method in HttpFile

 public function canPredictZipDataSize(): bool
    {
        return (array_key_exists(self::HEADER_CONTENT_LENGTH, $this->getHeaders()));
    }

It's a bug?

lorenzoalulithos commented 1 week ago

I believe that in php 8, variables that are typed must have a default value.

If I add a default value to $filesize everything works.

Alternatively we can set $filesize to null.

jszobody commented 1 week ago

@damarinov1 @lorenzoalulithos I just pushed a fix and tagged v5.1.