added ZipArchiver to actually compress zip files
parent
69e77fbbb5
commit
d57e2b9ffb
|
@ -386,6 +386,7 @@ class Factory
|
||||||
}
|
}
|
||||||
|
|
||||||
$am = new Archiver\ArchiveManager($dm);
|
$am = new Archiver\ArchiveManager($dm);
|
||||||
|
$am->addArchiver(new Archiver\ZipArchiver);
|
||||||
$am->addArchiver(new Archiver\PharArchiver);
|
$am->addArchiver(new Archiver\PharArchiver);
|
||||||
|
|
||||||
return $am;
|
return $am;
|
||||||
|
|
|
@ -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');
|
||||||
|
}
|
||||||
|
}
|
|
@ -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.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue