Skip to content

Commit

Permalink
Added iteration over archive
Browse files Browse the repository at this point in the history
  • Loading branch information
wapmorgan committed May 2, 2021
1 parent 9250d18 commit c597807
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 9 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ it will archive *log2.txt* as */var/www/log2.txt* in an archive (new behaviour).
- Added `UnifiedArchive->getComment()` to get comment of an archive. Available only in `Zip` and `Rar` drivers, others return `null`.
- Added `UnifiedArchive->setComment(?string $comment)` to set comment. Available only in `Zip`.
- Added filter in `UnifiedArcihve->getFileNames()`. If works as `fnmatch()` does.
- Added ability to iterate over archive and access files data as array:
```php
$a = \wapmorgan\UnifiedArchive\UnifiedArchive::open('tests/archives/fixtures.7z');
foreach ($a as $file => $data) {
echo $file.PHP_EOL;
}

$file_data = $a['filename'];
```

**Fixed:**
- Fixed `SevenZip` driver: disabled _tar.gz, tar.bzip2_ support as it isn't supported properly and described which formats driver can create, append, modify and encrypt.
Expand Down
7 changes: 0 additions & 7 deletions src/ArchiveEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ class ArchiveEntry
/** @var string Comment */
public $comment;

/**
* @var string Path of archive entry
* @deprecated 0.1.0
* @see $path property
*/
public $filename;

/**
* ArchiveEntry constructor.
* @param $path
Expand Down
86 changes: 84 additions & 2 deletions src/UnifiedArchive.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php
namespace wapmorgan\UnifiedArchive;

use ArrayAccess;
use Countable;
use InvalidArgumentException;
use Iterator;
use wapmorgan\UnifiedArchive\Drivers\BasicDriver;
use wapmorgan\UnifiedArchive\Exceptions\ArchiveExtractionException;
use wapmorgan\UnifiedArchive\Exceptions\ArchiveModificationException;
Expand All @@ -14,7 +17,7 @@
/**
* Class which represents archive in one of supported formats.
*/
class UnifiedArchive
class UnifiedArchive implements ArrayAccess, Iterator, Countable
{
const VERSION = '1.1.3';

Expand All @@ -27,6 +30,11 @@ class UnifiedArchive
/** @var array List of files in current archive */
protected $files;

/**
* @var int
*/
protected $filesIterator = 0;

/** @var int Number of files in archive */
protected $filesQuantity;

Expand All @@ -50,7 +58,6 @@ class UnifiedArchive
* @param string $fileName Archive filename
* @param null $password
* @return UnifiedArchive|null Returns UnifiedArchive in case of successful reading of the file
* @throws UnsupportedOperationException
*/
public static function open($fileName, $password = null)
{
Expand Down Expand Up @@ -777,4 +784,79 @@ public function countUncompressedFilesSize()
{
return $this->getOriginalSize();
}

/**
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
{
return $this->hasFile($offset);
}

/**
* @param mixed $offset
* @return mixed|string
* @throws NonExistentArchiveFileException
*/
public function offsetGet($offset)
{
return $this->getFileData($offset);
}

/**
* @param mixed $offset
* @param mixed $value
* @return bool|void
* @throws ArchiveModificationException
* @throws UnsupportedOperationException
*/
public function offsetSet($offset, $value)
{
return $this->addFileFromString($offset, $value);
}

/**
* @param mixed $offset
* @return bool|int|void
* @throws ArchiveModificationException
* @throws UnsupportedOperationException
*/
public function offsetUnset($offset)
{
return $this->deleteFiles($offset);
}

public function key()
{
return $this->files[$this->filesIterator];
}

/**
* @throws NonExistentArchiveFileException
*/
public function current()
{
return $this->getFileData($this->files[$this->filesIterator]);
}

public function next()
{
$this->filesIterator++;
}

public function valid()
{
return $this->filesIterator < $this->filesQuantity;
}

public function rewind()
{
$this->filesIterator = 0;
}

public function count()
{
return $this->filesQuantity;
}
}

0 comments on commit c597807

Please sign in to comment.