Skip to content

Commit 04b2fba

Browse files
Merge pull request #33 from kiwilan/develop
2.1.0
2 parents 24892da + 38ba793 commit 04b2fba

File tree

4 files changed

+91
-69
lines changed

4 files changed

+91
-69
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,17 @@ Works only with `.zip` archives.
148148

149149
```php
150150
$archive = Archive::make('path/to/archive.zip');
151-
$archive->addFiles([
152-
'path/to/file1.txt',
153-
'path/to/file2.txt',
154-
'path/to/file3.txt',
155-
]);
151+
$files = [
152+
'path/to/file/in/archive-file1.txt' => 'path/to/real-file1.txt',
153+
'path/to/file/in/archive-file2.txt' => 'path/to/real-file2.txt',
154+
'path/to/file/in/archive-file3.txt' => 'path/to/real-file3.txt',
155+
];
156+
157+
foreach ($files as $pathInArchive => $pathToRealFile) {
158+
$archive->addFile($pathInArchive, $pathToRealFile);
159+
}
156160
$archive->addFromString('test.txt', 'Hello World!');
157-
$archive->addDirectory('path/to/directory');
161+
$archive->addDirectory('./directory', 'path/to/directory');
158162
$archive->save();
159163
```
160164

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kiwilan/php-archive",
3-
"version": "2.0.02",
3+
"version": "2.1.0",
44
"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).",
55
"keywords": [
66
"php",

src/ArchiveZipCreate.php

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010
class ArchiveZipCreate
1111
{
1212
/**
13-
* @param SplFileInfo[] $files
14-
* @param array<string, string> $strings
13+
* @param ArchiveFile[] $files
1514
*/
1615
protected function __construct(
1716
protected string $path,
1817
protected string $name,
1918
protected array $files = [],
20-
protected array $strings = [],
2119
protected int $count = 0,
2220
) {
2321
}
@@ -64,85 +62,82 @@ public function getCount(): int
6462
}
6563

6664
/**
67-
* @return SplFileInfo[]
65+
* @return ArchiveFile[]
6866
*/
6967
public function getFiles(): array
7068
{
7169
return $this->files;
7270
}
7371

7472
/**
75-
* @return array<string, string>
73+
* Add a new file to the archive from existing file.
74+
*
75+
* @param string $outputPath Path to the file inside the archive
76+
* @param string $pathToFile Path to the file to add
7677
*/
77-
public function getStrings(): array
78-
{
79-
return $this->strings;
80-
}
81-
82-
public function addFile(string $path): self
78+
public function addFile(string $outputPath, string $pathToFile): self
8379
{
84-
$this->files[] = new SplFileInfo($path);
80+
$this->files[] = new ArchiveFile($outputPath, new SplFileInfo($pathToFile));
8581
$this->count++;
8682

8783
return $this;
8884
}
8985

90-
public function addFromString(string $filename, string $content): self
86+
/**
87+
* Add a new file to the archive from string.
88+
*
89+
* @param string $outputPath Path to the file inside the archive
90+
* @param string $content Content of the file to add
91+
*/
92+
public function addFromString(string $outputPath, string $content): self
9193
{
92-
$this->strings[$filename] = $content;
94+
$this->files[] = new ArchiveFile($outputPath, null, $content);
9395
$this->count++;
9496

9597
return $this;
9698
}
9799

98-
public function addFiles(array $paths): self
99-
{
100-
foreach ($paths as $path) {
101-
$this->addFile($path);
102-
}
103-
104-
return $this;
105-
}
106-
107-
public function addDirectory(string $path): self
100+
/**
101+
* Add a full directory to the archive, including subdirectories.
102+
*
103+
* @param string $relativeTo Relative path to the directory inside the archive
104+
* @param string $path Path to the directory to add
105+
*
106+
* ```php
107+
* $archive->addDirectory('./to/directory', '/path/to/directory');
108+
* ```
109+
*/
110+
public function addDirectory(string $relativeTo, string $path): self
108111
{
109-
$files = $this->pathsToSplFiles($this->directoryToPaths($path));
112+
$files = $this->pathsToSplFiles($this->directoryToPaths($path, $relativeTo));
110113
$this->files = [...$this->files, ...$files];
111114
$this->count = count($this->files);
112115

113116
return $this;
114117
}
115118

116-
public function addDirectories(array $paths): self
117-
{
118-
foreach ($paths as $path) {
119-
$this->addDirectory($path);
120-
}
121-
122-
return $this;
123-
}
124-
125-
public function save(): self
119+
/**
120+
* Save the archive.
121+
*/
122+
public function save(): bool
126123
{
127124
$zip = new ZipArchive();
128125
$zip->open($this->path, ZipArchive::CREATE);
129126

130127
foreach ($this->files as $file) {
131-
$zip->addFile($file->getRealPath(), $file->getFilename());
132-
}
133-
134-
foreach ($this->strings as $filename => $content) {
135-
$zip->addFromString($filename, $content);
128+
$content = $file->content;
129+
if (! $file->isString) {
130+
$content = file_get_contents($file->file->getPathname());
131+
}
132+
$zip->addFromString($file->outputPath, $content);
136133
}
137134

138135
$this->count = $zip->numFiles;
139136

140-
$zip->close();
141-
142-
return $this;
137+
return $zip->close();
143138
}
144139

145-
protected function directoryToPaths(string $path): array
140+
protected function directoryToPaths(string $path, string $relativeTo): array
146141
{
147142
$files = [];
148143
$directory = new RecursiveDirectoryIterator($path);
@@ -152,7 +147,9 @@ protected function directoryToPaths(string $path): array
152147
if ($file->isDir()) {
153148
continue;
154149
}
155-
$files[] = $file->getPathname();
150+
151+
$outputPath = str_replace($path, $relativeTo, $file->getPathname());
152+
$files[] = $this->addFile($outputPath, $file->getPathname());
156153
}
157154

158155
return $files;
@@ -176,3 +173,17 @@ protected function pathsToSplFiles(array $paths): array
176173
return $files;
177174
}
178175
}
176+
177+
class ArchiveFile
178+
{
179+
public function __construct(
180+
public string $outputPath,
181+
public ?SplFileInfo $file = null,
182+
public ?string $content = null,
183+
public bool $isString = false,
184+
) {
185+
if (! $this->file) {
186+
$this->isString = true;
187+
}
188+
}
189+
}

tests/ArchiveCreateTest.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Kiwilan\Archive\Archive;
4+
use Kiwilan\Archive\ArchiveFile;
45

56
beforeEach(function () {
67
recurseRmdir(outputPath());
@@ -9,35 +10,40 @@
910
it('can create', function () {
1011
$path = outputPath(filename: 'test.zip');
1112
$medias = [
12-
mediaPath('archive/cover.jpeg'),
13-
mediaPath('archive/file-1.md'),
14-
mediaPath('archive/file-2.md'),
15-
mediaPath('archive/file-3.md'),
16-
mediaPath('archive/metadata.xml'),
13+
'archive/cover.jpeg' => mediaPath('archive/cover.jpeg'),
14+
'archive/file-1.md' => mediaPath('archive/file-1.md'),
15+
'archive/file-2.md' => mediaPath('archive/file-2.md'),
16+
'archive/file-3.md' => mediaPath('archive/file-3.md'),
17+
'archive/metadata.xml' => mediaPath('archive/metadata.xml'),
1718
];
1819
$archive = Archive::make($path);
19-
$archive->addFiles($medias);
20+
foreach ($medias as $output => $media) {
21+
$archive->addFile($output, $media);
22+
}
2023
$archive->save();
2124

2225
expect($archive->getPath())->toBe($path);
2326
expect($archive->getName())->toBe('test.zip');
2427
expect($archive->getPath())->toBeReadableFile($path);
2528
expect($archive->getCount())->toBe(5);
2629
expect($archive->getFiles())->toBeArray()
27-
->each(fn ($file) => expect($file->value)->toBeInstanceOf(SplFileInfo::class));
30+
->each(fn ($file) => expect($file->value)->toBeInstanceOf(ArchiveFile::class));
2831
});
2932

3033
it('can create with files', function () {
3134
$path = outputPath(filename: 'test.zip');
35+
$files = [
36+
'archive/cover.jpeg' => mediaPath('archive/cover.jpeg'),
37+
'archive/file-1.md' => mediaPath('archive/file-1.md'),
38+
'archive/file-2.md' => mediaPath('archive/file-2.md'),
39+
'archive/file-3.md' => mediaPath('archive/file-3.md'),
40+
'archive/metadata.xml' => mediaPath('archive/metadata.xml'),
41+
];
3242

3343
$archive = Archive::make($path);
34-
$archive->addFiles([
35-
mediaPath('archive/cover.jpeg'),
36-
mediaPath('archive/file-1.md'),
37-
mediaPath('archive/file-2.md'),
38-
mediaPath('archive/file-3.md'),
39-
mediaPath('archive/metadata.xml'),
40-
]);
44+
foreach ($files as $path => $file) {
45+
$archive->addFile($path, $file);
46+
}
4147
$archive->addFromString('test.txt', 'Hello World!');
4248
$archive->save();
4349

@@ -61,18 +67,19 @@
6167
$path = outputPath(filename: 'test.zip');
6268

6369
$archive = Archive::make($path);
64-
$archive->addDirectory(mediaPath('archive'));
70+
$archive->addDirectory('./archive', mediaPath('archive'));
71+
$archive->addFromString('test.txt', 'Hello World!');
6572
$archive->save();
6673

6774
expect($archive->getPath())->toBeReadableFile($path);
68-
expect($archive->getCount())->toBe(5);
75+
expect($archive->getCount())->toBe(6);
6976
});
7077

7178
it('can edit', function () {
7279
$path = outputPath(filename: 'test.zip');
7380

7481
$archive = Archive::make($path);
75-
$archive->addDirectory(mediaPath('archive'));
82+
$archive->addDirectory('./archive', mediaPath('archive'));
7683
$archive->save();
7784

7885
$archive = Archive::make($path);

0 commit comments

Comments
 (0)