Created
November 11, 2020 14:58
-
-
Save andrewnicols/f36cbed1a789285f76c20fcf3b4471f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace core_files; | |
abstract class archive_writer { | |
public static function get_stream_writer(string $filename, string $type, ?archive_options $options = null): self { | |
$classname = self::get_classname_for_type($type); | |
return $classname::stream_instance($filename, $options); | |
} | |
abstract public static function stream_instance(string $filename, archive_options $options): self; | |
public static function get_file_writer(string $filepath, string $type, ?archive_options $options = null): self { | |
$classname = self::get_classname_for_type($type); | |
return $classname::file_instance($filepath, $options); | |
} | |
abstract public static function file_instance(string $filename, archive_options $options): self; | |
protected static function get_classname_for_type(string $type): string { | |
return "core_files\local\archive_writer\{$type}"; | |
} | |
protected function __construct() {} | |
abstract public function add_file_from_filepath(string $destinationpath, string $sourcepath): void; | |
abstract public function add_file_from_string(string $destinationpath, string $content): void; | |
abstract public function add_file_from_stream(string $destinationpath, $stream): void; | |
public function add_file_from_stored_file(string $destinationpath, stored_file $file) { | |
$filehandle = $file->get_content_file_handle(); | |
$this->add_file_from_stream($destinationpath, $filehandle); | |
} | |
abstract public function finish(): void; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace core_files\local\archive_writer; | |
class zip_writer { | |
private $zipfilehandle; | |
private $zipfilepath; | |
private $tempzipfilepath; | |
private $archive; | |
protected function __construct(\ZipStream\ZipStream $archive, ?archive_options $options = null) { | |
$this->archive = $archive; | |
if ($options) { | |
$this->parse_options($options); | |
} | |
} | |
public static function stream_instance(string $filename, archive_options $options): self { | |
$options = new \ZipStream\Option\Archive(); | |
$options->setSendHttpHeaders(true); | |
$zipwriter = new \ZipStream\ZipStream($filename, $options); | |
return new self($zipwriter, $options); | |
} | |
public static function file_instance(string $filepath, archive_options $options): self { | |
$exportoptions = new \ZipStream\Option\Archive(); | |
$dir = make_request_directory(); | |
$tempfilepath = $dir . "/$filename"; | |
$fh = fopen($tempfilepath, 'w'); | |
$exportoptions->setOutputStream($fh); | |
$exportoptions->setSendHttpHeaders(false); | |
$zipstream = new \ZipStream\ZipStream($filename, $exportoptions); | |
$zipwriter = new static($zipstream, $exportoptions); | |
// ZipStream only takes a file handle resource. | |
// It does not close this resource itself, and it does not know the location of this resource on disk. | |
// Store references to the filehandle, and the location of the filepath in the new class so that the `finish()` | |
// function can close the fh, and move the temporary file into place. | |
// The filehandle must be closed when finishing hte archive. ZipStream does not close it automatically. | |
$zipwriter->zipfilehandle = $fh; | |
$zipwriter->zipfilepath = $tempfilepath; | |
// The fiile is saved to requestdir during construction then moved to the zipfilepath in finish, | |
$zipwriter->targetzipfilepath = $filepath; | |
return new self($zipwriter, $options); | |
} | |
public function add_file_from_filepath(string $destinationpath, string $sourcepath): void { | |
} | |
public function add_file_from_string(string $destinationpath, string $content): void { | |
$this->archive->addFile($destinationpath, $content); | |
} | |
public function add_file_from_stream(string $destinationpath, $stream): void { | |
$this->archive->addFileFromStream($fullfilepathinzip, $filehandle); | |
fclose($filehandle); | |
} | |
public function finish(): void { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment