Php zip command line

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.

PHP zip/tar/bz2 archives (de)compression library with commandline or extensions

License

alchemy-fr/Zippy

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 PHP library to read, create, and extract archives in various formats via command line utilities or PHP extensions

The only supported installation method is via Composer. Run the following command to require Zippy in your project:

composer require alchemy/zippy 

Zippy currently supports the following drivers and file formats:

All the following code samples assume that Zippy is loaded and available as $zippy . You need the following code (or variation of) to load Zippy:

List an archive’s contents:

// Open an archive $archive = $zippy->open('build.tar'); // Iterate through members foreach ($archive as $member) < echo "Archive contains $member" . PHP_EOL; >

Extract an archive to a specific directory:

// Open an archive $archive = $zippy->open('build.tar'); // Extract archive contents to `/tmp` $archive->extract('/tmp');
// Creates an archive.zip that contains a directory "folder" that contains // files contained in "/path/to/directory" recursively $archive = $zippy->create('archive.zip', array( 'folder' => '/path/to/directory' ), true);

Customize file and directory names inside archive

$archive = $zippy->create('archive.zip', array( 'folder' => '/path/to/directory', // will create a folder at root 'http://www.google.com/logo.jpg', // will create a logo.jpg file at root fopen('https://www.facebook.com/index.php'), // will create an index.php at root 'directory/image.jpg' => 'image.jpg', // will create a image.jpg in 'directory' folder ));

This project is licensed under the MIT license.

Источник

How to zip by exec command line through php

Solution 3: How about gzip? Solution 4: why not use the zip/unzip linux utilities? run ‘zip —help’ in a linux commandline for details Question: I have an application that allows users to download mulitple files from a S3 bucket through an EC2 instance in one zip folder. What I need is the ability to do a system command line zip of large files based on what the web user selects and then prompt them or force a download.

Читайте также:  On off buttons in html

How to zip by exec command line through php

I have a ubuntu server. How can I zip files through executing a command line to zip from my php script

I want to zip the file because the file sizes are too big and zipping it through php will cause more load on server.

If you don’t need compression I would suggest to look into tar instead of zip as it should be quicker and will result in one file same as zip but without CPU time taken to process the archive

exec, system, fork, — any of the commands can you help execute command line programs with varying levels of control (blocking, non-blocking)

$exec = `usr/bin/zip -v zipfile filetozip filetozip2 filetozip3`

The -v is for verbose mode — if you want to check whether the zip worked you can print that out or log it or whatever. Your files will be found in zipfile.zip in the directory from where you ran the command.

Php creating zips without path to files inside the zip, local name inside ZIP archive. This means that the first parameter is the path to the actual file in the filesystem and the second is the path & filename that the file …

Which is More Efficient: Zipping With a System Command or Using PHP ZipArchive?

I’ve recently been given a task that involves uploading a zip file, storing it in a database as a blog, and then extracting and presenting the contents of that zip file when the client requests it.

I’ve got two approaches for this task: Using the exec command to execute the zip command native to the Linux OS the web server is running on, or using the ZipArchive class that comes with PHP.

  • Which approach uses the least amount of memory?
  • Which approach offers the most flexibility? W
  • What are the major advantages of one approach over the other?

exec(‘zip’) is way faster for large/many files. The built-in routines are always slower(due to many library calls & overhead. The system zip may have the advantage of using highly optimized routines. As a plus to the exec method, is the ease to change output formats from zip to rar , or 7zip or bzip etc.

How to zip a whole folder using PHP, I have found here at stackoveflow some codes on how to ZIP a specific file, but how about a specific folder? Folder/ index.html picture.jpg important.txt inside in My Folder, there are files.

Anyone know of a good example of a PHP system command line zip

This is another question based on an answer that was given to me.

What I need is the ability to do a system command line zip of large files based on what the web user selects and then prompt them or force a download. Can this be done?

Читайте также:  Set cookie with curl php

This can definitely be done — in fact there are many different ways to do it. One way would be to use the PHP Zip Extension (http://www.php.net/manual/en/book.zip.php) to make the file archive. Then, to let the user download the file, you’ll need to set the headers of your download page, like so:

header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=$file"); header("Content-Type: application/zip"); header("Content-Transfer-Encoding: binary"); 

You may want to refer to phpMyAdmin’s implementation as an example of a PHP-only implementation. Download it at phpmyadmin.net and see libraries/zip.lib.php .

why not use the zip/unzip linux utilities? run ‘zip —help’ in a linux commandline for details

How to delete a file using PHP, To delete a file by using PHP is very easy. Deleting a file means completely erase a file from a directory so that the file is no longer exist. PHP has an unlink() function that allows to delete a file.

Downloading a zip file from a folder on my web server

I have an application that allows users to download mulitple files from a S3 bucket through an EC2 instance in one zip folder.

The process works fine, however, I would like the system to create a folder for each user when they want to download files, and then the files are downloaded to this folder and then put in a zip folder, within the user’s folder, and then this zip folder is downloaded.

The files are downloading to this folder fine, and they are also zipping correctly, but my problem is that it is not downloading the zip file from the user’s folder, and the download is showing as an empty zip file.

Below is my ‘download all’ code:

mkdir($userId); $allName = $vshort . "v" . $number . "_All.zip"; $allName = str_replace(" ", "_", $allName); if (isset($_POST['downloadAll'])) < $zip = new ZipArchive(); $zip->open("/", ZipArchive::CREATE); $s3->registerStreamWrapper(); foreach ($items as $item) < $fid = $item->item_file_id; $file = ItemFile::locateId($fid); $a = $file[0]->item_file_type; $b = $file[0]->item_file_app; $c = $file[0]->item_file_name; $fileName = $a . "/" . $b . "/" . $c; $download = $s3->getCommand('GetObject', [ 'Bucket' => AWS_BUCKET, 'Key' => $fileName ]); $streamFile = $c; $user = User::locate($_SESSION['email']); $uid = $user->user_id; $cid = $user->user_company_id; $history = new History(); $history->insert($fid, $uid, $cid); $req = $s3->createPresignedRequest($download, '+1 minutes'); $link = (string)$req->getUri(); $stream = file_get_contents($link); header($_SERVER["SERVER_PROTOCOL"] . " 200 OK"); header("Cache-Control: public"); // needed for internet explorer header("Content-Type: application/'$a'"); header("Content-Disposition: attachment; filename='$c'"); file_put_contents($streamFile, $stream); $zip->addFile($streamFile); $downArray[] = $streamFile; > $zip->close(); $dLink = "https://mywebsite/" . $userId . "/" . $allName; $size = filesize($allName); header("Content-type: application/zip"); header("Content-Disposition: attachment; filename=$allName"); header("Content-length: " . $size); header("Expires: 0"); ob_end_clean(); stream_context_set_default([ 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false, ] ]); readfile($dLink); unlink($allName); foreach ($downArray as $down) < unlink($down); >> 

I have a feeling it is something to do with my headers but I’m not sure, any help will be greatly appreciated.

NOTE: I will be deleting the folder once the user has exited the page.

Below is what I am seeing on the web server

The folder ‘6’ is the user id for the user in question

You appear to be calling readfile() on your download link ($dLink), which would serve the file throught HTTP. You probably meant to call readfile on $allName, the filesystem path of the same file. My guess is that your http request to $dLink is failing somehow. Also, you mkdir($userId), but never use that directory.

Читайте также:  Python для новичков хабр

Apart from that, it seems your $allName variable contains the basename of the zipfile (i.e ‘myzip-v-20_ALL.zip’). I would recommend opening your zipfile to a full path to your data directory, so you keep filesystem and http paths separate.

$allNameFilesystem = "/"; $allNameHttp = "https://mywebsite/"; // . $zip->open($allNameFilesystem, ZipArchive::CREATE); // . // The download link is $allNameHttp 

That is, if you actually need the http path. In this case, if you change readfile to use the filesystem path, you really don’t use the http path anymore in your code.

Php — Creating zip or tar.gz archive without exec, Is there is any safe way to create a zip archive, or tar.gz from php without using exec command ? thanks

Источник

nmcgann / phpzip.php

PHP Command line zip utility. Simply zips a directory structure into an archive. No bells or whistles.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

/*
* Command line zip equivalent — very simple version that just zips a directory structure completely.
* No options!
* Useful when it is needed to build e.g. a WordPress plugin zip archive, but the dev environment
* doesn’t have zip — i.e. MINGW64 as bundled with Git — has unzip, but not zip.
*/
date_default_timezone_set( ‘UTC’ );
error_reporting ( E_ALL );
if ( $ argc < 3 )
echo » Usage: php phpzip archive_name directory_to_zip \n»;
exit( 1 );
>
if (!extension_loaded( ‘zip’ ))
echo » Error: PHP Zip extension not loaded, cannot continue. \n»;
exit( 1 );
>
//function to recursively add files and folders to zip
function folder_to_zip ( $ folder , & $ zip_obj )
if (is_null( $ zip_obj ))
throw new Exception (» Error: No Zip class object provided. «);
>
// ensure slash termination
$ folder = rtrim( $ folder , ‘/’ ) . ‘/’ ;
// iterate through files and directories
$ handle = @opendir( $ folder );
if ( $ handle === false )
throw new Exception (» Error: Cannot open ‘ $ folder ‘. «);
>
while ( $ f = readdir( $ handle ))
if ( $ f != » . » && $ f != » .. «)
if (is_file( $ folder . $ f ))
$ res = @ $ zip_obj -> addFile ( $ folder . $ f );
if ( $ res === false )
throw new Exception (» Error: Failed to write file to archive. «);
>
> elseif (is_dir( $ folder . $ f ))
// if dir, create a new dir in the zip
$ res = @ $ zip_obj -> addEmptyDir ( $ folder . $ f );
if ( $ res === false )
throw new Exception (» Error: Failed to create directory in archive. «);
>
// . and call the function again recursively
folder_to_zip( $ folder . $ f , $ zip_obj );
>
>
>
closedir( $ handle );
>
$ z = new ZipArchive ();
$ res = @ $ z -> open ( $ argv [ 1 ], ZIPARCHIVE :: OVERWRITE );
if ( $ res === false )
echo (» Error: Failed to open zip archive to write. \n»);
exit( 1 );
>
try
folder_to_zip( $ argv [ 2 ], $ z );
$ z -> close ();
> catch ( Exception $ ex )
echo $ ex -> getMessage ().»\n»;
$ z -> close ();
exit( 1 );
>
//all good
exit( 0 );
/* end */

Источник

Оцените статью