Skip to content

Commit 3bcfe89

Browse files
committed
Add file compression feature
- Implement file compression functionality - Use php-zip for compression - Add tests for compression Signed-off-by: rahul <[email protected]>
1 parent 005d8d5 commit 3bcfe89

File tree

3 files changed

+69
-25
lines changed

3 files changed

+69
-25
lines changed

composer.json

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
{
2-
"name": "rcsofttech85/file-handler",
3-
"description": "A simple library for abstracting various file operations and providing additional helper functions for file manipulation",
4-
"type": "library",
5-
"license": "MIT",
6-
"autoload": {
7-
"psr-4": {
8-
"rcsofttech85\\FileHandler\\": "src/"
9-
}
10-
},
11-
"authors": [
12-
{
13-
"name": "rahul",
14-
"email": "[email protected]"
15-
}
16-
],
17-
"minimum-stability": "stable",
18-
"require": {
19-
"php": ">8.2",
20-
"ext-sodium": "*",
21-
"ext-ctype": "*"
22-
},
23-
"require-dev": {
24-
"phpunit/phpunit": "^10",
25-
"squizlabs/php_codesniffer": "^3.7",
26-
"symfony/dotenv": "^6.3"
2+
"name": "rcsofttech85/file-handler",
3+
"description": "A simple library for abstracting various file operations and providing additional helper functions for file manipulation",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"rcsofttech85\\FileHandler\\": "src/"
279
}
10+
},
11+
"authors": [
12+
{
13+
"name": "rahul",
14+
"email": "[email protected]"
15+
}
16+
],
17+
"minimum-stability": "stable",
18+
"require": {
19+
"php": ">8.2",
20+
"ext-sodium": "*",
21+
"ext-ctype": "*",
22+
"ext-zip": "*"
23+
},
24+
"require-dev": {
25+
"phpunit/phpunit": "^10",
26+
"squizlabs/php_codesniffer": "^3.7",
27+
"symfony/dotenv": "^6.3",
28+
"ext-fileinfo": "*"
29+
}
2830
}

src/FileHandler.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Generator;
66
use rcsofttech85\FileHandler\Exception\FileHandlerException;
7+
use ZipArchive;
78

89
class FileHandler
910
{
@@ -45,6 +46,29 @@ public function write(string $data, ?int $length = null): void
4546
}
4647
}
4748

49+
/**
50+
* @throws FileHandlerException
51+
*/
52+
public function compress(string $filename, string $zipFilename): void
53+
{
54+
if (!file_exists($filename)) {
55+
throw new FileHandlerException('File to compress does not exist.');
56+
}
57+
58+
$zip = new ZipArchive();
59+
60+
if (!$zip->open($zipFilename, ZipArchive::CREATE)) {
61+
throw new FileHandlerException('Failed to create the ZIP archive.');
62+
}
63+
64+
if (!$zip->addFile($filename)) {
65+
throw new FileHandlerException('Failed to add the file to the ZIP archive.');
66+
}
67+
68+
69+
$zip->close();
70+
}
71+
4872
/**
4973
* @throws FileHandlerException
5074
*/

tests/unit/FileHandlerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace unit;
44

5+
use finfo;
56
use PHPUnit\Framework\Attributes\DataProvider;
67
use PHPUnit\Framework\Attributes\Test;
78
use PHPUnit\Framework\Attributes\TestDox;
@@ -134,6 +135,23 @@ public function studioIsFoundForExactNameMatch(string $keyword)
134135
$this->assertTrue($isStudioFound);
135136
}
136137

138+
#[Test]
139+
public function successfulCompression()
140+
{
141+
$testFile = 'movie.csv';
142+
$compressedZipFilename = 'compressed.zip';
143+
144+
$this->fileHandler->compress($testFile, $compressedZipFilename);
145+
146+
$fileInfo = new finfo(FILEINFO_MIME_TYPE);
147+
$mimeType = $fileInfo->file($compressedZipFilename);
148+
149+
$this->assertFileExists($compressedZipFilename);
150+
$this->assertEquals('application/zip', $mimeType);
151+
152+
unlink($compressedZipFilename);
153+
}
154+
137155
#[Test]
138156
public function toArrayMethodReturnsValidArray()
139157
{

0 commit comments

Comments
 (0)