diff options
author | Paul Duncan <pabs@pablotron.org> | 2018-09-02 13:16:16 -0400 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2018-09-03 09:54:38 -0400 |
commit | b520b6459d7b5a3dfb31c1b3febf02887bb07505 (patch) | |
tree | c16e7f3c264280f90de2120b7f6ea7d5a914256d /examples | |
parent | ca87bd7c79c17af6cdf804bc24f58fb20c41ad76 (diff) | |
download | zipstream-php-b520b6459d7b5a3dfb31c1b3febf02887bb07505.tar.bz2 zipstream-php-b520b6459d7b5a3dfb31c1b3febf02887bb07505.zip |
add examples
Diffstat (limited to 'examples')
-rw-r--r-- | examples/01-simple.php | 16 | ||||
-rw-r--r-- | examples/02-add_file_from_path.php | 18 | ||||
-rw-r--r-- | examples/03-add_stream.php | 24 | ||||
-rw-r--r-- | examples/04-add.php | 44 | ||||
-rw-r--r-- | examples/05-send.php | 14 |
5 files changed, 116 insertions, 0 deletions
diff --git a/examples/01-simple.php b/examples/01-simple.php new file mode 100644 index 0000000..a01e0f8 --- /dev/null +++ b/examples/01-simple.php @@ -0,0 +1,16 @@ +<?php + +require_once __DIR__ . '/../src/ZipStream.php'; + +# import zipstream class +use Pablotron\ZipStream\ZipStream; + +# create the output stream +$zip = new ZipStream('example.zip'); + +# add a file named "hello.txt" to output archive, containing +# the string "hello world!" +$zip->add_file('hello.txt', 'hello world!'); + +# finalize the output stream +$zip->close(); diff --git a/examples/02-add_file_from_path.php b/examples/02-add_file_from_path.php new file mode 100644 index 0000000..3b73754 --- /dev/null +++ b/examples/02-add_file_from_path.php @@ -0,0 +1,18 @@ +<?php + +require_once __DIR__ . '/../src/ZipStream.php'; + +# import zipstream class +use Pablotron\ZipStream\ZipStream; + +# set source path of local file +$src_path = __DIR__ . '/02-add_file_from_path.php'; + +# create the output archive named "example.zip" +$zip = new ZipStream('example.zip'); + +# read from source path and add it to the archive as "example.php" +$zip->add_file_from_path('example.php', $src_path); + +# finalize the output stream +$zip->close(); diff --git a/examples/03-add_stream.php b/examples/03-add_stream.php new file mode 100644 index 0000000..68895b3 --- /dev/null +++ b/examples/03-add_stream.php @@ -0,0 +1,24 @@ +<?php + +require_once __DIR__ . '/../src/ZipStream.php'; + +# import zipstream class +use Pablotron\ZipStream\ZipStream; + +# set source path +$src_path = __DIR__ . '/03-add_stream.php'; + +# create the output archive named "example.zip" +$zip = new ZipStream('example.zip'); + +# open stream +$src = fopen($src_path, 'rb'); + +# read from source stream and add it to the archive as "example.php" +$zip->add_stream('example.php', $src); + +# close input stream +fclose($src); + +# finalize the output stream +$zip->close(); diff --git a/examples/04-add.php b/examples/04-add.php new file mode 100644 index 0000000..3843086 --- /dev/null +++ b/examples/04-add.php @@ -0,0 +1,44 @@ +<?php + +require_once __DIR__ . '/../src/ZipStream.php'; + +# import zipstream class +use Pablotron\ZipStream\ZipStream; + +# set source path of local file +$files = [ + 'summary-01.txt' => '01-simple.php', + 'summary-04.txt' => '04-add.php', +]; + +# create the output archive named "example.zip" +$zip = new ZipStream('example.zip'); + +foreach ($files as $dst_path => $src_path) { + $zip->add($dst_path, function(&$e) use ($src_path) { + # build absolute path to source file + $abs_src_path = __DIR__ . "/$src_path"; + + # read file contents and get md5 hash + $data = file_get_contents($abs_src_path); + + # build arguments + $args = [ + 'source' => $src_path, + 'md5' => md5($data), + 'sha1' => sha1($data), + ]; + + # write lines to output archive + foreach ($args as $key => $val) { + # build line + $line = "$key: $val\n"; + + # write line to output file + $e->write($line); + } + }); +} + +# finalize the output stream +$zip->close(); diff --git a/examples/05-send.php b/examples/05-send.php new file mode 100644 index 0000000..93b4b27 --- /dev/null +++ b/examples/05-send.php @@ -0,0 +1,14 @@ +<?php +declare(strict_types = 1); + +require_once __DIR__ . '/../src/ZipStream.php'; + +# import zipstream class +use Pablotron\ZipStream\ZipStream; + +# create archive named "example.zip" +ZipStream::send('example.zip', function(ZipStream &$zip) { + # add a file named "hello.txt" to output archive, containing + # the string "hello world!" + $zip->add_file('hello.txt', 'hello world!'); +}); |