Skip to content

Commit 6771ddc

Browse files
committed
Add file decompression feature
- Implement file decompression functionality - Add tests for decompression Signed-off-by: rahul <[email protected]>
1 parent 3bcfe89 commit 6771ddc

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/FileHandler.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,28 @@ public function compress(string $filename, string $zipFilename): void
6969
$zip->close();
7070
}
7171

72+
/**
73+
* @throws FileHandlerException
74+
*/
75+
public function decompress(string $zipFilename, string $extractPath = "./"): void
76+
{
77+
if (!file_exists($zipFilename)) {
78+
throw new FileHandlerException('ZIP archive does not exist.');
79+
}
80+
81+
$zip = new ZipArchive();
82+
83+
if (!$zip->open($zipFilename)) {
84+
throw new FileHandlerException('Failed to open the ZIP archive.');
85+
}
86+
87+
if (!$zip->extractTo($extractPath)) {
88+
throw new FileHandlerException('Failed to extract the ZIP archive.');
89+
}
90+
91+
$zip->close();
92+
}
93+
7294
/**
7395
* @throws FileHandlerException
7496
*/

tests/unit/FileHandlerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,27 @@ public function successfulCompression()
148148

149149
$this->assertFileExists($compressedZipFilename);
150150
$this->assertEquals('application/zip', $mimeType);
151+
}
152+
153+
#[Test]
154+
public function successfulDecompression()
155+
{
156+
$compressedZipFilename = 'compressed.zip';
157+
$extractPath = 'extracted_contents';
158+
159+
$this->fileHandler->decompress($compressedZipFilename, $extractPath);
160+
161+
$expectedContent = "Film,Genre,Lead Studio,Audience score %,Profitability,Rotten Tomatoes %,Worldwide Gross,Year\n"
162+
. "Zack and Miri Make a Porno,Romance,The Weinstein Company,70,1.747541667,64,$41.94 ,2008\n"
163+
. "Youth in Revolt,Comedy,The Weinstein Company,52,1.09,68,$19.62 ,2010\n"
164+
. "Twilight,Romance,Independent,68,6.383363636,26,$702.17 ,2011";
165+
166+
$this->assertEquals($expectedContent, file_get_contents("./extracted_contents/movie.csv"));
167+
151168

152169
unlink($compressedZipFilename);
170+
unlink("./extracted_contents/movie.csv");
171+
rmdir($extractPath);
153172
}
154173

155174
#[Test]

0 commit comments

Comments
 (0)