Coder Social home page Coder Social logo

madzipper's Introduction

Madzipper

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

Build Status

Installation

  1. madnest/madzipper can be installed by running:
composer require madnest/madzipper
  1. Optionally when using with Laravel, go to app/config/app.php
  • add to providers Madnest\Madzipper\MadzipperServiceProvider::class

You can now access Madzipper with the Madzipper alias.

Versions

Package Laravel PHP
v1.5 11.* ^8.2
v1.4 10.* ^8.1
v1.3 9.* ^8.0
v1.2 8.* ^8.0

Simple Laravel example by using Madzipper facade

$files = glob('public/files/*');
Madzipper::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 \Madnest\Madzipper\Madzipper;

$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('', ['mySuperPackage/composer.json'], Madzipper::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

Madzipper::make('test.zip')->folder('test')->add('composer.json');
Madzipper::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 = Madzipper::make('test.zip')->listFiles('/\.log$/i');
$notLogFiles = Madzipper::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

Madzipper::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 Madzipper::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

Madzipper::WHITELIST

Madzipper::make('test.zip')->extractTo('public', array('vendor'), Madzipper::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

Madzipper::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.

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

Madzipper::EXACT_MATCH

Madzipper::make('test.zip')
    ->folder('vendor')
    ->extractTo('public', array('composer', 'bin/phpunit'), Madzipper::WHITELIST | Madzipper::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 Madzipper::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.

Madzipper::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.

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

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

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.

Credits

Big thank you goes to @Chumper for creating the original package Chumper/Zipper

madzipper's People

Contributors

bbrunekreeft avatar laravel-shift avatar ndum avatar ninoskopac avatar scs-ben avatar sgarwood avatar snoopysecurity avatar theimerj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

madzipper's Issues

Laravel 6, supported?

Just did an upgrade on a project:

chumper/zipper v1.0.0 requires illuminate/filesystem 5.x

close() function in destructor of Madzipper causes 'Invalid or uninitialized Zip object' with PHP 8

Sample code to reproduce:

$zipper = new Madzipper;
$zipper->make($relativeFolder . $filenameZip)
               ->addString($filename, $qms_data)
               ->close();
$zipper = null; // close is also called via destructor of Madzipper

ZipRepository.php

    /**
     * Closes the archive and saves it
     */
    public function close()
    {
        @$this->archive->close();      //  <----  error surpressed 
    }

In PHP 8.0, the @ operator does not suppress certain types of errors that were silenced prior to PHP 8.0.

Until this is fixed, I don't call close() after adding the string but let de destructor close it.

Laravel 11

Any chance this is going to get Laravel 11 support? Thanks

Not a zip archive

Hi,

Is there a way to check if a file is valid zip archive?

$zipper = new Madzipper;
$zipper->make($filePath);

Exception: Error: Failed to open /private/var/folders/23/m470jpgs09z9jwq25xg96qw80000gn/T/phpfdvQjI! Error: ZipArchive::ER_NOZIP - Not a zip archive.

Incompatible with Laravel >= 6.1

Trying to install on a Laravel 6.8 but the composer requeriments dont allow it.

- madnest/madzipper v1.0.1 requires illuminate/support 5.8 - 6.0 -> satisfiable by laravel/framework[v6.0.4], illuminate/support[v5.8.0, v5.8.11, v5.8.12, v5.8.14, v5.8.15, v5.8.17, v5.8.18, v5.8.19, v5.8.2, v5.8.20, v5.8.22, v5.8.24, v5.8.27, v5.8.28, v5.8.29, v5.8.3, v5.8.30, v5.8.31, v5.8.32, v5.8.33, v5.8.34, v5.8.35, v5.8.36, v5.8.4, v5.8.8, v5.8.9, v6.0.0, v6.0.1, v6.0.2, v6.0.3, v6.0.4].

Error: Unknown named parameter $stats

The following parts of the code throw an error with php 8 because the implementation passes the named parameter $stats which is neither defined in the methods signature nor used.

Error: Unknown named parameter $stats

xqz/vendor/madnest/madzipper/src/Madnest/Madzipper/Repositories/ZipRepository.php:119
xyz/vendor/madnest/madzipper/src/Madnest/Madzipper/Madzipper.php:501

Madzipper:

    public function listFiles($regexFilter = null)
    {
        $filesList = [];
        if ($regexFilter) {
            $filter = function ($file) use (&$filesList, $regexFilter) {
                // push/pop an error handler here to to make sure no error/exception thrown if $expected is not a regex
                set_error_handler(function () {
                });
                $match = preg_match($regexFilter, $file);
                restore_error_handler();

                if ($match === 1) {
                    $filesList[] = $file;
                } elseif ($match === false) {
                    throw new \RuntimeException("regular expression match on '$file' failed with error. Please check if pattern is valid regular expression.");
                }
            };
        } else {
            $filter = function ($file) use (&$filesList) {
                $filesList[] = $file;
            };
        }
        $this->repository->each($filter);

        return $filesList;
    }

ZipRepositry:

    public function each($callback)
   {
       for ($i = 0; $i < $this->archive->numFiles; ++$i) {
           //skip if folder
           $stats = $this->archive->statIndex($i);
           if ($stats['size'] === 0 && $stats['crc'] === 0) {
               continue;
           }
           call_user_func_array($callback, [
               'file' => $this->archive->getNameIndex($i),
               'stats' => $this->archive->statIndex($i),
           ]);
       }
   }

Verison v1.2.2 doesn't support Laravel 8

Verison v1.2.2 doesn't support Laravel 8, which supposed to support up to Laravel 8 according to readme.

yarn composer require madnest/madzipper:v1.2.2
yarn run v1.22.19
warning ../../../../package.json: No license field
$ docker-compose exec web composer require madnest/madzipper:v1.2.2
./composer.json has been updated
Running composer update madnest/madzipper
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Root composer.json requires madnest/madzipper v1.2.2 -> satisfiable by madnest/madzipper[v1.2.2].
    - madnest/madzipper v1.2.2 requires illuminate/support ^9.0 -> found illuminate/support[v9.0.0-beta.1, ..., 9.x-dev] but these were not loaded, likely because it conflicts with another require.

Create a password protected zip

The ->setPassword() method is not setting the password for the archive you are making; it's just setting the password to use to extract files when reading a zip archive.

As noted in this comment https://www.php.net/manual/en/ziparchive.setpassword.php#122465 the correct way to create a password protected zip file is to use ->setEncryptionName('test.txt', ZipArchive::EM_AES_256, 'passw0rd'); on each file entry.

This is currently not possible on Madzipper because the repository is not giving public access to the ZipArchive instance.

Madzipper Reads Archive as Empty

I have a problem where Madzipper cannot read the files from an archive I create during a test, whereas when using native ZipArchive there is no issue.

I am currently using PHP 8.3 together with Laravel v.10.39 and the latest Madzipper.

Creates an empty directory:

        $zipper = \Madzipper::make($extractSourcePath);
        $zipper->extractTo($extractTargetPath);
        $zipper->close();

Creates directory as expected containing all the files from the archive:

        $zipArchive = new \ZipArchive;
        $zipArchive->open($extractSourcePath);
        $zipArchive->extractTo($extractTargetPath);
        $zipArchive->close();

It doesn't matter if I am just using extractTo or listFiles, it just cannot read the contents. listFiles will return [] accordingly.

The common test code is:

        $response = $this->get(route('batch.zip.download', [
            'task_files' => $inputTaskFiles->implode('id', ',')
        ]));

        $response->assertStatus(200);

        $response->assertDownload();

        $fileName = 'task_batch';
        $sourceFileName = "{$fileName}.zip";

        $this->getTestStorageDisk()->put(
            $sourceFileName,
            $response->streamedContent()
        );

        $extractSourcePath = $this->getTestStorageDisk()->path($sourceFileName);
        $extractTargetPath = $this->getTestStorageDisk()->path($fileName);

The controller code uses the ZipStream package:

  return response()->streamDownload(function () use ($request, $name) {
            $zip = new ZipStream(
                outputName: $name,
                // enable output of HTTP headers
                sendHttpHeaders: true
            );

           // Add files to archive

            $zip->finish();

            return $zip;
        }, $name, [
            'Content-Type' => 'application/zip',
        ]);

I don't think it even matters how the archive is created, we can assume it's there and not empty.

Maybe someone can help to find out why this happens.

v1.2 missing on packagist

Hi there,

First of all thanks for providing this package :)

I'm trying to install v1.2 in my Laravel 8 application, but I'm getting a composer error:

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Root composer.json requires madnest/madzipper 1.2, found madnest/madzipper[dev-master, v0.0.1, v1.0.0, ..., v1.3.x-dev] but it does not match the constraint.

Inconsistency on Tag 1.2.1

Hello guys,

I just realized that version 1.2.1 on Packagist says Madzipper is compatible with PHP ^7.3 when it accepts only PHP 7.4 or above.

As for what I've seen, the only issue that makes it to be incompatible with PHP 7.4 is the fact the class ZipRepository has a typed property called $open. PHP 7.3 does not accept these typed variables yet, but only PHP 7.4 or above.

public bool $open = false;

It could be great to fix this minor detail =).

Sincerely,
Felipe Pastana.

Drop Laravel requirement

I'd like to use this package as an alternative for the chumper/zipper package.
I'm not using Laravel, but since Laravel is required in composer.json, the complete Laravel Package is installed.

Is it possible to drop the Laravel requirement from composer.json, so I can use it in my own project?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.