Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
Mubashar Ahmad committed Nov 8, 2022
1 parent 9ab79b9 commit 8bdcb51
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 90 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ use Zip;
// add directory (only its content)
$zip->add('/path/to/my/directory', true);

```

- Add content to zip:

```php
$zip->addFromString('file name with extension', 'content of file');

$zip->addFromString('filename.txt', $file_content);
$zip->addFromString('folder/file1.txt', $file_content);
$zip->addFromString('folder/file2.txt', $file_content);


```

- Add multiple files/directories to zip:
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"merge"
],
"require": {
"php": "^7.4 || ^8.0",
"illuminate/support": "^6.0|^7.0|^8.0|^9.0",
"php": ">=7.4",
"illuminate/support": "^7.0|^8.0|^9.0",
"ext-zip": "*"
},
"require-dev": {
Expand All @@ -49,7 +49,7 @@
"ZanySoft\\Zip\\ZipServiceProvider"
],
"aliases": {
"Zip": "ZanySoft\\Zip\\ZipFacade"
"Zip": "ZanySoft\\Zip\\Facades\\Zip"
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/Facades/Zip.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace ZanySoft\Zip\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @method static \ZanySoft\Zip\Zip open($zip_file)
* @method static \ZanySoft\Zip\Zip create($zip_file, $overwrite = false)
* @method static \ZanySoft\Zip\Zip check($zip_file)
*/
class Zip extends Facade
{
protected static function getFacadeAccessor()
{
return 'zanysoft.zip';
}
}
91 changes: 54 additions & 37 deletions src/Zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ZanySoft\Zip;

use Exception;
use Illuminate\Support\Str;
use ZipArchive;

/**
Expand Down Expand Up @@ -42,7 +43,7 @@ class Zip
/**
* ZipArchive internal pointer
*
* @var object
* @var \ZipArchive
*/
private $zip_archive = null;

Expand Down Expand Up @@ -105,13 +106,11 @@ class Zip
* @param string $zip_file ZIP file name
*
*/
public function __construct($zip_file)
public function __construct($zip_file = null)
{
if (empty($zip_file)) {
throw new \Exception(self::getStatus(ZipArchive::ER_NOENT));
if ($zip_file) {
$this->open($zip_file);
}

$this->zip_file = $zip_file;
}

/**
Expand All @@ -121,17 +120,15 @@ public function __construct($zip_file)
*
* @return Zip
*/
public static function open($zip_file)
public function open(string $zip_file)
{
try {
$zip = new Zip($zip_file);

$zip->setArchive(self::openZipFile($zip_file));
$this->setArchive(self::openZipFile($zip_file));
} catch (\Exception $ze) {
throw $ze;
}

return $zip;
return $this;
}

/**
Expand All @@ -141,14 +138,14 @@ public static function open($zip_file)
*
* @return bool
*/
public static function check($zip_file)
public function check($zip_file): bool
{
try {
$zip = self::openZipFile($zip_file, ZipArchive::CHECKCONS);

$zip->close();
} catch (Exception $ze) {
throw $ze;
return false;
}

return true;
Expand All @@ -162,27 +159,29 @@ public static function check($zip_file)
*
* @return Zip
*/
public static function create($zip_file, $overwrite = false)
public function create(string $zip_file, bool $overwrite = false)
{
if ($overwrite and !$this->check($zip_file)) {
$overwrite = false;
}

$overwrite = filter_var($overwrite, FILTER_VALIDATE_BOOLEAN, [
'options' => [
'default' => false
]
]);

try {
$zip = new Zip($zip_file);

if ($overwrite) {
$zip->setArchive(self::openZipFile($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE));
$this->setArchive(self::openZipFile($zip_file, ZipArchive::OVERWRITE));
} else {
$zip->setArchive(self::openZipFile($zip_file, ZipArchive::CREATE));
$this->setArchive(self::openZipFile($zip_file, ZipArchive::CREATE));
}
} catch (Exception $ze) {
throw $ze;
}

return $zip;
return $this;
}

/**
Expand Down Expand Up @@ -246,13 +245,15 @@ final public function getPassword()
*
* @return Zip
*/
final public function setPath($path)
public function setPath(string $path)
{
$path = rtrim(str_replace('\\', '/', $path), '/') . '/';

if (!file_exists($path)) {
throw new Exception('Not existent path');
}

$this->path = $path[strlen($path) - 1] == '/' ? $path : $path . '/';
$this->path = $path;

return $this;
}
Expand Down Expand Up @@ -429,33 +430,45 @@ public function extract($destination, $files = null)
return true;
}

/**
* Create file form content and add to zip archive
*
* @param string $name File name with extension
* @param string $string File content
* @return void
*/
public function addFromString(string $name, string $string)
{
$this->zip_archive->addFromString($name, $string);
}

/**
* Add files to zip archive
*
* @param mixed $file_name_or_array filename to add or an array of filenames
* @param bool $flatten_root_folder in case of directory, specify if root folder should be flatten or not
* @param mixed $file_path file path to add or an array of files path
* @param bool $flatroot in case of directory, specify if root folder should be flatten or not
*
* @return Zip
*/
public function add($file_name_or_array, $flatten_root_folder = false)
public function add($file_path, $flatroot = false)
{
if (empty($file_name_or_array)) {
if (empty($file_path)) {
throw new Exception(self::getStatus(ZipArchive::ER_NOENT));
}

$flatten_root_folder = filter_var($flatten_root_folder, FILTER_VALIDATE_BOOLEAN, [
$flatroot = filter_var($flatroot, FILTER_VALIDATE_BOOLEAN, [
'options' => [
'default' => false
]
]);

try {
if (is_array($file_name_or_array)) {
foreach ($file_name_or_array as $file_name) {
$this->addItem($file_name, $flatten_root_folder);
if (is_array($file_path)) {
foreach ($file_path as $file) {
$this->addItem($file, $flatroot);
}
} else {
$this->addItem($file_name_or_array, $flatten_root_folder);
$this->addItem($file_path, $flatroot);
}
} catch (Exception $ze) {
throw $ze;
Expand All @@ -467,23 +480,23 @@ public function add($file_name_or_array, $flatten_root_folder = false)
/**
* Delete files from zip archive
*
* @param mixed $file_name_or_array filename to delete or an array of filenames
* @param mixed $filename filename to delete or an array of filenames
*
* @return Zip
*/
public function delete($file_name_or_array)
public function delete($filename)
{
if (empty($file_name_or_array)) {
if (empty($filename)) {
throw new Exception(self::getStatus(ZipArchive::ER_NOENT));
}

try {
if (is_array($file_name_or_array)) {
foreach ($file_name_or_array as $file_name) {
if (is_array($filename)) {
foreach ($filename as $file_name) {
$this->deleteItem($file_name);
}
} else {
$this->deleteItem($file_name_or_array);
$this->deleteItem($filename);
}
} catch (Exception $ze) {
throw $ze;
Expand Down Expand Up @@ -549,7 +562,11 @@ private function getArchiveFiles()
*/
private function addItem(string $file, bool $flatroot = false, string $base = null): void
{
$file = is_null($this->path) ? $file : $this->path . $file;
//$file = is_null($this->path) ? $file : $this->path . $file;

if ($this->path && !Str::startsWith($file, $this->path)) {
$file = $this->path . $file;
}

$real_file = str_replace('\\', '/', realpath($file));

Expand Down
14 changes: 0 additions & 14 deletions src/ZipFacade.php

This file was deleted.

5 changes: 2 additions & 3 deletions src/ZipManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace ZanySoft\Zip;

use \ZanySoft\Zip\Zip;
use \Exception;
use Exception;

/**
* Multiple ZipArchive manager
Expand Down Expand Up @@ -227,7 +226,7 @@ public function merge($output_zip_file, $separate = true)
try {
$this->extract($temporary_folder, $separate, null);

$zip = Zip::create($output_zip_file);
$zip = \ZanySoft\Zip\Facades\Zip::create($output_zip_file);

$zip->add($temporary_folder, true)->close();

Expand Down
36 changes: 4 additions & 32 deletions src/ZipServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,18 @@ class ZipServiceProvider extends ServiceProvider
*/
public function register()
{
$this->registerCpanelService();

/*if ($this->app->runningInConsole()) {
$this->registerResources();
}*/
$this->registerZipService();
}

/**
* Register currency provider.
*
* @return void
*/
public function registerCpanelService()
public function registerZipService()
{
$this->app->singleton('zip', function ($app) {
return new Zip($app);
$this->app->singleton('zanysoft.zip', function ($app) {
return new Zip();
});
}

/**
* Register currency resources.
*
* @return void
*/
public function registerResources()
{
if ($this->isLumen() === false) {
$this->publishes([
__DIR__ . '/../config/zip.php' => config_path('zip.php'),
], 'config');
}
}

/**
* Check if package is running under Lumen app
*
* @return bool
*/
protected function isLumen()
{
return str_contains($this->app->version(), 'Lumen') === true;
}
}
2 changes: 1 addition & 1 deletion tests/ZipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


use Orchestra\Testbench\TestCase;
use ZanySoft\Zip\Zip;
use ZanySoft\Zip\Facades\Zip;

class ZipTest extends TestCase
{
Expand Down

0 comments on commit 8bdcb51

Please sign in to comment.