Skip "root" directory when extracting using ZipArchive class? - php

I am attempting to use the ZipArchive class to unzip the contents of a zip containing a GitHub repository.
When you download a zip from GitHub, the zip contains a folder at the root of the zip named something like "project-2302392-20230"... Then within that folder are the actual contents of the repository.
Is it possible extract everything within that project folder without extracting the folder itself?

Here's one way to figure out the name of the unique top directory of the github zip archive.
use getArchiveComment() to get the ziparchive comment.
use strpos to strip away all characters except the first 7 and also add the git username and repo name like so '/USER-REPO-'.substr($comment, 0, 7);
You could also use regex, scandir, and a number of other ways.

Related

phar exclude directories when creating tar archive

I want to create a tar archive in a PHP script using the built-in PharData class.
I want the tar archive to represent a directory so I thought of using PharData::buildFromDirectory() to do that. Unfortunately the directory also is a git repository and has a .git folder in it which is much bigger than the directory itself.
So I need to remove that .git directory (ideally also the .idea directory...) from the archive because it would bloat it unnecessarily.
What I tried so far:
Using a regular expression to filter the directory out:
$archive->buildFromDirectory("..", "#^(?!.git).+#");
Which didn't work.
Using PharData::delete(), but unfortunately that seems to only delete a file and not a directory.
So, what is the best way to do what I want?
I've seen the following used to exclude git directories using a negative lookahead:
$phar->buildFromDirectory(__DIR__, '/^((?!\.git).)*$/');
The problem with your regex is that the full path of the file gets matched, not only the directory (basename). Thus you cannot filter out the .git directory itself.
Using a character class negation ([^.][^g][^i][^t]) does also not help because there are parts of the path that do match this regex, so the path matches anyway.
This means you can use positive matches only.
You could use Phar::buildFromIterator and then use RecursiveFilterIterator to filter out the files.
There you can define your own matching method that filters correctly.

PHP UnRar.exe into a specific directory

Ive just found this script and it is exactly what im looking for, However how can i tell it to extract the files into a specific folder?
Here is the script
<?php
$winRAR = '"C:\Program Files\WinRAR\UnRAR.exe"';
$file="test.rar";
$do ="$winRAR e $file";
exec("$winRAR /?");
print_r($aOut);
exec($do,$aOut);
print_r($aOut);
?>
Im going to be extracting multiple files so i would like it to extract each archive into a a folder with the same name as the archive. So if the rar was called "Test" i want it to extract to a folder called /test/ and extract the files there?
Many Thanks in advance!
I suggest you to use the following command:
$do ="$winRAR x -ad $file $destinationPath";
Where $destinationPath is a destination path.
The "x" command will keep directory structure stored in archive in contrast with "e" command, which extracts files without subdirectories.
The "-ad" command line switch tells unrar to extract archive foo.rar to the $destinationPath/foo directory.
Command line commands and switches are listed in WinRAR help file, in section "Command line mode".
I think what you are trying to do may be easier using PHP's RAR extension. http://www.php.net/manual/en/book.rar.php

ci zip encoding class not working properly

I want to crate a zip file and download it.
So my code is as follow;
Since i am using ci zip encoding class
my folder structure is like
c:/xampp/htdocs/gallery/print/oid_1/1.jpg,2.jpg,3.jpg
My code
$path = getcwd().'/print/oid_1/';
$this->zip->read_dir($path,FALSE);
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('oid_1.zip');
Here is my problem,
l C:\Users\instinctclassic\Downloads\Compressed\oid_1.zip: The archive is either in unknown format or damaged so what does this means -does it means it was mistaken while making zip file or it was mistaken while downloading.For downloading mistake i have downloaded the zip file many time enough to be sure.
2 I repaired the downloaded zip file(oid_1) and extract it but the extracted folder structure is not ignored before the oid_1 folder as said in ci zip encoding class tutorial
$this->zip->read_dir($path, FALSE);
//This will create a ZIP with the folder "directory" inside, then all sub-folders stored correctly inside that, but will not include the folders /path/to/your.
3 Assuming my folder oid_1 already exists somewhere on server.
I know this question is previously asked but mine exist all the problem from making zip file to extracting.So i am sorry for this.
Thanks
Try directory path like this:
$path = getcwd().'\print\oid_1\';
this way path will be proper, because getcwd() will return you c:\xampp\htdocs\gallery

PCLZip - Is it posttible to rename a folder in an archive before extraction?

I've a situation where I am downloading a zip and need to rename one of it's folders before I extract it. Is this possible using PCLZip?
You can simply remove all the folder locations from each file using this PCLZIP_OPT_REMOVE_ALL_PATH
Regards.

PHP Zip non-empty folder with all files and sub-folders

I need to zip non-empty folder with all files and sub-folders. I found lots of classes and functions, but all of them zip whole directory structure or something like that. But I need to zip only the content of given folder.
In advance thanks for answers

Categories