Skip to content

Commit d8d639d

Browse files
Merge pull request #46 from barracudanetworks/feature/write-to-output-stream
Pass in an output stream to allow output to files.
2 parents 1bf9809 + 86a1287 commit d8d639d

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ A library for dynamically streaming dynamic tar or zip files without the need to
99
/**
1010
* Construct Parameters:
1111
*
12-
* $name - Name of output file (optional).
13-
* $opt - Hash of archive options (optional, see "Archive Options"
14-
* below).
12+
* $name - Name of output file (optional).
13+
* $opt - Hash of archive options (optional, see "Archive Options"
14+
* below).
15+
* $output_stream - Output stream for archive (optional - defaults to STDOUT)
1516
*
1617
* Archive Options:
1718
*
@@ -141,6 +142,7 @@ disable) the HTTP headers. See the class file for details.
141142
- Andrew Borek
142143
- Rafael Corral
143144
- John Maguire
145+
- Zachery Stuart
144146

145147
## License
146148

src/Archive.php

+23-10
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,20 @@ class Archive
5050
/**
5151
* Create a new ArchiveStream object.
5252
*
53-
* @param string $name The name of the resulting archive (optional).
54-
* @param array $opt Hash of archive options (see archive options in readme).
55-
* @param string $base_path An optional base path for files to be named under.
53+
* @param string $name The name of the resulting archive (optional).
54+
* @param array $opt Hash of archive options (see archive options in readme).
55+
* @param string $base_path An optional base path for files to be named under.
56+
* @param resource $output_stream Output stream for archive contents.
5657
*/
57-
public function __construct($name = null, array $opt = array(), $base_path = null)
58+
public function __construct(
59+
$name = null,
60+
array $opt = array(),
61+
$base_path = null,
62+
$output_stream = STDOUT
63+
)
5864
{
65+
$this->output_stream = $output_stream;
66+
5967
// save options
6068
$this->opt = $opt;
6169

@@ -90,25 +98,30 @@ public function __construct($name = null, array $opt = array(), $base_path = nul
9098
/**
9199
* Create instance based on useragent string
92100
*
93-
* @param string $base_filename A name for the resulting archive (without an extension).
94-
* @param array $opt Map of archive options (see above for list).
101+
* @param string $base_filename A name for the resulting archive (without an extension).
102+
* @param array $opt Map of archive options (see above for list).
103+
* @param resource $output_stream Output stream for archive contents.
95104
* @return ArchiveStream for either zip or tar
96105
*/
97-
public static function instance_by_useragent($base_filename = null, array $opt = array())
106+
public static function instance_by_useragent(
107+
$base_filename = null,
108+
array $opt = array(),
109+
$output_stream = STDOUT
110+
)
98111
{
99112
$user_agent = (isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : '');
100113

101114
// detect windows and use zip
102115
if (strpos($user_agent, 'windows') !== false)
103116
{
104117
$filename = (($base_filename === null) ? null : $base_filename . '.zip');
105-
return new Zip($filename, $opt, $base_filename);
118+
return new Zip($filename, $opt, $base_filename, $output_stream);
106119
}
107120
// fallback to tar
108121
else
109122
{
110123
$filename = (($base_filename === null) ? null : $base_filename . '.tar');
111-
return new Tar($filename, $opt, $base_filename);
124+
return new Tar($filename, $opt, $base_filename, $output_stream);
112125
}
113126
}
114127

@@ -327,7 +340,7 @@ protected function send($data)
327340

328341
$this->need_headers = false;
329342

330-
echo $data;
343+
fwrite($this->output_stream, $data);
331344
}
332345

333346
/**

0 commit comments

Comments
 (0)