mirror of
https://github.com/Ne-Lexa/php-zip.git
synced 2025-08-15 03:34:49 +02:00
Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
d8e40ee3f1 | ||
|
58e9f4bf73 | ||
|
39f8616336 | ||
|
8d140cc1a1 | ||
|
11a7c16f1b | ||
|
676eca7f87 |
5
CHANGELOG.md
Normal file
5
CHANGELOG.md
Normal file
@@ -0,0 +1,5 @@
|
||||
## 2.2.0 (2017-03-02)
|
||||
|
||||
Features:
|
||||
- create output object `ZipOutputFile` from `ZipFile` in method `ZipFile::edit()`.
|
||||
- create output object `ZipOutputFile` from filename in static method `ZipOutputFile::openFromFile(string $filename)`.
|
24
README.md
24
README.md
@@ -149,6 +149,10 @@ Get entry content.
|
||||
```php
|
||||
$data = $zipFile->getEntryContent($entryName);
|
||||
```
|
||||
Edit zip archive
|
||||
```php
|
||||
$zipOutputFile = $zipFile->edit();
|
||||
```
|
||||
Close zip archive.
|
||||
```php
|
||||
$zipFile->close();
|
||||
@@ -162,6 +166,11 @@ $zipOutputFile = \PhpZip\ZipOutputFile::create();
|
||||
```
|
||||
Open zip file from update.
|
||||
```php
|
||||
$filename = "file.zip";
|
||||
$zipOutputFile = \PhpZip\ZipOutputFile::openFromFile($filename);
|
||||
```
|
||||
or
|
||||
```php
|
||||
// initial ZipFile
|
||||
$zipFile = \PhpZip\ZipFile::openFromFile($filename);
|
||||
|
||||
@@ -169,23 +178,28 @@ $zipFile = \PhpZip\ZipFile::openFromFile($filename);
|
||||
$zipOutputFile = new \PhpZip\ZipOutputFile($zipFile);
|
||||
// or
|
||||
$zipOutputFile = \PhpZip\ZipOutputFile::openFromZipFile($zipFile);
|
||||
// or
|
||||
$zipOutputFile = $zipFile->edit();
|
||||
```
|
||||
Add entry from file.
|
||||
```php
|
||||
$zipOutputFile->addFromFile($filename); // $entryName == basename($filename);
|
||||
$zipOutputFile->addFromFile($filename, $entryName);
|
||||
$zipOutputFile->addFromFile($filename, $entryName, ZipEntry::METHOD_DEFLATED);
|
||||
$zipOutputFile->addFromFile($filename, $entryName, ZipEntry::METHOD_STORED); // no compress
|
||||
$zipOutputFile->addFromFile($filename, null, ZipEntry::METHOD_BZIP2); // $entryName == basename($filename);
|
||||
```
|
||||
Add entry from string data.
|
||||
```php
|
||||
$zipOutputFile->addFromString($entryName, $data)
|
||||
$zipOutputFile->addFromString($entryName, $data, ZipEntry::METHOD_DEFLATED)
|
||||
$zipOutputFile->addFromString($entryName, $data);
|
||||
$zipOutputFile->addFromString($entryName, $data, ZipEntry::METHOD_DEFLATED);
|
||||
$zipOutputFile->addFromString($entryName, $data, ZipEntry::METHOD_STORED); // no compress
|
||||
```
|
||||
Add entry from stream.
|
||||
```php
|
||||
$zipOutputFile->addFromStream($stream, $entryName)
|
||||
$zipOutputFile->addFromStream($stream, $entryName, ZipEntry::METHOD_DEFLATED)
|
||||
$zipOutputFile->addFromStream($stream, $entryName);
|
||||
$zipOutputFile->addFromStream($stream, $entryName, ZipEntry::METHOD_DEFLATED);
|
||||
$zipOutputFile->addFromStream($stream, $entryName, ZipEntry::METHOD_STORED); // no compress
|
||||
```
|
||||
Add empty dir
|
||||
```php
|
||||
@@ -446,7 +460,7 @@ $zipOutputFile->close(); // close output file, release all streams
|
||||
$zipFile = \PhpZip\ZipFile::openFromFile($outputFilename); // open zip archive from file
|
||||
$zipFile->extractTo($outputDirExtract); // extract files to dir
|
||||
|
||||
$zipOutputFile = \PhpZip\ZipOutputFile::openFromZipFile($zipFile); // create zip output archive for update
|
||||
$zipOutputFile = $zipFile->edit(); // create zip output archive for update
|
||||
$zipOutputFile->deleteFromRegex('~^\.~'); // delete all hidden (Unix) files
|
||||
$zipOutputFile->addFromString('dir/file.txt', 'Test file'); // add files from string contents
|
||||
$zipOutputFile->saveAsFile($outputFilename); // update zip file
|
||||
|
@@ -105,9 +105,26 @@ class TraditionalPkwareEncryptionEngine
|
||||
private function updateKeys($charAt)
|
||||
{
|
||||
$this->keys[0] = self::crc32($this->keys[0], $charAt);
|
||||
$this->keys[1] = ($this->keys[1] + ($this->keys[0] & 0xff)) & 4294967295;
|
||||
$this->keys[1] = ($this->keys[1] * 134775813 + 1) & 4294967295;
|
||||
$this->keys[2] = self::crc32($this->keys[2], ($this->keys[1] >> 24) & 0xff);
|
||||
$this->keys[1] = $this->keys[1] + ($this->keys[0] & 0xff);
|
||||
$this->keys[1] = self::toInt($this->keys[1] * 134775813 + 1);
|
||||
$this->keys[2] = self::toInt(self::crc32($this->keys[2], ($this->keys[1] >> 24) & 0xff));
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast to int
|
||||
*
|
||||
* @param $i
|
||||
* @return int
|
||||
*/
|
||||
private static function toInt($i)
|
||||
{
|
||||
$i = (int)($i & 0xffffffff);
|
||||
if ($i > 2147483647) {
|
||||
return -(-$i & 0xffffffff);
|
||||
} elseif ($i < -2147483648) {
|
||||
return $i & -2147483648;
|
||||
}
|
||||
return $i;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -88,16 +88,20 @@ class WinZipAesEngine
|
||||
throw new ZipCryptoException("Expected end of file after WinZip AES authentication code!");
|
||||
}
|
||||
|
||||
do {
|
||||
assert($this->entry->getPassword() !== null);
|
||||
assert(self::AES_BLOCK_SIZE_BITS <= $keyStrengthBits);
|
||||
$password = $this->entry->getPassword();
|
||||
assert($password !== null);
|
||||
assert(self::AES_BLOCK_SIZE_BITS <= $keyStrengthBits);
|
||||
|
||||
// WinZip 99-character limit
|
||||
// @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/
|
||||
$password = substr($password, 0, 99);
|
||||
do {
|
||||
// Here comes the strange part about WinZip AES encryption:
|
||||
// Its unorthodox use of the Password-Based Key Derivation
|
||||
// Function 2 (PBKDF2) of PKCS #5 V2.0 alias RFC 2898.
|
||||
// Yes, the password verifier is only a 16 bit value.
|
||||
// So we must use the MAC for password verification, too.
|
||||
$keyParam = hash_pbkdf2("sha1", $this->entry->getPassword(), $salt, self::ITERATION_COUNT, (2 * $keyStrengthBits + self::PWD_VERIFIER_BITS) / 8, true);
|
||||
$keyParam = hash_pbkdf2("sha1", $password, $salt, self::ITERATION_COUNT, (2 * $keyStrengthBits + self::PWD_VERIFIER_BITS) / 8, true);
|
||||
$ctrIvSize = self::AES_BLOCK_SIZE_BITS / 8;
|
||||
$iv = str_repeat(chr(0), $ctrIvSize);
|
||||
|
||||
@@ -202,6 +206,10 @@ class WinZipAesEngine
|
||||
$password = $this->entry->getPassword();
|
||||
assert($password !== null);
|
||||
|
||||
// WinZip 99-character limit
|
||||
// @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/
|
||||
$password = substr($password, 0, 99);
|
||||
|
||||
$keyStrengthBytes = 32;
|
||||
$keyStrengthBits = $keyStrengthBytes * 8;
|
||||
|
||||
|
@@ -157,6 +157,13 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
return $zipFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ZipOutputFile
|
||||
*/
|
||||
public function edit(){
|
||||
return ZipOutputFile::openFromZipFile($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check zip file signature
|
||||
*
|
||||
|
@@ -152,6 +152,22 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
return new self($zipFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Open zip file from update.
|
||||
*
|
||||
* @param string $filename
|
||||
* @return ZipOutputFile
|
||||
* @throws IllegalArgumentException
|
||||
* @see ZipOutputFile::__construct()
|
||||
*/
|
||||
public static function openFromFile($filename)
|
||||
{
|
||||
if (empty($filename)) {
|
||||
throw new IllegalArgumentException("Zip file is null");
|
||||
}
|
||||
return new self(ZipFile::openFromFile($filename));
|
||||
}
|
||||
|
||||
/**
|
||||
* Count zip entries.
|
||||
*
|
||||
|
@@ -279,11 +279,9 @@ class ZipTest extends ZipTestCase
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$zipFile = ZipFile::openFromFile($this->outputFilename);
|
||||
$outputZipFile = new ZipOutputFile($zipFile);
|
||||
$outputZipFile = ZipOutputFile::openFromFile($this->outputFilename);
|
||||
$outputZipFile->rename($oldName, $newName);
|
||||
$outputZipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
@@ -309,7 +307,7 @@ class ZipTest extends ZipTestCase
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$zipFile = ZipFile::openFromFile($this->outputFilename);
|
||||
$outputZipFile = new ZipOutputFile($zipFile);
|
||||
$outputZipFile = $zipFile->edit();
|
||||
$outputZipFile->deleteFromName($deleteEntryName);
|
||||
$outputZipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
@@ -752,7 +750,7 @@ class ZipTest extends ZipTestCase
|
||||
*/
|
||||
public function testSetPassword()
|
||||
{
|
||||
$password = CryptoUtil::randomBytes(100);
|
||||
$password = base64_encode(CryptoUtil::randomBytes(100));
|
||||
$badPassword = "sdgt43r23wefe";
|
||||
|
||||
$outputZip = ZipOutputFile::create();
|
||||
@@ -761,6 +759,8 @@ class ZipTest extends ZipTestCase
|
||||
$outputZip->saveAsFile($this->outputFilename);
|
||||
$outputZip->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename, $password);
|
||||
|
||||
$zipFile = ZipFile::openFromFile($this->outputFilename);
|
||||
|
||||
// set bad password Traditional Encryption
|
||||
@@ -791,6 +791,8 @@ class ZipTest extends ZipTestCase
|
||||
$outputZip->close();
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename, $password);
|
||||
|
||||
// check from WinZip AES encryption
|
||||
$zipFile = ZipFile::openFromFile($this->outputFilename);
|
||||
|
||||
@@ -1083,6 +1085,7 @@ class ZipTest extends ZipTestCase
|
||||
$zipFile = ZipFile::openFromFile($this->outputFilename);
|
||||
self::assertEquals($zipFile->count(), $countFiles);
|
||||
foreach ($zipFile as $entry => $content) {
|
||||
strlen($content);
|
||||
}
|
||||
$zipFile->close();
|
||||
}
|
||||
|
@@ -9,18 +9,47 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase
|
||||
/**
|
||||
* Assert correct zip archive.
|
||||
*
|
||||
* @param $filename
|
||||
* @param string $filename
|
||||
* @param string|null $password
|
||||
*/
|
||||
public static function assertCorrectZipArchive($filename)
|
||||
public static function assertCorrectZipArchive($filename, $password = null)
|
||||
{
|
||||
if (DIRECTORY_SEPARATOR !== '\\' && `which zip`) {
|
||||
exec("zip -T " . escapeshellarg($filename), $output, $returnCode);
|
||||
if (DIRECTORY_SEPARATOR !== '\\' && `which unzip`) {
|
||||
$command = "unzip";
|
||||
if ($password !== null) {
|
||||
$command .= " -P " . escapeshellarg($password);
|
||||
}
|
||||
$command .= " -t " . escapeshellarg($filename);
|
||||
exec($command, $output, $returnCode);
|
||||
|
||||
$output = implode(PHP_EOL, $output);
|
||||
|
||||
self::assertEquals($returnCode, 0);
|
||||
self::assertNotContains('zip error', $output);
|
||||
self::assertContains(' OK', $output);
|
||||
if ($password !== null && $returnCode === 81) {
|
||||
if(`which 7z`){
|
||||
// WinZip 99-character limit
|
||||
// @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/
|
||||
$password = substr($password, 0, 99);
|
||||
|
||||
$command = "7z t -p" . escapeshellarg($password). " " . escapeshellarg($filename);
|
||||
exec($command, $output, $returnCode);
|
||||
|
||||
$output = implode(PHP_EOL, $output);
|
||||
|
||||
self::assertEquals($returnCode, 0);
|
||||
self::assertNotContains(' Errors', $output);
|
||||
self::assertContains(' Ok', $output);
|
||||
}
|
||||
else{
|
||||
fwrite(STDERR, 'Program unzip cannot support this function.'.PHP_EOL);
|
||||
fwrite(STDERR, 'Please install 7z. For Ubuntu-like: sudo apt-get install p7zip-full'.PHP_EOL);
|
||||
}
|
||||
}
|
||||
else {
|
||||
self::assertEquals($returnCode, 0);
|
||||
self::assertNotContains('incorrect password', $output);
|
||||
self::assertContains(' OK', $output);
|
||||
self::assertContains('No errors', $output);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +81,6 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase
|
||||
exec("zipalign -c -v 4 " . escapeshellarg($filename), $output, $returnCode);
|
||||
return $returnCode === 0;
|
||||
} else {
|
||||
echo 'Can not find program "zipalign" for test' . PHP_EOL;
|
||||
fwrite(STDERR, 'Can not find program "zipalign" for test');
|
||||
return null;
|
||||
}
|
||||
|
Reference in New Issue
Block a user