Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 57... Línea 57...
57
 * // list of local files
57
 * // list of local files
58
 * $files = array('foo.txt', 'bar.jpg');
58
 * $files = array('foo.txt', 'bar.jpg');
59
 *
59
 *
60
 * // read and add each file to the archive
60
 * // read and add each file to the archive
61
 * foreach ($files as $path)
61
 * foreach ($files as $path)
62
 *   $zip->addFileFormPath(fileName: $path, $path);
62
 *   $zip->addFileFromPath(fileName: $path, $path);
63
 *
63
 *
64
 * // write archive footer to stream
64
 * // write archive footer to stream
65
 * $zip->finish();
65
 * $zip->finish();
66
 * ```
66
 * ```
67
 */
67
 */
Línea 261... Línea 261...
261
        ?int $exactSize = null,
261
        ?int $exactSize = null,
262
        ?bool $enableZeroHeader = null,
262
        ?bool $enableZeroHeader = null,
263
    ): void {
263
    ): void {
264
        $this->addFileFromCallback(
264
        $this->addFileFromCallback(
265
            fileName: $fileName,
265
            fileName: $fileName,
266
            callback: fn () => $data,
266
            callback: fn() => $data,
267
            comment: $comment,
267
            comment: $comment,
268
            compressionMethod: $compressionMethod,
268
            compressionMethod: $compressionMethod,
269
            deflateLevel: $deflateLevel,
269
            deflateLevel: $deflateLevel,
270
            lastModificationDateTime: $lastModificationDateTime,
270
            lastModificationDateTime: $lastModificationDateTime,
271
            maxSize: $maxSize,
271
            maxSize: $maxSize,
Línea 291... Línea 291...
291
     * );
291
     * );
292
     *
292
     *
293
     * // add a file named 'bigfile.rar' from the local file
293
     * // add a file named 'bigfile.rar' from the local file
294
     * // '/usr/share/bigfile.rar' with a comment and a last-modified
294
     * // '/usr/share/bigfile.rar' with a comment and a last-modified
295
     * // time of two hours ago
295
     * // time of two hours ago
296
     * $zip->addFile(
296
     * $zip->addFileFromPath(
297
     *   fileName: 'bigfile.rar',
297
     *   fileName: 'bigfile.rar',
298
     *   path: '/usr/share/bigfile.rar',
298
     *   path: '/usr/share/bigfile.rar',
299
     *   comment: 'this is a comment about bigfile.rar',
299
     *   comment: 'this is a comment about bigfile.rar',
300
     *   lastModificationDateTime: new DateTime('2 hours ago'),
300
     *   lastModificationDateTime: new DateTime('2 hours ago'),
301
     * );
301
     * );
Línea 328... Línea 328...
328
                throw new FileNotFoundException($path);
328
                throw new FileNotFoundException($path);
329
            }
329
            }
330
            throw new FileNotReadableException($path);
330
            throw new FileNotReadableException($path);
331
        }
331
        }
Línea 332... Línea 332...
332
 
332
 
-
 
333
        $fileTime = filemtime($path);
333
        if ($fileTime = filemtime($path)) {
334
        if ($fileTime !== false) {
334
            $lastModificationDateTime ??= (new DateTimeImmutable())->setTimestamp($fileTime);
335
            $lastModificationDateTime ??= (new DateTimeImmutable())->setTimestamp($fileTime);
Línea 335... Línea 336...
335
        }
336
        }
336
 
337
 
Línea 392... Línea 393...
392
        ?int $exactSize = null,
393
        ?int $exactSize = null,
393
        ?bool $enableZeroHeader = null,
394
        ?bool $enableZeroHeader = null,
394
    ): void {
395
    ): void {
395
        $this->addFileFromCallback(
396
        $this->addFileFromCallback(
396
            fileName: $fileName,
397
            fileName: $fileName,
397
            callback: fn () => $stream,
398
            callback: fn() => $stream,
398
            comment: $comment,
399
            comment: $comment,
399
            compressionMethod: $compressionMethod,
400
            compressionMethod: $compressionMethod,
400
            deflateLevel: $deflateLevel,
401
            deflateLevel: $deflateLevel,
401
            lastModificationDateTime: $lastModificationDateTime,
402
            lastModificationDateTime: $lastModificationDateTime,
402
            maxSize: $maxSize,
403
            maxSize: $maxSize,
Línea 471... Línea 472...
471
        ?int $exactSize = null,
472
        ?int $exactSize = null,
472
        ?bool $enableZeroHeader = null,
473
        ?bool $enableZeroHeader = null,
473
    ): void {
474
    ): void {
474
        $this->addFileFromCallback(
475
        $this->addFileFromCallback(
475
            fileName: $fileName,
476
            fileName: $fileName,
476
            callback: fn () => $stream,
477
            callback: fn() => $stream,
477
            comment: $comment,
478
            comment: $comment,
478
            compressionMethod: $compressionMethod,
479
            compressionMethod: $compressionMethod,
479
            deflateLevel: $deflateLevel,
480
            deflateLevel: $deflateLevel,
480
            lastModificationDateTime: $lastModificationDateTime,
481
            lastModificationDateTime: $lastModificationDateTime,
481
            maxSize: $maxSize,
482
            maxSize: $maxSize,
Línea 492... Línea 493...
492
     *
493
     *
493
     * ##### Examples
494
     * ##### Examples
494
     *
495
     *
495
     * ```php
496
     * ```php
496
     * foreach($files as $name => $size) {
497
     * foreach($files as $name => $size) {
497
     *   $archive->addFileFromPsr7Stream(
498
     *   $archive->addFileFromCallback(
498
     *     fileName: 'streamfile.txt',
499
     *     fileName: 'streamfile.txt',
499
     *     exactSize: $size,
500
     *     exactSize: $size,
500
     *     callback: function() use($name): Psr\Http\Message\StreamInterface {
501
     *     callback: function() use($name): Psr\Http\Message\StreamInterface {
501
     *       $response = download($name);
502
     *       $response = download($name);
502
     *       return $response->getBody();
503
     *       return $response->getBody();
Línea 561... Línea 562...
561
    ): void {
562
    ): void {
562
        $file = new File(
563
        $file = new File(
563
            dataCallback: function () use ($callback, $maxSize) {
564
            dataCallback: function () use ($callback, $maxSize) {
564
                $data = $callback();
565
                $data = $callback();
Línea 565... Línea 566...
565
 
566
 
566
                if(is_resource($data)) {
567
                if (is_resource($data)) {
567
                    return $data;
568
                    return $data;
Línea 568... Línea 569...
568
                }
569
                }
569
 
570
 
570
                if($data instanceof StreamInterface) {
571
                if ($data instanceof StreamInterface) {
Línea 571... Línea 572...
571
                    return StreamWrapper::getResource($data);
572
                    return StreamWrapper::getResource($data);
Línea 609... Línea 610...
609
            exactSize: $exactSize,
610
            exactSize: $exactSize,
610
            enableZip64: $this->enableZip64,
611
            enableZip64: $this->enableZip64,
611
            enableZeroHeader: $enableZeroHeader ?? $this->defaultEnableZeroHeader,
612
            enableZeroHeader: $enableZeroHeader ?? $this->defaultEnableZeroHeader,
612
        );
613
        );
Línea 613... Línea 614...
613
 
614
 
614
        if($this->operationMode !== OperationMode::NORMAL) {
615
        if ($this->operationMode !== OperationMode::NORMAL) {
615
            $this->recordedSimulation[] = $file;
616
            $this->recordedSimulation[] = $file;
Línea 616... Línea 617...
616
        }
617
        }
617
 
618
 
Línea 627... Línea 628...
627
     *
628
     *
628
     * ##### Examples
629
     * ##### Examples
629
     *
630
     *
630
     * ```php
631
     * ```php
631
     * // add a directory named 'world/'
632
     * // add a directory named 'world/'
632
     * $zip->addFile(fileName: 'world/');
633
     * $zip->addDirectory(fileName: 'world/');
633
     * ```
634
     * ```
634
     */
635
     */
635
    public function addDirectory(
636
    public function addDirectory(
636
        string $fileName,
637
        string $fileName,
637
        string $comment = '',
638
        string $comment = '',
Línea 674... Línea 675...
674
     * $zip->executeSimulation();
675
     * $zip->executeSimulation();
675
     * ```
676
     * ```
676
     */
677
     */
677
    public function executeSimulation(): void
678
    public function executeSimulation(): void
678
    {
679
    {
679
        if($this->operationMode !== OperationMode::NORMAL) {
680
        if ($this->operationMode !== OperationMode::NORMAL) {
680
            throw new RuntimeException('Zip simulation is not finished.');
681
            throw new RuntimeException('Zip simulation is not finished.');
681
        }
682
        }
Línea 682... Línea 683...
682
 
683
 
683
        foreach($this->recordedSimulation as $file) {
684
        foreach ($this->recordedSimulation as $file) {
684
            $this->centralDirectoryRecords[] = $file->cloneSimulationExecution()->process();
685
            $this->centralDirectoryRecords[] = $file->cloneSimulationExecution()->process();
Línea 685... Línea 686...
685
        }
686
        }
686
 
687
 
Línea 767... Línea 768...
767
            return StreamWrapper::getResource($outputStream);
768
            return StreamWrapper::getResource($outputStream);
768
        }
769
        }
769
        if (is_resource($outputStream)) {
770
        if (is_resource($outputStream)) {
770
            return $outputStream;
771
            return $outputStream;
771
        }
772
        }
772
        return fopen('php://output', 'wb');
773
        $resource = fopen('php://output', 'wb');
-
 
774
 
-
 
775
        if ($resource === false) {
-
 
776
            throw new RuntimeException('fopen of php://output failed');
-
 
777
        }
-
 
778
 
-
 
779
        return $resource;
773
    }
780
    }
Línea 774... Línea 781...
774
 
781
 
775
    /**
782
    /**
776
     * Record sent bytes
783
     * Record sent bytes
Línea 813... Línea 820...
813
                flush();
820
                flush();
814
            }
821
            }
815
        }
822
        }
816
    }
823
    }
Línea 817... Línea 824...
817
 
824
 
818
     /**
825
    /**
819
     * Send HTTP headers for this stream.
826
    * Send HTTP headers for this stream.
820
     */
827
    */
821
    private function sendHttpHeaders(): void
828
    private function sendHttpHeaders(): void
822
    {
829
    {
823
        // grab content disposition
830
        // grab content disposition
Línea 824... Línea 831...
824
        $disposition = $this->contentDisposition;
831
        $disposition = $this->contentDisposition;
825
 
832
 
826
        if ($this->outputName) {
833
        if ($this->outputName !== null) {
Línea 827... Línea 834...
827
            // Various different browsers dislike various characters here. Strip them all for safety.
834
            // Various different browsers dislike various characters here. Strip them all for safety.
828
            $safeOutput = trim(str_replace(['"', "'", '\\', ';', "\n", "\r"], '', $this->outputName));
835
            $safeOutput = trim(str_replace(['"', "'", '\\', ';', "\n", "\r"], '', $this->outputName));
Línea 852... Línea 859...
852
    private function clear(): void
859
    private function clear(): void
853
    {
860
    {
854
        $this->centralDirectoryRecords = [];
861
        $this->centralDirectoryRecords = [];
855
        $this->offset = 0;
862
        $this->offset = 0;
Línea 856... Línea 863...
856
 
863
 
857
        if($this->operationMode === OperationMode::NORMAL) {
864
        if ($this->operationMode === OperationMode::NORMAL) {
858
            $this->ready = false;
865
            $this->ready = false;
859
            $this->recordedSimulation = [];
866
            $this->recordedSimulation = [];
860
        } else {
867
        } else {
861
            $this->operationMode = OperationMode::NORMAL;
868
            $this->operationMode = OperationMode::NORMAL;