This is a simple Wrapper around the ZipArchive methods with some handy functions

Related tags

Miscellaneous Zipper
Overview

Note

I haven't updated this package in a long time except merging PRs. The last time I was using this package was with PHP5. I archived the repository for the reason that I am no longer working with PHP (we all have to move on sometimes) and have no time to take proper care of it anymore.

Feel free to read the code, to fork it or to use it in whatever way you want.

Update 25th February 2020

I have merged a PR that includes a security fixe to mitigate zip directory traversal attacks.
This package is still archived and should be swapped out with another package.
However, as long as security fixes will come in I see it as my basic obligation to update this package on demand.

Zipper

Build Status

This is a simple Wrapper around the ZipArchive methods with some handy functions.

Installation

  1. Add this package to the list of required packages, inside composer.json
  • for Laravel 5: "chumper/zipper": "1.0.x"
  • for Laravel 4: "chumper/zipper": "0.5.x"
  1. Run composer update

  2. Go to app/config/app.php

  • add to providers Chumper\Zipper\ZipperServiceProvider::class
  • add to aliases 'Zipper' => Chumper\Zipper\Zipper::class

You can now access Zipper with the Zipper alias.

Simple example

$files = glob('public/files/*');
Zipper::make('public/test.zip')->add($files)->close();
  • by default the package will create the test.zip in the project route folder but in the example above we changed it to project_route/public/.

Another example

$zipper = new \Chumper\Zipper\Zipper;

$zipper->make('test.zip')->folder('test')->add('composer.json');
$zipper->zip('test.zip')->folder('test')->add('composer.json','test');

$zipper->remove('composer.lock');

$zipper->folder('mySuperPackage')->add(
    array(
        'vendor',
        'composer.json'
    ),
);

$zipper->getFileContent('mySuperPackage/composer.json');

$zipper->make('test.zip')->extractTo('',array('mySuperPackage/composer.json'),Zipper::WHITELIST);

$zipper->close();

Note: Please be aware that you need to call ->close() at the end to write the zip file to disk.

You can easily chain most functions, except getFileContent, getStatus, close and extractTo which must come at the end of the chain.

The main reason I wrote this little package is the extractTo method since it allows you to be very flexible when extracting zips. So you can for example implement an update method which will just override the changed files.

Functions

make($pathToFile)

Create or Open a zip archive; if the file does not exists it will create a new one. It will return the Zipper instance so you can chain easily.

add($files/folder)

You can add an array of Files, or a Folder and all the files in that folder will then be added, so from the first example we could instead do something like $files = 'public/files/';.

addString($filename, $content)

add a single file to the zip by specifying a name and the content as strings.

remove($file/s)

removes a single file or an array of files from the zip.

folder($folder)

Specify a folder to 'add files to' or 'remove files from' from the zip, example

Zipper::make('test.zip')->folder('test')->add('composer.json');
Zipper::make('test.zip')->folder('test')->remove('composer.json');

listFiles($regexFilter = null)

Lists all files within archive (if no filter pattern is provided). Use $regexFilter parameter to filter files. See Pattern Syntax for regular expression syntax

NB: listFiles ignores folder set with folder function

Example: Return all files/folders ending/not ending with '.log' pattern (case insensitive). This will return matches in sub folders and their sub folders also

$logFiles = Zipper::make('test.zip')->listFiles('/\.log$/i'); 
$notLogFiles = Zipper::make('test.zip')->listFiles('/^(?!.*\.log).*$/i'); 

home()

Resets the folder pointer.

zip($fileName)

Uses the ZipRepository for file handling.

getFileContent($filePath)

get the content of a file in the zip. This will return the content or false.

getStatus()

get the opening status of the zip as integer.

close()

closes the zip and writes all changes.

extractTo($path)

Extracts the content of the zip archive to the specified location, for example

Zipper::make('test.zip')->folder('test')->extractTo('foo');

This will go into the folder test in the zip file and extract the content of that folder only to the folder foo, this is equal to using the Zipper::WHITELIST.

This command is really nice to get just a part of the zip file, you can also pass a 2nd & 3rd param to specify a single or an array of files that will be

NB: Php ZipArchive uses internally '/' as directory separator for files/folders in zip. So Windows users should not set whitelist/blacklist patterns with '' as it will not match anything

white listed

Zipper::WHITELIST

Zipper::make('test.zip')->extractTo('public', array('vendor'), Zipper::WHITELIST);

Which will extract the test.zip into the public folder but only files/folders starting with vendor prefix inside the zip will be extracted.

or black listed

Zipper::BLACKLIST Which will extract the test.zip into the public folder except files/folders starting with vendor prefix inside the zip will not be extracted.

Zipper::make('test.zip')->extractTo('public', array('vendor'), Zipper::BLACKLIST);

Zipper::EXACT_MATCH

Zipper::make('test.zip')
    ->folder('vendor')
    ->extractTo('public', array('composer', 'bin/phpunit'), Zipper::WHITELIST | Zipper::EXACT_MATCH);

Which will extract the test.zip into the public folder but only files/folders exact matching names. So this will:

  • extract file or folder named composer in folder named vendor inside zip to public resulting public/composer
  • extract file or folder named bin/phpunit in vendor/bin/phpunit folder inside zip to public resulting public/bin/phpunit

NB: extracting files/folder from zip without setting Zipper::EXACT_MATCH When zip has similar structure as below and only test.bat is given as whitelist/blacklist argument then extractTo would extract all those files and folders as they all start with given string

test.zip
 |- test.bat
 |- test.bat.~
 |- test.bat.dir/
    |- fileInSubFolder.log

extractMatchingRegex($path, $regex)

Extracts the content of the zip archive matching regular expression to the specified location. See Pattern Syntax for regular expression syntax.

Example: extract all files ending with .php from src folder and its sub folders.

Zipper::make('test.zip')->folder('src')->extractMatchingRegex($path, '/\.php$/i'); 

Example: extract all files except those ending with test.php from src folder and its sub folders.

Zipper::make('test.zip')->folder('src')->extractMatchingRegex($path, '/^(?!.*test\.php).*$/i'); 

Development

Maybe it is a good idea to add other compression functions like rar, phar or bzip2 etc... Everything is setup for that, if you want just fork and develop further.

If you need other functions or got errors, please leave an issue on github.

Comments
  • errors

    errors

    Zipper::getFileContent('public/test.zip') return NULL

    Zipper::make('public/test.zip')->folder('comp')->remove('composer.lock')->close() return NULL

    Zipper::make('public/test.zip')->extractTo('public/_new/', ['comp'], Zipper::BLACKLIST) Undefined class constant 'BLACKLIST'

    opened by ctf0 13
  • please create laravel 5 branch

    please create laravel 5 branch

    i will fix all the bugs in laravel 5 branch i have project that is in laravel 5 and i want to use this package

    it will be grate if you add me in contributors or i will create pull requests

    opened by thecotne 7
  • Packagist needs a version

    Packagist needs a version

    Hi

    Any chance you could start using versions, even if it is 0.1.0? I'd like to ensure that when I do a composer install when I push my project to different servers that I'm getting the same version. At the moment we can only install the dev-master, which means I can never be sure it's the same version of files on different servers.

    I don't know how hard it is to define version, so I don't know if this is a big task or not sorry.

    opened by benfreke 7
  • Laravel 5.4 support

    Laravel 5.4 support

    Hi,

    many thanks for providing this package. Unfortunately it doesn't work with Laravel 5.4, as an exception is thrown:

    Call to undefined method Illuminate\Foundation\Application::share()

    I see that there's already a PR, but it's not yet released due to failing tests on PHP 5.4 and PHP 5.5. Laravel 5.3+ requires PHP >=5.6.0, so in my opinion old dependencies could be removed?

    opened by marcoraddatz 6
  • More clarification please.

    More clarification please.

    there are a couple of methods i just don't get , basicly when to use any of the below

    addString($filename, $content)
    close()
    home()
    zip($fileName) // does exactly the same as make ,so is it for ?
    

    also i cant quite understand the main example ,do they all related to each other or each suppose to do something on its own ,am asking because they are using the same object so some lines doesn't make any sense.

    opened by ctf0 6
  • Issue with Laravel Facade

    Issue with Laravel Facade

    Hi Nils,

    I'm using barryvdh/laravel-ide-helper and get "Class \Chumper\Zipper\Facades\Zipper is not found.". It seems that there is something wrong with the L4 Facade.

    When I try to use your class like "Zipper::make('/tmp/test.zip')->folder('app/config')->add('app.php');" I get the following error:

    Unknown class passed as parameter (Zipper.php line 71): if (is_subclass_of($type, 'Chumper\Zipper\Repositories\RepositoryInterface'))

    Any idea? Thanks!

    opened by bart 6
  • runs as background process - no way to wait for it to be done

    runs as background process - no way to wait for it to be done

    I think this runs asynchronously in the background. In my script, I zip the newly created files before I email them, and I'd like there to be a way to wait for the process to complete.

    opened by iateadonut 4
  • Add folder + subdirectories in windows environment

    Add folder + subdirectories in windows environment

    I am having trouble adding a folder to a zip archive on a windows machine. It adds the files from within the folder, but does not recurse through to subdirectories...

    I think it may be to do with backslash / forward slash mixing, but I am not entirely sure.

    Any ideas?

    opened by plantwebdesign 4
  • Call to a member function addFile() on a non-object

    Call to a member function addFile() on a non-object

    Hello, I can use the package on local XAMPP, however, when I use it on the server, I am getting this error at line 365 (/vendor/chumper/zipper/src/Chumper/Zipper/Zipper.php)

    $this->repository->addFile($pathToAdd, $this->getInternalPath() . $file_name);

    how to fix this problem?

    opened by sil218 4
  • GetFileContent returns file not found

    GetFileContent returns file not found

    I'm reading an existing zip file

    $file = storage_path('pathToZip/myfile.zip');
    return Zipper::make($file)-listFiles (); // works fine [ "anotherfile.txt" ]
    return Zipper::make( $file)->getFileContent($file . '/anotherFile.txt'); // error file not found
    
    opened by yamenarahman 3
  • extractTo() extracts all files that start with given string

    extractTo() extracts all files that start with given string

    Currently public function extractTo($path, array $files = array(), $method = Zipper::BLACKLIST) parameter $files acts as a prefix for files to be blacklisted/whitelisted.

    So if your zip contains files

    " ... /folder/somefile.data /folder/somefile.data.log /folder/another.data /folder/another.data.log ... "

    $this->zipper->folder('folder')->extractTo(
            $toDirectory, 
            ['somefile.data', 'another.data'], 
            Zipper::WHITELIST
    );
    

    would extract .log files also.

    Maybe this filename matching should be strict or parameter to force exact matches only?

    opened by aldas 3
Releases(v1.0.3)
Owner
Nils Plaschke
Computer Scientist at @adobe in Hamburg
Nils Plaschke
PHP exercises from my course at ETEC and some of my own play-around with PHP

etec-php-exercises PHP exercises from my course at ETEC and some of my own play-around with PHP Translations: Português (BR) Projects Project Descript

Luis Felipe Santos do Nascimento 6 May 3, 2022
An opinionated extension package for Laravel Orchid to extend its table handling capabilities, and some further useful helper methods.

OrchidTables An opinionated extension package for Laravel Orchid to extend its table handling capabilities, and some further useful helper methods. In

null 25 Dec 22, 2022
A simple wrapper around vlucas' PHP dotenv library for Kirby CMS.

kirby-phpdotenv A simple wrapper around vlucas' PHP dotenv library for Kirby CMS. Why? I've been using .env in my Kirby projects for a while, but I go

Steve Jamesson 5 Feb 6, 2020
This project backports features found in the latest PHP versions and provides compatibility layers for some extensions and functions

This project backports features found in the latest PHP versions and provides compatibility layers for some extensions and functions. It is intended to be used when portability across PHP versions and extensions is desired.

Symfony 2.2k Dec 29, 2022
WordPlate is a wrapper around WordPress. It makes developers life easier. It is just like building any other WordPress website with themes and plugins. Just with sprinkles on top.

WordPlate is simply a wrapper around WordPress. It makes developers life easier. It is just like building any other WordPress website with themes and plugins. Just with sprinkles on top.

WordPlate 1.7k Dec 24, 2022
A wrapper around faker for factory muffin

Factory Muffin Faker 2.3 The goal of this package is to wrap Faker to make it super easy to use with Factory Muffin. Note that this library does not a

The League of Extraordinary Packages 36 Nov 29, 2022
An object oriented wrapper around PHP's built-in server.

Statix Server Requirements PHP 8 minumum Installation composer require statix/server Basic Usage To get started, ensure the vendor autoload script is

Statix PHP 113 Dec 27, 2022
A PHP wrapper around Libreoffice for converting documents from one format to another.

Document Converter A PHP wrapper around Libreoffice for converting documents from one format to another. For example: Microsoft Word to PDF OpenOffice

Lukas White 0 Jul 28, 2022
A wrapper around symplify/config-transformer used to update recipes and using easy coding standard for generating readable config files.

Symfony Recipes Yaml to PHP Converter This is a wrapper around the symplify/config-transformer used to convert Symfony core recipes which uses .yaml c

Alexander Schranz 3 Nov 24, 2022
Magento-Functions - A Resource of Magento Functions

Magento-Functions A Resource of Magento Functions Table of Contents Category Product User Cart Checkout General Account [Working w/ URL's] (#urls) Cat

Bryan Littlefield 28 Apr 19, 2021
Here is the top 100 PHP functions: it is the list of the most often used PHP native functions

Here is the top 100 PHP functions: it is the list of the most often used PHP native functions. If you are a PHP developer, you must know the Top 100 PHP Functions deeply.

Max Base 16 Dec 11, 2022
RMT is a handy tool to help releasing new version of your software

RMT - Release Management Tool RMT is a handy tool to help releasing new versions of your software. You can define the type of version generator you wa

Liip 442 Dec 8, 2022
⚓️ Easily test HTTP webhooks with this handy tool that displays requests instantly.

Webhook.site With Webhook.site, you instantly get a unique, random URL that you can use to test and debug Webhooks and HTTP requests, as well as to cr

Webhook.site 3.7k Jan 9, 2023
Dobren Dragojević 6 Jun 11, 2023
this is a simple website about news and it has some features

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

a14z 2 Dec 11, 2022
Simple, modern looking server status page with administration and some nice features, that can run even on shared webhosting

Simple, modern looking server status page with administration and some nice features, that can run even on shared webhosting

Server status project 363 Dec 28, 2022
This is a plugin for pocketmine-mp, when locking a player's items helps players not to lose items or throw things around causing server lag.

[] LockedItem| v1.0.0 Player's item lock Features Player's item lock Players aren't afraid of losing items For Devolopers You can access to LockedItem

JeroGamingYT 3 Jan 4, 2022
Makes indexing of your Magento store around x times faster! ‼️ Maintainers wanted!

FastIndexer This module has never been used in production. No more empty results in the frontend due to a long taking reindex process! Integrates seam

Cyrill Schumacher 79 Jul 10, 2022
⭐ It is an platform for people to help them get connected with the like minding folks around the globe.

Meetups It is an Platform for people to help them get connected with the like minded folks around the globe. Live on Web: Cick here ?? Meet and Connec

Hardik Kaushik 5 Apr 26, 2022