Php program to unzip file

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

A better PHP zipping/unzipping class

License

alexcorvi/php-zip

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

A better PHP zipping/unzipping class.

When building a CMS that is supposed to be redistributable and functioning on a range of hosting solutions (shared, VPS, dedicated) a problem often occurs in zipping and unzipping files. since not all PHP installation have the zipArchive class exists.

Case: Duplicator plugin for WordPress

For a number of clients, a problem seemed often occurring due to missing zipArchive class:

However, although the plugin itself is was distributed as a zip file, the WordPress code was able to unzip and install it. This is because the WordPress core code will check if the zipArchive class exists, and if it does not, it will use the PclZip class which can be included as a PHP file and considered as an alternative for zipArchive .

In this class, I tried to solve this problem in the same approach that the WordPress core team did; by checking if the class zipArchive exists or not, and using PclZip if it does not.

This does NOT solve missing the zlib extension which is a requirement even for the PclZip class. However, missing the zlib extension is less common than just missing the zipArchive class.

This class also provides a very simple and intuitive way of dealing with zip files.

To install this class, simply upload the src folder contents to your server and include the Zip.php file.

Note: To install in CodeIgniter create a folder called zip in your in application/libraries/ and put the src folder contents inside of it, then load it using the library loader $this->load->library(«zip/zip»); .

Note: When I wrote this class, I intended to use it in a multi-line fashion (You’ll see what I mean). However, I decided later that I should make a shorthand for this, so it would be usable in a one-line style.

Adding new files to the zip / Creating new zip file:

require_once "path/to/src/zip.php"; $zip = new Zip(); $zip->zip_start("path/to/new/or/old/zip_file.zip"); $zip->zip_add("path/to/example.png"); // adding a file $zip->zip_add(array("path/to/example1.png","path/to/example2.png")); // adding two files as an array $zip->zip_add(array("path/to/example1.png","path/to/directory/")); // adding one file and one directory $zip->zip_end();
$this->load->library("zip/zip"); $this->zip->zip_start("path/to/new/or/old/zip_file.zip"); $this->zip->zip_add("path/to/example.png"); // adding a file $this->zip->zip_add(array("path/to/example1.png","path/to/example2.png")); // adding two files as an array $this->zip->zip_add(array("path/to/example1.png","path/to/directory/")); // adding one file and one directory $this->zip->zip_end();
require_once "path/to/src/zip.php"; $zip = new Zip(); $zip->zip_files(array("path/to/new/or/old/zip_file.zip","path/to/directory/"),"path/to/zip/file.zip");
$this->load->library("zip/zip"); $this->zip->zip_files(array("path/to/new/or/old/zip_file.zip","path/to/directory/"),"path/to/zip/file.zip");

Extracting (unzipping) files:

require_once "path/to/src/zip.php"; $zip = new Zip(); $zip->unzip_file("path/to/new/or/old/zip_file.zip"); $zip->unzip_to("path/to/containing/directory/");
$this->load->library("zip/zip"); $this->zip->unzip_file("path/to/new/or/old/zip_file.zip"); $this->zip->unzip_to("path/to/containing/directory/");
require_once "path/to/src/zip.php"; $zip = new Zip(); $zip->unzip_file("path/to/new/or/old/zip_file.zip","path/to/containing/directory/");
$this->load->library("zip/zip"); $this->zip->unzip_file("path/to/new/or/old/zip_file.zip","path/to/containing/directory/");

Note: The library will throw a new exception in case of error and will return true in case of success.

  • This approach of solution was inspired by WordPress.
  • PHPConcept for creating PclZip , an awesome alternative for zipArchive .

License: The MIT License (MIT)

Copyright (c) 2017 Alex Corvi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the «Software»), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED «AS IS», WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

A better PHP zipping/unzipping class

Источник

How to Extract the Zip file with PHP

You don’t need to require any other extra plugin for working with Zip files.

PHP has a ZipArchive class that allows us to create a zip file or extract the existing file.

ZipArchive class extractTo() method is used to extract the zip file that takes the destination absolute path as an argument.

How to extract the Zip file with PHP

Contents

1. On the same directory

I am assuming the Zip file is stored in the project root directory.

Create an object of the ZipArchive class and open the given zip file using $zip->open() method.

If it returns TRUE then extract the file to the specified path ($path) with extractTo() method by passing path value as an argument in it.

Here, I am extracting the file in project files directory.

Completed Code

// Get Project path define(‘_PATH’, dirname(__FILE__)); // Zip file name $filename = ‘zipfile.zip’; $zip = new ZipArchive; $res = $zip->open($filename); if ($res === TRUE) < // Unzip path $path = _PATH."/files/"; // Extract file $zip->extractTo($path); $zip->close(); echo ‘Unzip!’; > else

2. Specific file

With file element, you can select the zip file that you want to extract.

If a selected file is valid then pass $_FILES[‘file’][‘tmp_name’] to open() method and extract it to specified path using extractTo() method.

Completed Code

open($tmp_name); if ($res === TRUE) < // Unzip path $path = _PATH."/files/"; // Extract file $zip->extractTo($path); $zip->close(); echo 'Unzip!'; > else < echo 'failed!'; >>else < echo 'Invalid file'; >> ?> 

3. Conclusion

Using the above PHP script you can extract the existing zip files at the specified location. For this, you need to pass the absolute path in extractTo() method of ZipArchive Class.

If you found this tutorial helpful then don’t forget to share.

Источник

How to Zip/Unzip files using PHP

Compressing files while transferring from one location to other is always a good option. In case of web experience, compressing files means reducing the file size by nice margin resulting in low bandwidth consumption as well as faster download and upload speeds.

However, you might find it tedious to zip and unzip your files manually. Don’t worry, we are here with a simple PHP script that will allow you to zip and unzip files simply executing the script.

We will be using PHP ZipArchive class for this purpose that has a lot of properties and methods which can help you compress and decompress all your files.

Initial Setup :

For the demo of zip and unzip files, prepare a folder/file structure as follows with file write permission in directories “compressed” and “uncompressed”

files” is the folder which will contain files that we will be compressing to zip

compressed” is the folder which will contain the zipped output

uncompressed” is the folder which will contain the unzipped files

zip.php” and “unzip.php” are the PHP scripts for compressing and uncompressing

Source Code :

open('compressed/images.zip', ZipArchive::CREATE); $zip->addFile('files/1.jpg', '1.jpg'); $zip->addFile('files/2.jpg', '2.jpg'); $zip->addFile('files/3.jpg', '3.jpg'); $zip->addFile('files/4.jpg', '4.jpg'); $zip->close();
open('compressed/images.zip', ZipArchive::CREATE); $zip->extractTo('uncompressed/'); $zip->close();

Follow this video for complete guidance :

Источник

Читайте также:  Css a prevent click
Оцените статью