Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

Implementation of .tar.gz packer. A limited subset of the .tar format is supported. This packer can open files that it wrote, but may not be able to open files from other sources, especially if they use extensions. There are restrictions on file length and character set of filenames.

Copyright: 2013 The Open University
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 902 lines (34 kb)
Included or required:0 times
Referenced: 3 times
Includes or requires: 0 files

Defines 3 classes

tgz_packer:: (13 methods):
  archive_to_storage()
  delete_existing_file_record()
  set_include_index()
  archive_to_pathname()
  write_tar_entry()
  calculate_checksum()
  list_files_path()
  list_files_stored()
  extract_to_pathname()
  extract_to_storage()
  list_files()
  is_tgz_file()
  has_required_extension()

tgz_packer_extract_to_pathname:: (4 methods):
  __construct()
  tgz_start_file()
  tgz_end_file()
  tgz_directory()

tgz_packer_extract_to_storage:: (4 methods):
  __construct()
  tgz_start_file()
  tgz_end_file()
  tgz_directory()


Class: tgz_packer  - X-Ref

Utility class - handles all packing/unpacking of .tar.gz files.

archive_to_storage(array $files, $contextid,$component, $filearea, $itemid, $filepath, $filename,$userid = null, $ignoreinvalidfiles = true, file_progress $progress = null)   X-Ref
Archive files and store the result in file storage.

Any existing file at that location will be overwritten.

param: array $files array from archive path => pathname or stored_file
param: int $contextid context ID
param: string $component component
param: string $filearea file area
param: int $itemid item ID
param: string $filepath file path
param: string $filename file name
param: int $userid user ID
param: bool $ignoreinvalidfiles true means ignore missing or invalid files, false means abort on any error
param: file_progress $progress Progress indicator callback or null if not required
return: stored_file|bool false if error stored_file instance if ok

delete_existing_file_record(file_storage $fs, array $filerecord)   X-Ref
Wrapper function useful for deleting an existing file (if present) just
before creating a new one.

param: file_storage $fs File storage
param: array $filerecord File record in same format used to create file

set_include_index($includeindex)   X-Ref
By default, the .tar file includes a .ARCHIVE_INDEX file as its first
entry. This makes list_files much faster and allows for better progress
reporting.

If you need to disable the inclusion of this file, use this function
before calling one of the archive_xx functions.

param: bool $includeindex If true, includes index

archive_to_pathname(array $files, $archivefile,$ignoreinvalidfiles=true, file_progress $progress = null)   X-Ref
Archive files and store the result in an OS file.

param: array $files array from archive path => pathname or stored_file
param: string $archivefile path to target zip file
param: bool $ignoreinvalidfiles true means ignore missing or invalid files, false means abort on any error
param: file_progress $progress Progress indicator callback or null if not required
return: bool true if file created, false if not

write_tar_entry($gz, $archivepath, $file, $size, $mtime, $content = null,file_progress $progress = null, $done = 0)   X-Ref
Writes a single tar file to the archive, including its header record and
then the file contents.

param: resource $gz Gzip file
param: string $archivepath Full path of file within archive
param: string|resource $file Full path of file on disk or file handle or null if none
param: int $size Size or 0 for directories
param: int|string $mtime Time or ? if unknown
param: string $content Actual content of file to write (null if using $filepath)
param: file_progress $progress Progress indicator or null if none
param: int $done Value for progress indicator
return: bool True if OK

calculate_checksum($str)   X-Ref
Calculates a checksum by summing all characters of the binary string
(treating them as unsigned numbers).

param: string $str Input string
return: int Checksum

list_files_path(array &$expandedfiles, $archivepath, $path,file_progress $progress = null, $done)   X-Ref
Based on an OS path, adds either that path (if it's a file) or
all its children (if it's a directory) into the list of files to
archive.

If a progress indicator is supplied and if this corresponds to a
directory, then it will be repeatedly called with the same values. This
allows the progress handler to respond in some way to avoid timeouts
if required.

param: array $expandedfiles List of all files to archive (output)
param: string $archivepath Current path within archive
param: string $path OS path on disk
param: file_progress $progress Progress indicator or null if none
param: int $done Value for progress indicator
return: bool True if successful

list_files_stored(array &$expandedfiles, $archivepath, stored_file $file)   X-Ref
Based on a stored_file objects, adds either that file (if it's a file) or
all its children (if it's a directory) into the list of files to
archive.

If a progress indicator is supplied and if this corresponds to a
directory, then it will be repeatedly called with the same values. This
allows the progress handler to respond in some way to avoid timeouts
if required.

param: array $expandedfiles List of all files to archive (output)
param: string $archivepath Current path within archive
param: stored_file $file File object

extract_to_pathname($archivefile, $pathname,array $onlyfiles = null, file_progress $progress = null, $returnbool = false)   X-Ref
Extract file to given file path (real OS filesystem), existing files are overwritten.

param: stored_file|string $archivefile full pathname of zip file or stored_file instance
param: string $pathname target directory
param: array $onlyfiles only extract files present in the array
param: file_progress $progress Progress indicator callback or null if not required
param: bool $returnbool Whether to return a basic true/false indicating error state, or full per-file error
return: array list of processed files (name=>true)

extract_to_storage($archivefile, $contextid,$component, $filearea, $itemid, $pathbase, $userid = null,file_progress $progress = null)   X-Ref
Extract file to given file path (real OS filesystem), existing files are overwritten.

param: string|stored_file $archivefile full pathname of zip file or stored_file instance
param: int $contextid context ID
param: string $component component
param: string $filearea file area
param: int $itemid item ID
param: string $pathbase file path
param: int $userid user ID
param: file_progress $progress Progress indicator callback or null if not required
return: array list of processed files (name=>true)

list_files($archivefile)   X-Ref
Returns array of info about all files in archive.

param: string|stored_file $archivefile
return: array of file infos

is_tgz_file($archivefile)   X-Ref
Checks whether a file appears to be a .tar.gz file.

param: string|stored_file $archivefile
return: bool True if file contains the gzip magic number

has_required_extension()   X-Ref
The zlib extension is required for this packer to work. This is a single
location for the code to check whether the extension is available.

return: bool True if the zlib extension is available OK

Class: tgz_packer_extract_to_pathname  - X-Ref

Handles extraction to pathname.

__construct($pathname, array $onlyfiles = null)   X-Ref
Constructor.

param: string $pathname target directory
param: array $onlyfiles only extract files present in the array

tgz_start_file($archivepath)   X-Ref


tgz_end_file($archivepath, $realpath)   X-Ref


tgz_directory($archivepath, $mtime)   X-Ref


Class: tgz_packer_extract_to_storage  - X-Ref

Handles extraction to file storage.

__construct($contextid, $component, $filearea, $itemid, $pathbase, $userid)   X-Ref
Constructor.

param: int $contextid Context id for files.
param: string $component Component name for files.
param: string $filearea File area for files.
param: int $itemid Item ID for files.
param: string $pathbase Base path for files (subfolders will go inside this).
param: int $userid User id for files or null if none.

tgz_start_file($archivepath)   X-Ref


tgz_end_file($archivepath, $realpath)   X-Ref


tgz_directory($archivepath, $mtime)   X-Ref