ZIP File support

  Status: implemented,  Mon Jan 4 20:23:39 2010
  • Created: 2008-10-20 16:49:44+0200
  • Categories: io
  • Target: 5.7.7
  • Author: friebe

Scope of Change

A new package named io.archive.zip will be created.


Rationale

Reading and creating zip files.

Functionality

The entry point class will be named io.archive.zip.ZipFile. It provides static methods to read and write zip files.

Creating

  $archive= ZipFile::create(new FileOutputStream(new File($filename)));

$out= $archive->addFile(new ZipFileEntry('title.txt', Date::now()));
$out->setCompression(Compression::$GZ);
$out->write('Title goes here');

$archive->close();

Reading

  $archive= ZipFile::open(new FileInputStream(new File($filename)));

// List contents
foreach ($archive->entries() as $entry) {
Console::writeLine
('- ', $entry->getName());
}

// Extract a file and dump its contents to the console
with ($in= $archive->getEntry('title.txt')->inputStream()); {
while ($in->available()) {
Console::write
($in->read());
}
$in->close();
}



Security considerations

None.

Speed impact

None.

Dependencies

  • ext/bz2 for working with BZIP compression
  • ext/zlib for working with GZIP compression


Related documents

http://experiments.xp-forge.net/xml/browse?arena,zip

Comments

friebe, Thu Dec 24 13:45:38 2009

The class name ZipArchive is already taken, see the PHP manual at http://de3.php.net/ZipArchive. We also cannot use the name "Archive" which together with io.archive.zip would form a meaningful name because of our own lang.archive.Archive class. Because ZipArchive is used only as a factory class to create ZipArchiveWriter and ZipArchiveReader instances, we could either:

  • Omit it entirely
  • Fully qualify it (io·zip·Archive)
  • Rename it to Zip, Zips, ZipArchiveFile, ZipBundle, ZipFactory or ZipArchives (or any other name, these are just suggestions)
  • Rename ZipFile to ZipFileEntry (and ZipDir to ZipDirEntry) and then rename ZipArchive to ZipFile.

friebe, Thu Dec 31 17:51:57 2009

Went for the last option above:

  $ svn ci -m '- Rename to work around naming clash:
  >   . ZipFile -> ZipFileEntry
  >   . ZipDir -> ZipDirEntry
  >   . ZipArchive -> ZipFile'
  Deleting       zip/io/zip/ZipArchive.class.php
  Deleting       zip/io/zip/ZipDir.class.php
  Adding         zip/io/zip/ZipDirEntry.class.php
  Replacing      zip/io/zip/ZipFile.class.php
  Adding         zip/io/zip/ZipFileEntry.class.php
  Transmitting file data ..
  Committed revision 11800.

<EOF>


Table of contents