Skip to content

Commit 77ffe13

Browse files
committed
Accept file names, io.Channel instances as well as streams
1 parent 9cfc431 commit 77ffe13

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ ZIP File support for the XP Framework ChangeLog
55

66
## 10.0.0 / 2021-10-21
77

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

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Usage (creating a zip file)
1515
use io\archive\zip\{ZipFile, ZipDirEntry, ZipFileEntry};
1616
use io\File;
1717

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

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

39-
$z= ZipFile::open((new File('dist.zip'))->in());
39+
$z= ZipFile::open(new File('dist.zip'));
4040
foreach ($z->entries() as $entry) {
4141
if ($entry->isDirectory()) {
4242
// Create dir

src/main/php/io/archive/zip/ZipFile.class.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace io\archive\zip;
22

3-
use io\streams\{InputStream, OutputStream};
3+
use io\Channel;
4+
use io\streams\{InputStream, FileInputStream, OutputStream, FileOutputStream};
45

56
/**
67
* Zip archives hanadling
@@ -47,20 +48,32 @@ abstract class ZipFile {
4748
/**
4849
* Creation constructor
4950
*
50-
* @param io.streams.OutputStream stream
51+
* @param string|io.Channel|io.streams.OutputStream $arg
5152
* @return io.archive.zip.ZipArchiveWriter
5253
*/
53-
public static function create(OutputStream $stream) {
54-
return new ZipArchiveWriter($stream);
54+
public static function create($arg) {
55+
if ($arg instanceof Channel) {
56+
return new ZipArchiveWriter($arg->out());
57+
} else if ($arg instanceof OutputStream) {
58+
return new ZipArchiveWriter($arg);
59+
} else {
60+
return new ZipArchiveWriter(new FileOutputStream($arg));
61+
}
5562
}
5663

5764
/**
5865
* Read constructor
5966
*
60-
* @param io.streams.InputStream stream
67+
* @param string|io.Channel|io.streams.InputStream $arg
6168
* @return io.archive.zip.ZipArchiveReader
6269
*/
63-
public static function open(InputStream $stream) {
64-
return new ZipArchiveReader($stream);
70+
public static function open($arg) {
71+
if ($arg instanceof Channel) {
72+
return new ZipArchiveReader($arg->in());
73+
} else if ($arg instanceof InputStream) {
74+
return new ZipArchiveReader($arg);
75+
} else {
76+
return new ZipArchiveReader(new FileInputStream($arg));
77+
}
6578
}
6679
}

0 commit comments

Comments
 (0)