Skip to content

Commit

Permalink
Allow zip archiving recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
Benau committed Dec 7, 2019
1 parent b6583ec commit 5dbdcfb
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions include/FileSystem.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,33 @@ public static function deleteOldSubdirectories($dir, $max_age)
}
}

/**
* Add files or sub-folders to zip file recursively
* @param ZipArchive $zip
* @param string $in input files
* @param string $out output location in zip file
*/
private static function zipAdd($zip, $in, $out)
{
if (static::isDirectory($in))
{
$zip->addEmptyDir($out);
foreach (static::ls($in) as $file)
{
static::zipAdd($zip, $in . DS . $file, $out . DS . $file);
}
return;
}
if (!$zip->addFile($in, $out))
{
throw new FileSystemException("Can't add this file = '$out' to the archive");
}
if (!static::exists($in))
{
throw new FileSystemException("Can't add this file = '$out' as it doesn't exist");
}
}

/**
* Add a directory to a zip archive
*
Expand All @@ -573,19 +600,7 @@ public static function compressToArchive($directory, $filename)
// Find files to add to archive
foreach (static::ls($directory) as $file)
{
if (static::isDirectory($directory . $file))
{
continue;
}

if (!$zip->addFile($directory . $file, $file))
{
throw new FileSystemException("Can't add this file = '$file' to the archive");
}
if (!static::exists($directory . $file))
{
throw new FileSystemException("Can't add this file = '$file' as it doesn't exist");
}
static::zipAdd($zip, $directory . $file, $file);
}

if (!$zip->close())
Expand Down

0 comments on commit 5dbdcfb

Please sign in to comment.