Skip to content

Commit 1920a69

Browse files
committed
Merge pull request #16 from mcuadros/master
Method/Native/TarExtractor: handling files in dirs
2 parents 2b858ca + ef68362 commit 1920a69

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

src/Method/Native/TarExtractor.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,22 @@ protected function extractTarFile($filename, $target)
210210
$name = $fields['name'];
211211
$type = $fields['type'];
212212

213+
$location = $target . DIRECTORY_SEPARATOR . $name;
213214
if (self::TYPE_FILE === $type) {
214215
$size = $fields['size'];
215216
if (0 === $size) {
216217
continue;
217218
}
218219

220+
// create the dir
221+
$this->getFilesystem()->mkdir(dirname($location));
222+
219223
// create the file
220-
file_put_contents($target . DIRECTORY_SEPARATOR . $name, $this->readBlockData($fileHandler, $size));
224+
file_put_contents($location, $this->readBlockData($fileHandler, $size));
221225

222226
continue;
223227
} elseif (self::TYPE_DIRECTORY === $type) {
224-
$this->getFilesystem()->mkdir($target . DIRECTORY_SEPARATOR . $name);
228+
$this->getFilesystem()->mkdir($location);
225229
}
226230
}
227231

tests/Method/AbstractMethodTest.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ public function testAllFormatsSupportedByMethod()
7979
{
8080
// check supported formats (ok and invalid data)
8181
foreach ($this->supportedFormats as $format) {
82-
if (file_exists($this->filesPath.'file_ok.' . $format->getExtensions()[0])) {
83-
$this->checkSupportedValidFormatUsingMethod($format);
84-
$this->checkSupportedInvalidFormatUsingMethod($format);
82+
foreach ($this->getOkTestResources($format) as $file) {
83+
$this->checkSupportedValidFormatUsingMethod($file, $format);
8584
}
85+
86+
$this->checkSupportedInvalidFormatUsingMethod($format);
8687
}
8788

8889
// check unsupported formats
@@ -91,13 +92,26 @@ public function testAllFormatsSupportedByMethod()
9192
}
9293
}
9394

94-
public function checkSupportedValidFormatUsingMethod(FormatInterface $format)
95+
protected function getOkTestResources(FormatInterface $format)
96+
{
97+
$valid = ['file_ok', 'file_ok_no_dirs'];
98+
$ext = $format->getExtensions()[0];
99+
$files = [];
100+
foreach ($valid as $base) {
101+
$filename = $base.'.'.$ext;
102+
if (file_exists($this->filesPath.$filename)) {
103+
$files[] = $filename;
104+
}
105+
}
106+
107+
return $files;
108+
}
109+
110+
public function checkSupportedValidFormatUsingMethod($file, FormatInterface $format)
95111
{
96112
$target = $this->getTemporaryPath();
97113
$this->clearTemporaryPath();
98114

99-
$file = 'file_ok.' . $format->getExtensions()[0];
100-
101115
$response = $this->extract($file, $target, $format);
102116

103117
$this->assertTrue($response);
3 KB
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/level2/4.txt|4.txt file
2+
/level2/level3/5.txt|5.txt file

tests/Resources/scripts/tar.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ dd if=/dev/urandom of=$FILES_DIR/file_fake.tar bs=1 count=1240
2121
# tar: regular file
2222
cd $FILES_DIR/uncompressed
2323
tar -cvf ../file_ok.tar *
24-
printf "/1.txt|1.txt file\n/2.txt|2.txt file\n/3.txt|3.txt file\n/level2/4.txt|4.txt file\n/level2/level3/5.txt|5.txt file" > ../file_ok.tar.key
24+
printf "/1.txt|1.txt file\n/2.txt|2.txt file\n/3.txt|3.txt file\n/level2/4.txt|4.txt file\n/level2/level3/5.txt|5.txt file" > ../file_ok.tar.key
25+
26+
# tar: files in dirs, without dirs
27+
tar -cvf ../file_ok_no_dirs.tar level2/4.txt level2/level3/5.txt
28+
printf "/level2/4.txt|4.txt file\n/level2/level3/5.txt|5.txt file" > ../file_ok_no_dirs.tar.key

0 commit comments

Comments
 (0)