Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

Implementation of zip file archive.

Copyright: 2008 Petr Skoda (http://skodak.org)
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 893 lines (30 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

zip_archive:: (24 methods):
  __construct()
  open()
  mangle_pathname()
  unmangle_pathname()
  close()
  get_stream()
  extract_to()
  get_info()
  list_files()
  is_system_file()
  count()
  estimated_count()
  add_file_from_pathname()
  add_file_from_string()
  add_directory()
  current()
  key()
  next()
  rewind()
  valid()
  init_namelookup()
  fix_utf8_flags()
  zip_get_central_end()
  zip_parse_file_header()


Class: zip_archive  - X-Ref

Zip file archive class.

__construct()   X-Ref
Create new zip_archive instance.


open($archivepathname, $mode=file_archive::CREATE, $encoding=null)   X-Ref
Open or create archive (depending on $mode).

param: string $archivepathname
param: int $mode OPEN, CREATE or OVERWRITE constant
param: string $encoding archive local paths encoding, empty means autodetect
return: bool success

mangle_pathname($localname)   X-Ref
Normalize $localname, always keep in utf-8 encoding.

param: string $localname name of file in utf-8 encoding
return: string normalised compressed file or directory name

unmangle_pathname($localname)   X-Ref
Tries to convert $localname into utf-8
please note that it may fail really badly.
The resulting file name is cleaned.

param: string $localname name (encoding is read from zip file or guessed)
return: string in utf-8

close()   X-Ref
Close archive, write changes to disk.

return: bool success

get_stream($index)   X-Ref
Returns file stream for reading of content.

param: int $index index of file
return: resource|bool file handle or false if error

extract_to($destination, $index)   X-Ref
Extract the archive contents to the given location.

param: string $destination Path to the location where to extract the files.
param: int $index Index of the archive entry.
return: bool true on success or false on failure

get_info($index)   X-Ref
Returns file information.

param: int $index index of file
return: stdClass|bool info object or false if error

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

return: array of file infos

is_system_file($fileinfo)   X-Ref
No description

count()   X-Ref
Returns number of files in archive.

return: int number of files

estimated_count()   X-Ref
Returns approximate number of files in archive. This may be a slight
overestimate.

return: int|bool Estimated number of files, or false if not opened

add_file_from_pathname($localname, $pathname)   X-Ref
Add file into archive.

param: string $localname name of file in archive
param: string $pathname location of file
return: bool success

add_file_from_string($localname, $contents)   X-Ref
Add content of string into archive.

param: string $localname name of file in archive
param: string $contents contents
return: bool success

add_directory($localname)   X-Ref
Add empty directory into archive.

param: string $localname name of file in archive
return: bool success

current()   X-Ref
Returns current file info.

return: stdClass

key()   X-Ref
Returns the index of current file.

return: int current file index

next()   X-Ref
Moves forward to next file.


rewind()   X-Ref
Rewinds back to the first file.


valid()   X-Ref
Did we reach the end?

return: bool

init_namelookup()   X-Ref
Create a map of file names used in zip archive.

return: void

fix_utf8_flags()   X-Ref
Add unicode flag to all files in archive.

NOTE: single disk archives only, no ZIP64 support.

return: bool success, modifies the file contents

zip_get_central_end($fp, $filesize)   X-Ref
Read end of central signature of ZIP file.

param: resource $fp
param: int $filesize
return: array|bool

zip_parse_file_header($data, $centralend, &$pos)   X-Ref
Parse file header.

param: string $data
param: array $centralend
param: int $pos (modified)
return: array|bool file info