PHP RAR Library: Create, manipulate and extract RAR archives

Recommend this page to a friend!
  Info   View files Example   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum (5)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2020-09-05 (14 hours ago) RSS 2.0 feedStarStarStar 59%Total: 745 This week: 2All time: 4,441 This week: 143Up
Version License PHP version Categories
rar-archiver 1.0.3BSD License5.5.11PHP 5, Files and Folders, Compression
Description Author

This class can create, manipulate and extract RAR archives.

It can create new archives in RAR format by adding files from directories, existing files or data strings.

The class can also check if a given file is a valid RAR archive, list its file contents, extract the files to a given directory, rename or delete files from the archive.

The RAR archive files are built or extracted using pure PHP code without resorting to external programs.

Innovation Award
PHP Programming Innovation award nominee
June 2015
Number 3


Prize: SourceGuarding PHP encoder tool
RAR is a popular format for packing files in compressed file format.

This class provides a pure PHP implementation to create and manipulate RAR archives, so you do not rely on the availability of the rar external command.

Manuel Lemos
Picture of Dmitry Mamontov
  Performance   Level  
Name: Dmitry Mamontov <contact>
Classes: 16 packages by
Country: Russian Federation Russian Federation
Innovation award
Innovation award
Nominee: 6x

Details

Latest Stable Version License

In the process version 2.0.

RarArchiver

Class for working with archives RAR. It allows you to add files to the directory, as well as extract, delete, rename, and get content.

Requirements

  • PHP version ~5.5.11

Installation

1) Install composer

2) Follow in the project folder:

composer require dmamontov/rararchiver ~1.0.0

In config composer.json your project will be added to the library dmamontov/rararchiver, who settled in the folder vendor/. In the absence of a config file or folder with vendors they will be created.

If before your project is not used composer, connect the startup file vendors. To do this, enter the code in the project:

require 'path/to/vendor/autoload.php';

Available methods

RarArchiver::__construct

Description

void RarArchiver::__construct ( string $file [, $flag = 0] )

It creates or opens a file and initializes it.

Parameters

  • `file` - The path to archive.
  • `flag` - An optional parameter. * `RarArchiver::CREATE` - It creates the archive if it is not found. * `RarArchiver::REPLACE` - Overwrites the archive if it is found, otherwise create it.

Examples

$rar = new RarArchiver('example.rar', RarArchiver::CREATE);

RarArchiver::isRar

Description

boolean RarArchiver::isRar ( void )

Checks whether the file archive RAR.

Return Values

Returns TRUE on success or FALSE on failure.

Examples

$rar = new RarArchiver('example.rar', RarArchiver::CREATE);

if ($rar->isRar()) {
    echo 'ok';
} else {
	echo 'failed';
}

RarArchiver::getFileList

Description

array RarArchiver::getFileList ( void )

Gets a list of files in the archive.

Return Values

Returns the filled array on success, or an empty array on failure.

Examples

$rar = new RarArchiver('example.rar', RarArchiver::CREATE);

if (count($rar->getFileList()) > 0) {
    echo 'ok';
} else {
	echo 'failed';
}

RarArchiver::addEmptyDir

Description

boolean RarArchiver::addEmptyDir ( string $dirname )

Adds an empty directory in the archive.

Parameters

  • `dirname` - The directory to add.

Return Values

Returns TRUE on success or FALSE on failure.

Examples

$rar = new RarArchiver('example.rar', RarArchiver::CREATE);

if ($rar->addEmptyDir('newEmptyDirectory')) {
    echo 'Created a new root directory';
} else {
	echo 'Could not create the directory';
}

RarArchiver::addFile

Description

boolean RarArchiver::addFile ( string $filename [, string $localname = ''] )

Adds a file to a RAR archive from a given path.

Parameters

  • `filename` - The path to the file to add.
  • `localname` - If supplied, this is the local name inside the RAR archive that will override the `filename`.

Return Values

Returns TRUE on success or FALSE on failure.

Examples

$rar = new RarArchiver('example.rar', RarArchiver::CREATE);

if ($rar->addFile('/path/to/index.txt', 'newname.txt')) {
    echo 'ok';
} else {
	echo 'failed';
}

RarArchiver::addFromString

Description

boolean RarArchiver::addFromString ( string $localname , string $contents )

Add a file to a RAR archive using its contents.

Parameters

  • `localname` - The name of the entry to create.
  • `contents` - The contents to use to create the entry.

Return Values

Returns TRUE on success or FALSE on failure.

Examples

$rar = new RarArchiver('example.rar', RarArchiver::CREATE);

if ($rar->addFromString('newname.txt', 'file content goes here')) {
    echo 'ok';
} else {
	echo 'failed';
}

RarArchiver::buildFromDirectory

Description

void RarArchiver::buildFromDirectory ( string $path [, string $regex ] )

Populate a RAR archive from directory contents. The optional second parameter is a regular expression (pcre) that is used to exclude files.

Parameters

  • `path` - The full or relative path to the directory that contains all files to add to the archive.
  • `regex` - An optional pcre regular expression that is used to filter the list of files. Only file paths matching the regular expression will be included in the archive.

Examples

$rar = new RarArchiver('example.rar', RarArchiver::CREATE);

$rar->buildFromDirectory(dirname(__FILE__) . '/project');
// or
$rar->buildFromDirectory(dirname(__FILE__) . '/project', '/\.php$/');

RarArchiver::deleteIndex

Description

boolean RarArchiver::deleteIndex ( int $index )

Delete an entry in the archive using its index.

Parameters

  • `index` - Index of the entry to delete.

Return Values

Returns TRUE on success or FALSE on failure.

Examples

$rar = new RarArchiver('example.rar', RarArchiver::CREATE);

if ($rar->deleteIndex(2)) {
    echo 'ok';
} else {
	echo 'failed';
}

RarArchiver::deleteName

Description

boolean RarArchiver::deleteName ( string $name )

Delete an entry in the archive using its name.

Parameters

  • `name` - Name of the entry to delete.

Return Values

Returns TRUE on success or FALSE on failure.

Examples

$rar = new RarArchiver('example.rar', RarArchiver::CREATE);

if ($rar->deleteName('testfromfile.php')) {
    echo 'ok';
} else {
	echo 'failed';
}

RarArchiver::getFromIndex

Description

string RarArchiver::getFromIndex ( int $index [, int $length = 0] )

Returns the entry contents using its index.

Parameters

  • `index` - Index of the entry.
  • `length` - The length to be read from the entry. If 0, then the entire entry is read.

Return Values

Returns the contents of the entry on success or FALSE on failure.

Examples

$rar = new RarArchiver('example.rar', RarArchiver::CREATE);

$rar->getFromIndex(2);
// or
$rar->getFromIndex(2, 100);

RarArchiver::getFromName

Description

string RarArchiver::getFromName ( string $name [, int $length = 0] )

Returns the entry contents using its name.

Parameters

  • `name` - Name of the entry.
  • `length` - The length to be read from the entry. If 0, then the entire entry is read.

Return Values

Returns the contents of the entry on success or FALSE on failure.

Examples

$rar = new RarArchiver('example.rar', RarArchiver::CREATE);

$rar->getFromName('testfromfile.php');
// or
$rar->getFromIndex('testfromfile.php', 100);

RarArchiver::getNameIndex

Description

string RarArchiver::getNameIndex ( int $index )

Returns the name of an entry using its index.

Parameters

  • `index` - Index of the entry.

Return Values

Returns the name on success or FALSE on failure.

Examples

$rar = new RarArchiver('example.rar', RarArchiver::CREATE);

$rar->getNameIndex(2);

RarArchiver::renameIndex

Description

boolean RarArchiver::renameIndex ( int $index , string $newname )

Renames an entry defined by its index.

Parameters

  • `index` - Index of the entry to rename.
  • `newname` - New name.

Return Values

Returns TRUE on success or FALSE on failure.

Examples

$rar = new RarArchiver('example.rar', RarArchiver::CREATE);

if ($rar->renameIndex(2, 'newname.php')) {
    echo 'ok';
} else {
	echo 'failed';
}

RarArchiver::renameName

Description

boolean RarArchiver::renameName ( string $name , string $newname )

Renames an entry defined by its name.

Parameters

  • `name` - Name of the entry to rename.
  • `newname` - New name.

Return Values

Returns TRUE on success or FALSE on failure.

Examples

$rar = new RarArchiver('example.rar', RarArchiver::CREATE);

if ($rar->renameName('testfromfile.php', 'newname.php')) {
    echo 'ok';
} else {
	echo 'failed';
}

RarArchiver::extractTo

Description

boolean RarArchiver::extractTo ( string $destination [, mixed $entries ] )

Extract the complete archive or the given files to the specified destination.

Parameters

  • `destination` - Location where to extract the files.
  • `entries` - The entries to extract. It accepts either a single entry name or an array of names.

Return Values

Returns TRUE on success or FALSE on failure.

Examples

$rar = new RarArchiver('example.rar', RarArchiver::CREATE);

if ($rar->extractTo('/my/destination/dir/')) {
    echo 'ok';
} else {
	echo 'failed';
}
  Files folder image Files  
File Role Description
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Plain text file RarArchiver.php Class Class source
Accessible without login Plain text file README.md Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:745
This week:2
All time:4,441
This week:143Up
 User Ratings  
 
 All time
Utility:75%StarStarStarStar
Consistency:80%StarStarStarStarStar
Documentation:70%StarStarStarStar
Examples:65%StarStarStarStar
Tests:-
Videos:-
Overall:59%StarStarStar
Rank:1481
  

For more information send a message to info at phpclasses dot org.