Skip to content

Commit

Permalink
Merge pull request #1 from jakubboucek/v2
Browse files Browse the repository at this point in the history
Project design update & refctoring
  • Loading branch information
jakubboucek authored Jun 18, 2023
2 parents b68df06 + 02e3efb commit 763eabc
Show file tree
Hide file tree
Showing 58 changed files with 12,061 additions and 383 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.gitignore export-ignore
.github export-ignore
example.php export-ignore
tests export-ignore
29 changes: 23 additions & 6 deletions .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ jobs:
- name: PHPStan
run: composer phpstan

- name: Easy Coding Standard
run: composer ecs
- name: Unit tests
run: composer test

name: ${{ matrix.actions.name }}
php:
- '8.0'
- '8.1'
- '8.2'

name: ${{ matrix.actions.name }} at PHP ${{ matrix.php }}
runs-on: ubuntu-latest

steps:
Expand All @@ -29,10 +34,22 @@ jobs:
# see https://github.com/shivammathur/setup-php
uses: shivammathur/setup-php@v2
with:
php-version: 7.3
php-version: ${{ matrix.php }}
coverage: none

# see https://github.com/actions/cache/blob/main/examples.md#php---composer
- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- uses: actions/cache@v2
with:
path: |
${{ steps.composer-cache.outputs.dir }}
**/composer.lock
key: ${{ runner.os }}-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}

- name: Install Composer
run: composer install --no-progress
run: composer update --no-progress

- run: ${{ matrix.actions.run }}
- run: ${{ matrix.actions.run }}
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor
composer.lock
/.phpunit.result.cache
/composer.lock
/vendor/
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Jakub Bouček
Copyright (c) 2023 Jakub Bouček

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
52 changes: 27 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ Reader for TAR and TAR+GZip Archives, optimized for read huge size archives, eff
## Features

- Read **TAR archives** from disk
- Support **GZipped archives**
- Support **GZipped** and **BZipped** archives
- Iterate over Archive content
- Get **name**, **size** and **type** of each file
- Recognize Directory files type
- Recognize Regular files type
- Get **content** of files
- Scan file list only mode (doesn't read file's content from disk)
- Allows to export content to files
- Optimized for performance and low-memory
- Use stream-first access - file content not stored to memory

## Install

Expand All @@ -23,40 +25,40 @@ composer require jakubboucek/tar-stream-reader

Read files from an archive:
```php
foreach (ArchiveReader::read('example.tar') as $filename => $fileInfo) {
echo "File {$filename} is {$fileInfo->getSize()} bytes size, content of file:\n";
echo $fileInfo->getContent() . "\n\n";
}
```
use JakubBoucek\Tar;

Only scan files from an archive:
```php
foreach (ArchiveReader::scan('example.tar') as $filename => $fileInfo) {
echo "File {$filename} is {$fileInfo->getSize()} bytes size.\n";
foreach (new Tar\FileReader('example.tar') as $file) {
echo "File {$file->getName()} is {$file->getSize()} bytes size, content of file:\n";
echo $fileInfo->getContent() . "\n";
}
```
Scan mode is skipping contents od files in Archive. It's faster and less memory consume than read mode.

Define type of archive:
Package recognizes few types of Archive when using classic filename extension (e.g.: `.tar`, `.tgz`, `.tar.bz2`), but
You can explicitly define archive type thought second parameter:
```php
foreach (ArchiveReader::scan('example.tgz') as $filename => $fileInfo) {
echo "File {$filename} is {$fileInfo->getSize()} bytes size.\n";
use JakubBoucek\Tar;

foreach (new Tar\FileReader('example.tar+gzip', new Tar\Filehandler\Gzip()) as $file) {
echo "File {$file->getName()} is {$file->getSize()} bytes size.\n";
}
```
Package recognize right type of Archive when using classic filename extension (`.tar`, `.tgz`, `.tar.gz`), but you can
set archive type manually by second parameter `ArchiveReader::TYPE_GZ`:

Package allows to process any type of stream, use `StreamReader` instead of `FileReader`:
```php
foreach (ArchiveReader::scan('example.tar+gzip', ArchiveReader::TYPE_GZ) as $filename => $fileInfo) {
echo "File {$filename} is {$fileInfo->getSize()} bytes size.\n";
use JakubBoucek\Tar;

$stream = someBeatifulFuntionToGetStream();

foreach (new Tar\StreamReader($stream) as $file) {
echo "File {$file->getName()} is {$file->getSize()} bytes size.\n";
}
```


## FAQ

### Can I use Package for ZIP, RAR, BZ, … archives?
### Can I use Package for ZIP, RAR, … archives?

No, Package recognize only TAR Archive format, additionaly recognize GZipped Archive.
No, Package recognize only TAR Archive format, additionaly recognize GZipped or BZipped Archive.

### Can I use Package to create/modify Archive?

Expand All @@ -71,8 +73,8 @@ No, TAR Archive is stream-based format, it does not support search, you must alw
Here are two scopes of this question: **Archive size** or **Size of files in Archive**

- **Archive size** is teoretically unlimited, beacuse package is using stream very effective.
- **Size of files in Archive** is in read mode limited to available RAM because Content of each file is directly
loaded to variable. For each file content is not available another read method.
- **Size of files in Archive** is teoretically unlimited when use steam-based method to extraxt content
(`toFile()` or `toStream()`), otherwise is size limited with available memory, because is content filled into variable.

## Contributing
Please don't hesitate send Issue or Pull Request.
Expand All @@ -81,4 +83,4 @@ Please don't hesitate send Issue or Pull Request.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.

## License
The MIT License (MIT). Please see [License File](LICENSE) for more information.
The MIT License (MIT). Please see [License File](LICENSE) for more information.
22 changes: 15 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
{
"name": "jakubboucek/tar-stream-reader",
"description": "Reader for TAR and TAR+GZip Archives, optimized for read huge size archives, effective memory usage.",
"type": "library",
"license": "MIT",
"type": "library",
"authors": [
{
"name": "Jakub Bouček",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.3",
"ext-zlib": "*"
"php": "~8.0.0 || ~8.1.0 || ~8.2.0",
"psr/http-message": "~1.0 || ~2.0"
},
"require-dev": {
"phpstan/phpstan": "^0.12.83",
"symplify/easy-coding-standard": "^9.2"
"phpstan/phpstan": "1.10.18",
"phpunit/phpunit": "^9.0 || ^10.0"
},
"suggest": {
"ext-bz2": "Needed to support BZ2 compressed archive format",
"ext-zlib": "Needed to support GZipped archive format"
},
"autoload": {
"psr-4": {
"JakubBoucek\\Tar\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"JakubBoucek\\Tar\\Tests\\": "tests/"
}
},
"scripts": {
"phpstan": "phpstan analyse src --level 7",
"ecs": "ecs check src",
"ecs-fix": "ecs check src --fix"
"test": "phpunit tests"
}
}
29 changes: 0 additions & 29 deletions ecs.php

This file was deleted.

19 changes: 6 additions & 13 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use JakubBoucek\Tar\ArchiveReader;
use JakubBoucek\Tar\FileReader;

require __DIR__ . '/vendor/autoload.php';

Expand All @@ -19,19 +19,12 @@

$archiveFile = $argv[1];

$reader = ArchiveReader::scan($archiveFile);


echo sprintf("%10s\t%10s\t%4s\t%s\n", 'File size', 'Memory', 'Type', 'File name');
foreach ($reader as $filename => $file) {
foreach (new FileReader($archiveFile) as $file) {
echo sprintf(
"%10d\t%10d\t%4s\t%s\n",
"%20s: announced %6s bytes, really %6s bytes, hash: %s\n",
$file->getName(),
$file->getSize(),
memory_get_usage(),
$file->isDir() ? 'dir' : 'file',
$filename
strlen((string)$file->getContent()),
md5((string)$file->getContent())
);

// You can also read content of file:
//$file->getContent();
}
19 changes: 19 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile=".phpunit.result.cache"
failOnWarning="true"
>
<testsuites>
<testsuite name="Unit tests">
<directory>tests</directory>
</testsuite>
</testsuites>

<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
66 changes: 0 additions & 66 deletions src/ArchiveReader.php

This file was deleted.

9 changes: 9 additions & 0 deletions src/Exception/EofException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace JakubBoucek\Tar\Exception;

class EofException extends \RuntimeException
{
}
9 changes: 9 additions & 0 deletions src/Exception/FileContentClosedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace JakubBoucek\Tar\Exception;

class FileContentClosedException extends \LogicException
{
}
Loading

0 comments on commit 763eabc

Please sign in to comment.