Skip to content

Commit 727e3f6

Browse files
committed
upd tests, add notes to readme
1 parent 414a36a commit 727e3f6

3 files changed

Lines changed: 54 additions & 8 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
- **7-zip >= 7.30 (p7zip >= 9.38)**
2222

2323

24+
### Примечания:
25+
26+
- Список файлов/директорий отображается всегда в кодировке UTF-8
27+
- При указании файлов/директорий для распаковки, их имена нужно указывать в кидировке текущей файловой системы
28+
- При распаковке архива, имена файлов/директорий запишутся в кодировке текущей файловой системы
29+
30+
2431
### Установка через composer:
2532

2633
- Добавьте проект в ваш файл composer.json:

Tests/Archive7z/Archive7zTest.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ protected function setUp()
2323
$this->mock = $this->getMock('Archive7z\Archive7z', null, array('fake.7z', $this->cliPath));
2424
}
2525

26+
27+
protected function getCurrentFilesystemEncoding()
28+
{
29+
if (stripos(PHP_OS, 'WIN') !== false) { // windows
30+
return 'Windows-1251';
31+
}
32+
return exec('locale charmap');
33+
}
34+
35+
2636
protected function tearDown()
2737
{
2838
$this->cleanDir($this->tmpDir);
@@ -110,7 +120,7 @@ public function testExtract()
110120
public function testExtractCyrillic()
111121
{
112122
$dirCyrillic = $this->tmpDir . '/папка';
113-
$chavezFile = iconv('UTF-8', 'Windows-1251', 'чавес.jpg');
123+
$chavezFile = iconv('UTF-8', $this->getCurrentFilesystemEncoding(), 'чавес.jpg');
114124

115125
if (!mkdir($dirCyrillic)) {
116126
self::markTestIncomplete('Cant create cyrillic directory.');
@@ -231,9 +241,9 @@ public function testExtractEntryOverwrite()
231241
}
232242

233243

234-
public function testExtractEntryUnicode()
244+
public function testExtractEntryCyrillic()
235245
{
236-
$file = iconv('UTF-8', 'Windows-1251', 'чавес.jpg');
246+
$file = iconv('UTF-8', $this->getCurrentFilesystemEncoding(), 'чавес.jpg');
237247
$obj = new Archive7z($this->fixturesDir . '/test.7z', $this->cliPath);
238248
$obj->setOutputDirectory($this->tmpDir);
239249
$obj->extractEntry($file);
@@ -396,4 +406,25 @@ public function testRenameEntryPasswd()
396406
$resultDest = $obj->getEntry('test' . DIRECTORY_SEPARATOR . 'newTest.txt');
397407
self::assertInstanceOf('Archive7z\Entry', $resultDest);
398408
}
409+
410+
411+
public function testChangeSystemLocale()
412+
{
413+
$file = iconv('UTF-8', $this->getCurrentFilesystemEncoding(), 'чавес.jpg');
414+
$obj = new Archive7z($this->fixturesDir . '/test.7z', $this->cliPath);
415+
$obj->setChangeSystemLocale(true);
416+
$obj->setOutputDirectory($this->tmpDir);
417+
$obj->extractEntry($file);
418+
419+
self::assertFileExists($this->tmpDir . '/' . $file);
420+
}
421+
422+
public function testChangeSystemLocaleFail()
423+
{
424+
$new = new Archive7z($this->tmpDir . '/test.7z', $this->cliPath);
425+
$new->setChangeSystemLocale(true);
426+
$this->setExpectedException('Archive7z\Exception');
427+
$new->getContent('file.txt');
428+
}
429+
399430
}

src/Archive7z/Archive7z.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ class Archive7z
9090
/**
9191
* @var string
9292
*/
93-
protected $systemLocale = 'en_US.utf8';
93+
protected $systemLocaleNix = 'en_US.utf8';
94+
/**
95+
* @var string
96+
*/
97+
protected $systemLocaleWin = '65001';
9498

9599

96100
/**
@@ -532,10 +536,10 @@ public function isValid()
532536
*/
533537
protected function execute($cmd)
534538
{
535-
if (!$this->getChangeSystemLocale() || $this->isOsWin()) {
539+
if (!$this->getChangeSystemLocale()) {
536540
$out = $this->exec($cmd);
537541
} else {
538-
$out = $this->execLang($cmd);
542+
$out = $this->execLocale($cmd);
539543
}
540544

541545
return $out;
@@ -564,9 +568,13 @@ protected function exec($cmd)
564568
* @return array
565569
* @throws Exception
566570
*/
567-
protected function execLang($cmd)
571+
protected function execLocale($cmd)
568572
{
569-
return $this->exec('LANG=' . escapeshellarg($this->systemLocale) . ' ' . $cmd);
573+
if ($this->isOsWin()) {
574+
return $this->exec('chcp ' . escapeshellarg($this->systemLocaleWin) . ' & ' . $cmd);
575+
} else {
576+
return $this->exec('LANG=' . escapeshellarg($this->systemLocaleNix) . ' ' . $cmd);
577+
}
570578
}
571579

572580

0 commit comments

Comments
 (0)