Skip to content

Commit c98af84

Browse files
committed
Keep underlying file / output stream open
1 parent a8eb9c9 commit c98af84

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php namespace io\archive\zip;
2+
3+
use io\streams\OutputStream;
4+
5+
/** Needs explicit closing */
6+
class RemainsOpen implements OutputStream {
7+
private $out;
8+
9+
public function __construct(OutputStream $out) { $this->out= $out; }
10+
11+
public function write($bytes) { $this->out->write($bytes); }
12+
13+
public function flush() { $this->out->flush(); }
14+
15+
public function close($force= false) { $force && $this->out->close(); }
16+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ZipArchiveWriter implements Closeable {
3131
* @param bool $unicode whether to use unicode for entry names
3232
*/
3333
public function __construct(OutputStream $stream, $unicode= false) {
34-
$this->stream= $stream;
34+
$this->stream= new RemainsOpen($stream);
3535
$this->unicode= $unicode;
3636
}
3737

@@ -261,7 +261,7 @@ public function close() {
261261
strlen($comment)
262262
));
263263
$this->stream->write($comment);
264-
$this->stream->close();
264+
$this->stream->close(true);
265265
$this->dir= null;
266266
}
267267

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ public function close() {
7676
$this->name
7777
);
7878
$out= $encryption->out($this->writer, $crc32);
79-
while ($this->data->available()) {
80-
$out->write($this->data->read());
81-
}
82-
$out->close();
8379
} else {
8480
$this->writer->addEntry(
8581
0,
@@ -91,12 +87,13 @@ public function close() {
9187
$this->name,
9288
''
9389
);
94-
while ($this->data->available()) {
95-
$this->writer->stream->write($this->data->read());
96-
}
97-
// Leave underlying stream open
90+
$out= $this->writer->stream;
9891
}
9992

93+
while ($this->data->available()) {
94+
$out->write($this->data->read());
95+
}
96+
$out->close();
10097
$this->data= null;
10198
}
10299
}

0 commit comments

Comments
 (0)