1
0
Fork 0

Merge pull request #2585 from jaypea/ziparchiver

added ZipArchiver to actually compress zip files
pull/4851/head^2
Jordi Boggiano 2016-02-06 15:49:24 +00:00
commit 7e17fec3b8
3 changed files with 130 additions and 0 deletions

View File

@ -512,6 +512,7 @@ class Factory
}
$am = new Archiver\ArchiveManager($dm);
$am->addArchiver(new Archiver\ZipArchiver);
$am->addArchiver(new Archiver\PharArchiver);
return $am;

View File

@ -0,0 +1,65 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Package\Archiver;
use ZipArchive;
/**
* @author Jan Prieser <jan@prieser.net>
*/
class ZipArchiver implements ArchiverInterface
{
protected static $formats = array(
'zip' => 1
);
/**
* {@inheritdoc}
*/
public function archive($sources, $target, $format, array $excludes = array())
{
$sources = realpath($sources);
$zip = new ZipArchive();
$res = $zip->open($target, ZipArchive::CREATE);
if ($res === true) {
$files = new ArchivableFilesFinder($sources, $excludes);
foreach($files as $file) {
/** @var $file \SplFileInfo */
$filepath = $file->getPath()."/".$file->getFilename();
$localname = str_replace($sources."/", '', $filepath);
$zip->addFile($filepath, $localname);
}
if ($zip->close()) {
return $target;
}
}
$message = sprintf("Could not create archive '%s' from '%s': %s",
$target,
$sources,
$zip->getStatusString()
);
throw new \RuntimeException($message);
}
/**
* {@inheritdoc}
*/
public function supports($format, $sourceType)
{
return isset(static::$formats[$format]) && $this->compressionAvailable();
}
private function compressionAvailable() {
return class_exists('ZipArchive');
}
}

View File

@ -0,0 +1,64 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Test\Package\Archiver;
use Composer\Package\Archiver\ZipArchiver;
class ZipArchiverTest extends ArchiverTest
{
public function testZipArchive()
{
// Set up repository
$this->setupDummyRepo();
$package = $this->setupPackage();
$target = sys_get_temp_dir().'/composer_archiver_test.zip';
// Test archive
$archiver = new ZipArchiver();
$archiver->archive($package->getSourceUrl(), $target, 'zip');
$this->assertFileExists($target);
unlink($target);
}
/**
* Create a local dummy repository to run tests against!
*/
protected function setupDummyRepo()
{
$currentWorkDir = getcwd();
chdir($this->testDir);
$this->writeFile('file.txt', 'content', $currentWorkDir);
$this->writeFile('foo/bar/baz', 'content', $currentWorkDir);
$this->writeFile('foo/bar/ignoreme', 'content', $currentWorkDir);
$this->writeFile('x/baz', 'content', $currentWorkDir);
$this->writeFile('x/includeme', 'content', $currentWorkDir);
chdir($currentWorkDir);
}
protected function writeFile($path, $content, $currentWorkDir)
{
if (!file_exists(dirname($path))) {
mkdir(dirname($path), 0777, true);
}
$result = file_put_contents($path, 'a');
if (false === $result) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not save file.');
}
}
}