Skip to content

Commit

Permalink
Merge pull request #37 from kiwilan/develop
Browse files Browse the repository at this point in the history
v2.1.02
  • Loading branch information
ewilan-riviere authored Sep 20, 2023
2 parents 757690c + 9b88ce3 commit f65f9b3
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 32 deletions.
50 changes: 25 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ Supports Linux, macOS and Windows.
- [`imagick`](https://www.php.net/manual/en/book.imagick.php) (optional) for `.PDF`
- [`bz2`](https://www.php.net/manual/en/book.bzip2.php) (optional) for `.BZ2` archives

| Type | Supported | Requirement | Uses |
| :-----------------------: | :-------: | :------------------------------------------------------------------------------------------------------: | :------------------: |
| `.zip`, `.epub`, `.cbz` || N/A | N/A |
| `.tar`, `.tar.gz`, `.cbt` || N/A | N/A |
| `.rar`, `.cbr` || [`rar` PHP extension](https://github.com/cataphract/php-rar) or [`p7zip`](https://www.7-zip.org/) binary | PHP `rar` or `p7zip` |
| `.7z`, `.cb7` || [`p7zip`](https://www.7-zip.org/) binary | `p7zip` binary |
| `.pdf` || Optional (for extraction) [`imagick` PHP extension](https://github.com/Imagick/imagick) | `smalot/pdfparser` |
| Type | Supported | Requirement | Uses |
| :-----------------------: | :-------: | :------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------: |
| `.zip`, `.epub`, `.cbz` || N/A | Uses [`zip` extension](https://www.php.net/manual/en/book.zip.php) |
| `.tar`, `.tar.gz`, `.cbt` || N/A | Uses [`phar` extension](https://www.php.net/manual/en/book.phar.php) |
| `.rar`, `.cbr` || [`rar` PHP extension](https://github.com/cataphract/php-rar) or [`p7zip`](https://www.7-zip.org/) binary | PHP `rar` or `p7zip` |
| `.7z`, `.cb7` || [`p7zip`](https://www.7-zip.org/) binary | `p7zip` binary |
| `.pdf` || Optional (for extraction) [`imagick` PHP extension](https://github.com/Imagick/imagick) | `smalot/pdfparser` |

> **Note**
>
Expand All @@ -53,26 +53,26 @@ If you want more information, you can read section [**About**](#about).
## Features

- List files as `ArchiveItem` array
- With `getFiles` method: list of files
- With `getFirst` method: first file
- With `getLast` method: last file
- With `find` method: find first file that match with `path` property
- With `filter` method: find all files that match with `path` property
- With `getFiles()` method: list of files
- With `getFirst()` method: first file
- With `getLast()` method: last file
- With `find()` method: find first file that match with `path` property
- With `filter()` method: find all files that match with `path` property
- Content of file
- With `getContent` method: content of file as string (useful for images)
- With `getText` method: content of text file (binaries files return `null`)
- With `getContents()` method: content of file as string (useful for images)
- With `getText()` method: content of text file (binaries files return `null`)
- Extract files
- With `extract` method: extract files to directory
- With `extractAll` method: extract all files to directory
- Stat of archive with `getPath`, `getDeviceNumber`, `getInodeNumber`, `getInodeProtectionMode`, `getNumberOfLinks`, `getUserId`, `getGroupId`, `getDeviceType`, `getSize`, `getLastAccessAt`, `getCreatedAt`, `getModifiedAt`, `getBlockSize`, `getNumberOfBlocks`, `getStatus`, `getComment` properties
- PDF metadata: `getTitle`, `getAuthor`, `getSubject`, `getCreator`, `getCreationDate`, `getModDate`, `getPages`,
- With `extract()` method: extract files to directory
- With `extractAll()` method: extract all files to directory
- Stat of archive corresponding to [`stat`](https://www.php.net/manual/en/function.stat.php)
- PDF metadata: `getTitle()`, `getAuthor()`, `getSubject()`, `getCreator()`, `getCreationDate()`, `getModDate()`, `getPages()`,
- Count files
- Create or edit archives, only with `.zip` format
- With `make` method: create or edit archive
- With `addFiles` method: add files to archive
- With `addFromString` method: add string to archive
- With `addDirectory` and `addDirectories` methods: add directories to archive
- With `save` method: save archive
- With `make()` method: create or edit archive
- With `addFiles()` method: add files to archive
- With `addFromString()` method: add string to archive
- With `addDirectory()` and `addDirectories()` methods: add directories to archive
- With `save()` method: save archive

## Installation

Expand All @@ -96,7 +96,7 @@ $count = $archive->getCount(); // int of files count

$images = $archive->filter('jpeg'); // ArchiveItem[] with `jpeg` in their path
$metadataXml = $archive->find('metadata.xml'); // First ArchiveItem with `metadata.xml` in their path
$content = $archive->getContent($metadataXml); // `metadata.xml` file content
$content = $archive->getContents($metadataXml); // `metadata.xml` file content

$paths = $archive->extract('/path/to/directory', [$metadataXml]); // string[] of extracted files paths
$paths = $archive->extractAll('/path/to/directory'); // string[] of extracted files paths
Expand All @@ -109,7 +109,7 @@ $archive = Archive::read('path/to/file.pdf');

$pdf = $archive->getPdf(); // Metadata of PDF

$content = $archive->getContent($archive->getFirst()); // PDF page as image
$content = $archive->getContents($archive->getFirst()); // PDF page as image
$text = $archive->getText($archive->getFirst()); // PDF page as text
```

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kiwilan/php-archive",
"version": "2.1.01",
"version": "2.1.02",
"description": "PHP package to handle archives (.zip, .rar, .tar, .7z) or .pdf with hybrid solution (native/p7zip), designed to works with eBooks (.epub, .cbz, .cbr, .cb7, .cbt).",
"keywords": [
"php",
Expand Down
10 changes: 9 additions & 1 deletion src/Readers/ArchivePdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function extract(string $toPath, array $files): array
$paths = [];
foreach ($this->files as $file) {
if (in_array($file, $files)) {
$content = $this->getContent($file);
$content = $this->getContents($file);
$toPathFile = "{$toPath}{$file->getPath()}.{$this->pdfExt}";

if (! is_dir(dirname($toPathFile))) {
Expand All @@ -47,7 +47,15 @@ public function extract(string $toPath, array $files): array
return $paths;
}

/**
* @deprecated Use `getContents()` instead
*/
public function getContent(?ArchiveItem $file, bool $toBase64 = false): ?string
{
return $this->getContents($file, $toBase64);
}

public function getContents(?ArchiveItem $file, bool $toBase64 = false): ?string
{
if (! $file) {
return null;
Expand Down
12 changes: 10 additions & 2 deletions src/Readers/ArchivePhar.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ public static function read(string $path): self
return $self;
}

/**
* @deprecated Use `getContents()` instead
*/
public function getContent(?ArchiveItem $file, bool $toBase64 = false): ?string
{
return $this->getContents($file, $toBase64);
}

public function getContents(?ArchiveItem $file, bool $toBase64 = false): ?string
{
if (! $file) {
return null;
Expand All @@ -35,14 +43,14 @@ public function getText(ArchiveItem $file): ?string
throw new \Exception("Error, {$file->getFilename()} is an image");
}

return $this->getContent($file);
return $this->getContents($file);
}

public function extract(string $toPath, array $files): array
{
$paths = [];
foreach ($files as $file) {
$content = $this->getContent($file);
$content = $this->getContents($file);

$toPathFile = "{$toPath}{$file->getRootPath()}";

Expand Down
8 changes: 8 additions & 0 deletions src/Readers/ArchiveRar.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ public function extract(string $toPath, array $files): array
return $paths;
}

/**
* @deprecated Use `getContents()` instead
*/
public function getContent(?ArchiveItem $item, bool $toBase64 = false): ?string
{
return $this->getContents($item, $toBase64);
}

public function getContents(?ArchiveItem $item, bool $toBase64 = false): ?string
{
if (! $item) {
return null;
Expand Down
8 changes: 8 additions & 0 deletions src/Readers/ArchiveSevenZip.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ public static function read(string $path): self
return $self;
}

/**
* @deprecated Use `getContents()` instead
*/
public function getContent(?ArchiveItem $file, bool $toBase64 = false): ?string
{
return $this->getContents($file, $toBase64);
}

public function getContents(?ArchiveItem $file, bool $toBase64 = false): ?string
{
if (! $file) {
return null;
Expand Down
8 changes: 8 additions & 0 deletions src/Readers/ArchiveZip.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ public function extractAll(string $toPath): array
return $files;
}

/**
* @deprecated Use `getContents()` instead
*/
public function getContent(?ArchiveItem $file, bool $toBase64 = false): ?string
{
return $this->getContents($file, $toBase64);
}

public function getContents(?ArchiveItem $file, bool $toBase64 = false): ?string
{
if (! $file) {
return null;
Expand Down
7 changes: 7 additions & 0 deletions src/Readers/BaseArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,16 @@ public function getPdf(): ?PdfMeta

/**
* Get content from file.
*
* @deprecated Use `getContents()` instead
*/
abstract public function getContent(?ArchiveItem $file, bool $toBase64 = false): ?string;

/**
* Get content from file.
*/
abstract public function getContents(?ArchiveItem $file, bool $toBase64 = false): ?string;

/**
* Get text from file.
*/
Expand Down
6 changes: 4 additions & 2 deletions tests/ArchiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,22 @@ function (Pest\Expectation $item) use ($ext) {

it('can get content first file', function (string $path) {
$archive = Archive::read($path);
$content = $archive->getContent($archive->getFirst());
$content = $archive->getContents($archive->getFirst());

$output = outputPath();
$file = BaseArchive::pathToOsPath("{$output}first.jpg");
stringToImage($content, $file);

expect($content)->toBeString();
expect($file)->toBeReadableFile();

$content = $archive->getContent($archive->getFirst());
})->with([...ARCHIVES_ZIP, ...ARCHIVES_TAR, ...ARCHIVES_RAR, SEVENZIP]);

it('can get cover', function (string $path) {
$archive = Archive::read($path);
$cover = $archive->find('cover.jpeg');
$content = $archive->getContent($cover);
$content = $archive->getContents($cover);

$output = outputPath();
$coverPath = "{$output}cover.jpeg";
Expand Down
2 changes: 1 addition & 1 deletion tests/PdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

it('can get content first file', function () {
$archive = Archive::read(PDF);
$content = $archive->getContent($archive->getFirst());
$content = $archive->getContents($archive->getFirst());

$output = outputPath();
$file = "{$output}first.jpg";
Expand Down

0 comments on commit f65f9b3

Please sign in to comment.