Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Class used for creating ZIP archives.
19
 *
20
 * @package   core_files
21
 * @copyright 2020 Mark Nelson <mdjnelson@gmail.com>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace core_files\local\archive_writer;
26
 
27
use ZipStream\ZipStream;
28
use core_files\archive_writer;
29
use core_files\local\archive_writer\file_writer_interface as file_writer_interface;
30
use core_files\local\archive_writer\stream_writer_interface as stream_writer_interface;
31
 
32
/**
33
 * Class used for creating ZIP archives.
34
 *
35
 * @package   core_files
36
 * @copyright 2020 Mark Nelson <mdjnelson@gmail.com>
37
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class zip_writer extends archive_writer implements file_writer_interface, stream_writer_interface {
40
 
41
    /**
42
     * @var resource File resource for the file handle for a file-based zip stream
43
     */
44
    private $zipfilehandle = null;
45
 
46
    /**
47
     * @var String The location of the zip file.
48
     */
49
    private $zipfilepath = null;
50
 
51
    /**
52
     * @var ZipStream The zip stream.
53
     */
54
    private $archive;
55
 
56
    /**
57
     * The zip_writer constructor.
58
     *
59
     * @param ZipStream $archive
60
     */
61
    protected function __construct(ZipStream $archive) {
62
        parent::__construct();
63
        $this->archive = $archive;
64
    }
65
 
66
    public static function stream_instance(string $filename): stream_writer_interface {
67
        $zipwriter = new ZipStream(
68
            outputName: $filename,
69
        );
70
 
71
        return new static($zipwriter);
72
    }
73
 
74
    public static function file_instance(string $filename): file_writer_interface {
75
        $dir = make_request_directory();
76
        $filepath = "$dir/$filename";
77
        $fh = fopen($filepath, 'w');
78
 
79
        $zipstream = new ZipStream(
80
            outputName: $filename,
81
            outputStream: $fh,
82
            sendHttpHeaders: false,
83
        );
84
 
85
        $zipwriter = new static($zipstream);
86
        // ZipStream only takes a file handle resource.
87
        // It does not close this resource itself, and it does not know the location of this resource on disk.
88
        // Store references to the filehandle, and the location of the filepath in the new class so that the `finish()`
89
        // function can close the fh, and move the temporary file into place.
90
        // The filehandle must be closed when finishing the archive. ZipStream does not close it automatically.
91
        $zipwriter->zipfilehandle = $fh;
92
        $zipwriter->zipfilepath = $filepath;
93
 
94
        return $zipwriter;
95
    }
96
 
97
    public function add_file_from_filepath(string $name, string $path): void {
98
        $this->archive->addFileFromPath($this->sanitise_filepath($name), $path);
99
    }
100
 
101
    public function add_file_from_string(string $name, string $data): void {
102
        $this->archive->addFile($this->sanitise_filepath($name), $data);
103
    }
104
 
105
    public function add_file_from_stream(string $name, $stream): void {
106
        $this->archive->addFileFromStream($this->sanitise_filepath($name), $stream);
107
        fclose($stream);
108
    }
109
 
110
    public function add_file_from_stored_file(string $name, \stored_file $file): void {
111
        $datetime = new \DateTime();
112
        $datetime->setTimestamp($file->get_timemodified());
113
        $filehandle = $file->get_content_file_handle();
114
        $this->archive->addFileFromStream(
115
            fileName: $this->sanitise_filepath($name),
116
            stream: $filehandle,
117
            lastModificationDateTime: $datetime,
118
        );
119
        fclose($filehandle);
120
    }
121
 
122
    public function finish(): void {
123
        $this->archive->finish();
124
 
125
        if ($this->zipfilehandle) {
126
            fclose($this->zipfilehandle);
127
        }
128
    }
129
 
130
    public function get_path_to_zip(): string {
131
        return $this->zipfilepath;
132
    }
133
}