Home

Awesome

ZipStreamWriter for PHP

A fast, efficient streaming library for creating ZIP files on the fly in pure userland PHP without any external tools, PHP extensions, or physical disk storage requirements. Follows version 6.3.7 of the PKWARE Zip specification. Choose from a MIT or LGPL license.

Donate Discord

If you use this project, don't forget to donate to support its development!

Features

Getting Started

Download/clone this repo, put the 'support' directory on a server, and then do something like this from PHP:

<?php
	require_once "support/zip_stream_writer.php";

	// Adjust various PHP settings and output relevant HTTP headers to trigger a download.
	ZipStreamWriter::StartHTTPResponse("test.zip");

	$zip = new ZipStreamWriter();
	$zip->Init();

	$lastmodified  = mktime(9, 41, 20, 12, 10, 2018);
	$lastaccessed = $lastmodified + 60;
	$created = $lastmodified - 24 * 60 * 60;

	// Register some empty directories.
	$zip->AddDirectory("dir/", array("last_modified" => $lastmodified));
	$zip->AddDirectory("dir/dir2/", array("last_modified" => $lastmodified));


	// Add smaller files with a simple call.
	$zip->AddFileFromString("dir/ Hello.txt", "Hello there!", array("last_modified" => $lastmodified));

	// Keep RAM usage low by streaming data.  Just call Read() whenever to clear out the internal buffer.
	echo $zip->Read();


	// Completely customize options per file.
	// Some combinations can break some ZIP readers though (see documentation).
	$options = array(
		"last_modified" => $lastmodified,
		"64bit" => true,
		"unix_attrs" => 0644,
		"extra_fields" => array(),
		"comment" => "Best file ever."
	);

	// NTFS timestamps.
	ZipStreamWriter::AppendNTFSExtraField($options["extra_fields"], $lastmodified, $lastaccessed, $created);

	// UNIX timestamps + uid/gid.
	$uid = 1234;
	$gid = 1234;

	ZipStreamWriter::AppendUNIXExtraField($options["extra_fields"], $lastaccessed, $lastmodified, $uid, $gid);


	// Generate and compress larger files in chunks.
	$zip->OpenFile("dir/Hello2.txt", $options);

	$zip->AppendFileData("Hello ");
	if ($zip->BytesAvailable() > 65536)  echo $zip->Read();

	$zip->AppendFileData("there!");
	echo $zip->Read();

	$zip->CloseFile();
	echo $zip->Read();

	// Finalize and close the ZIP file.  Adding a comment is optional.
	$zip->Finalize("Woot!");

	// The last Read() call is required.  The rest are optional.
	echo $zip->Read();

See the ZipStreamWriter::InitCentralDirHeader() documentation for a complete list of options.

Documentation

Limitations

The following limitations are ordered from most important to least: