Skip to content

Commit

Permalink
Accept file names, io.Channel instances as well as streams
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Oct 21, 2021
1 parent 9cfc431 commit 77ffe13
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ ZIP File support for the XP Framework ChangeLog

## 10.0.0 / 2021-10-21

* Made `ZipFile::create()` and `ZipFile::open()` accept file names,
`io.Channel` instances as well as in- and output streams
(@thekid)
* Implemented xp-framework/rfc#341, dropping compatibility with XP 9
(@thekid)

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Usage (creating a zip file)
use io\archive\zip\{ZipFile, ZipDirEntry, ZipFileEntry};
use io\File;

$z= ZipFile::create((new File('dist.zip'))->out());
$z= ZipFile::create(new File('dist.zip'));

// Add a directory
$dir= $z->add(new ZipDirEntry('META-INF'));
Expand All @@ -36,7 +36,7 @@ use io\archive\zip\ZipFile;
use io\streams\Streams;
use io\File;

$z= ZipFile::open((new File('dist.zip'))->in());
$z= ZipFile::open(new File('dist.zip'));
foreach ($z->entries() as $entry) {
if ($entry->isDirectory()) {
// Create dir
Expand Down
27 changes: 20 additions & 7 deletions src/main/php/io/archive/zip/ZipFile.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace io\archive\zip;

use io\streams\{InputStream, OutputStream};
use io\Channel;
use io\streams\{InputStream, FileInputStream, OutputStream, FileOutputStream};

/**
* Zip archives hanadling
Expand Down Expand Up @@ -47,20 +48,32 @@ abstract class ZipFile {
/**
* Creation constructor
*
* @param io.streams.OutputStream stream
* @param string|io.Channel|io.streams.OutputStream $arg
* @return io.archive.zip.ZipArchiveWriter
*/
public static function create(OutputStream $stream) {
return new ZipArchiveWriter($stream);
public static function create($arg) {
if ($arg instanceof Channel) {
return new ZipArchiveWriter($arg->out());
} else if ($arg instanceof OutputStream) {
return new ZipArchiveWriter($arg);
} else {
return new ZipArchiveWriter(new FileOutputStream($arg));
}
}

/**
* Read constructor
*
* @param io.streams.InputStream stream
* @param string|io.Channel|io.streams.InputStream $arg
* @return io.archive.zip.ZipArchiveReader
*/
public static function open(InputStream $stream) {
return new ZipArchiveReader($stream);
public static function open($arg) {
if ($arg instanceof Channel) {
return new ZipArchiveReader($arg->in());
} else if ($arg instanceof InputStream) {
return new ZipArchiveReader($arg);
} else {
return new ZipArchiveReader(new FileInputStream($arg));
}
}
}

0 comments on commit 77ffe13

Please sign in to comment.