Question

I have a PHP script which executes a 7-zip extract command. The extracted file name is different than the zipped file's name. Is there a switch in 7-zip command line to specify the newly extracted file's name?

How can I store the newly extracted file's name in a PHP variable?

if (endsWith($currentFile, '.zip'))
{
    $extractCMD = '7z e uploads\\' . $currentFile;
    exec($extractCMD);
}

I'm thinking I have to do another exec() for the most recently created file, and store that. But there must be an easier way.

Was it helpful?

Solution

You can use -o to specify an output directory, but this will add a layer of depth:

7z x -oMyDir some_file.zip

For example, if some_file would normally output to some_file/ it will now output to MyDir/some_file/.

If you just want to know the name of the top-level directory in the archive, that's a bit more tricky. You can get a list of files before extraction using the l command:

7z l some_file.zip

but then you have to analyze the file names to see if there's a top-level directory at all. There doesn't have to be.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top