Skip to content

PhpZip is a php-library for extended work with ZIP-archives.

License

Notifications You must be signed in to change notification settings

Ne-Lexa/php-zip

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Documentation

Create and manipulate zip archives. No use ZipArchive class and php-zip extension.

class \Nelexa\Zip\ZipFile

Initialization

$zip = new \Nelexa\Zip\ZipFile();

Create archive

$zip->create();

Open archive file

$zip->open($filename);

Open archive from string

$zip->openFromString($string)

Set password

$zip->setPassword($password);

List files

$listFiles = $zip->getListFiles();

Get count files

$countFiles = $zip->getCountFiles();

Add empty dir

$zip->addEmptyDir($dirName);

Add dir

$directory = "/tmp";
$ignoreFiles = array("xxx.file", "xxx2.file");
$zip->addDir($directory); // add path /tmp to /
$zip->addDir($directory, "var/temp"); // add path /tmp to var/temp
$zip->addDir($directory, "var/temp", $ignoreFiles); // add path /tmp to var/temp and ignore files xxx.file and xxx2.file

Add files from glob pattern

$zip->addGlob("music/*.mp3"); // add all mp3 files

Add files from regex pattern

$zip->addPattern("~file[0-9]+\.jpg$~", "picture/");

Add file

$zip->addFile($filename);
$zip->addFile($filename, $localName);
$zip->addFile($filename, $localName, \Nelexa\Zip\ZipEntry::COMPRESS_METHOD_STORED); // no compression
$zip->addFile($filename, $localName, \Nelexa\Zip\ZipEntry::COMPRESS_METHOD_DEFLATED);

Add file from string

$zip->addFromString($localName, $contents);
$zip->addFromString($localName, $contents,  \Nelexa\Zip\ZipEntry::COMPRESS_METHOD_STORED); // no compression
$zip->addFromString($localName, $contents,  \Nelexa\Zip\ZipEntry::COMPRESS_METHOD_DEFLATED);

Update timestamp for all files

$timestamp = time(); // now time
$zip->updateTimestamp($timestamp);

Delete files from glob pattern

$zip->deleteGlob("*.jpg"); // remove all jpg files

Delete files from regex pattern

$zip->deletePattern("~\.jpg$~i"); // remove all jpg files

Delete file from index

$zip->deleteIndex(0);

Delete all files

$zip->deleteAll();

Delete from file name

$zip->deleteName($filename);

Extract zip archive

$zip->extractTo($toPath)
$zip->extractTo($toPath, array("file1", "file2")); // extract only files file1 and file2

Get archive comment

$archiveComment = $zip->getArchiveComment();

Set archive comment

$zip->setArchiveComment($comment)

Get comment file from index

$commentFile = $zip->getCommentIndex($index);

Set comment file from index

$zip->setCommentIndex($index, $comment);

Get comment file from filename

$commentFile = $zip->getCommentName($filename);

Set comment file from filename

$zip->setCommentName($name, $comment);

Get file content from index

$content = $zip->getFromIndex($index);

Get file content from filename

$content = $zip->getFromName($name);

Get filename from index

$filename = $zip->getNameIndex($index);

Rename file from index

$zip->renameIndex($index, $newFilename);

Rename file from filename

$zip->renameName($oldName, $newName);

Get zip entries

/**
 * @var \Nelexa\Zip\ZipEntry[] $zipEntries
 */
$zipEntries = $zip->getZipEntries();

Get zip entry from index

/**
 * @var \Nelexa\Zip\ZipEntry $zipEntry
 */
$zipEntry = $zip->getZipEntryIndex($index);

Get zip entry from filename

/**
 * @var \Nelexa\Zip\ZipEntry $zipEntry
 */
$zipEntry = $zip->getZipEntryName($name);

Get info from index

$info = $zip->statIndex($index);
// [
//     'name' - filename
//     'index' - index number
//     'crc' - crc32
//     'size' - uncompressed size
//     'mtime' - last modify date time
//     'comp_size' - compressed size
//     'comp_method' - compressed method
// ]

Get info from name

$info = $zip->statName($name);
// [
//     'name' - filename
//     'index' - index number
//     'crc' - crc32
//     'size' - uncompressed size
//     'mtime' - last modify date time
//     'comp_size' - compressed size
//     'comp_method' - compressed method
// ]

Get info from all files

$info = $zip->getExtendedListFiles();

Get output contents

$content = $zip->output();

Save opened file

$isSuccessSave = $zip->save();

Save file as

$zip->saveAs($outputFile);

Close archive

$zip->close();

Example create zip archive

$zip = new \Nelexa\Zip\ZipFile();
$zip->create();
$zip->addFile("README.md");
$zip->addFile("README.md", "folder/README");
$zip->addFromString("folder/file.txt", "File content");
$zip->addEmptyDir("f/o/l/d/e/r");
$zip->setArchiveComment("Archive comment");
$zip->setCommentIndex(0, "Comment file with index 0");
$zip->saveAs("output.zip");
$zip->close();

// $ zipinfo output.zip
// Archive:  output.zip
// Zip file size: 912 bytes, number of entries: 4
// -rw----     1.0 fat      387 b- defN README.md
// -rw----     1.0 fat      387 b- defN folder/README
// -rw----     1.0 fat       12 b- defN folder/file.txt
// -rw----     1.0 fat        0 b- stor f/o/l/d/e/r/
// 4 files, 786 bytes uncompressed, 448 bytes compressed:  43.0%

Example modification zip archive

$zip = new \Nelexa\Zip\ZipFile();
$zip->open("output.zip");
$zip->addFromString("new-file", file_get_contents(__FILE__));
$zip->saveAs("output2.zip");
$zip->close();

// $ zipinfo output2.zip 
// Archive:  output2.zip
// Zip file size: 1331 bytes, number of entries: 5
// -rw----     1.0 fat      387 b- defN README.md
// -rw----     1.0 fat      387 b- defN folder/README
// -rw----     1.0 fat       12 b- defN folder/file.txt
// -rw----     1.0 fat        0 b- stor f/o/l/d/e/r/
// -rw----     1.0 fat      593 b- defN new-file
// 5 files, 1379 bytes uncompressed, 775 bytes compressed:  43.8%