PHP Classes

ZipFly PHP ZipArchive zip64 Creator: Compress files and create archives in Zip format

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 263 All time: 7,781 This week: 488Up
Version License PHP version Categories
zipfly 1.0.4BSD License5.6Tools, Files and Folders, Compression, P...
Description 

Author

This package can compress files and create archives in Zip format.

It can create a ZIP archive from the scratch with a given name in the ZIP64 format.

The package can also add individual files to the archive and compress them immediately, so the compressed data is added to the ZIP archive file.

Innovation Award
PHP Programming Innovation award nominee
November 2018
Number 2


Prize: One big elePHPant Plush Mascott
ZIP is a very popular format for storing a structure of directories and files in a single compressed file.

However, for creating ZIP files that can store very large files, it may be necessary to create ZIP files that use the ZIP64 format version.

This package provides a pure PHP implementation of the packing and compression process that supports the ZIP64 format.

Manuel Lemos
Picture of David Tamas
  Performance   Level  
Name: David Tamas <contact>
Classes: 5 packages by
Country: Hungary Hungary
Age: 37
All time rank: 310928 in Hungary Hungary
Week rank: 164 Up3 in Hungary Hungary Up
Innovation award
Innovation award
Nominee: 2x

Winner: 1x

Example

<?php

function gAutoLoader($class) {
   
$class = str_replace('\\', DIRECTORY_SEPARATOR, $class);

    if (
is_readable($class . '.php')) {
        include_once
$class . '.php';
        return
true;
    }

    return
false;
}

spl_autoload_register('gAutoLoader');


define('GTEST_DIR', __DIR__.'/');

if (isset(
$argv[1])) {
   
$nr = $argv[1];
} else {
   
$nr = '';
}



//$zipFile = new \zipFly\zipFly64(GTEST_DIR.'onfly'.$nr.'.zip');
$zipFile = new \zipFly\zipFly64();
$zipFile->setDebugMode(false);
$zipFile->setZipFeature(true, false);

$zipFile->create(GTEST_DIR.'onfly'.$nr.'.zip');

$zipFile->addFile(GTEST_DIR."zipFly/zipFly64.php", "zipFly64.php", \zipFly\zipFly64::METHOD_BZIP2);
$zipFile->addFile(GTEST_DIR."zipFly/parts/headers.php", "parts/headers.php", \zipFly\zipFly64::METHOD_DEFLATE);
$zipFile->addFromString("Zip64\nTest\n", 'test.txt');

$zipFile->close();


Details

G-Lex's zipFly compression library

This PHP library helps you to create true zip64 archives that contains hundreds of thousands of files. It is highly optimized for low memory and I/O usage even when you need to compress a large number of files.

Story

I started to create a custom PHP based backup software that can accomplish my special requirements. I've got 12 debian wheezy and jessie based linux servers with PHP 5.6 installed on it. Originally I wanted to use the built-in zipArchive class to the compression purpose. Unfortunately I have realized that the version shipped with the OS can't do this job. When adding a large number of files to the zip archive I was running into memory limit problems as well as file descriptor limit issues. The reason is that when a file is added to the zip, the source file will open and remain open until you close the entire zip file. After I solved the file descriptor issue with closing and reopening the zip archive after a certain number of added files, I was attempting to compress some larger files. I think you know... I faced another surprise. The original zip format has file number and file size restrictions. Because the zipArchive version that I used did not support the zip64 extension to overcome these limits, so the created large zip files can not be extracted due to file corruption. There was also lot of another problem with zipArchive: big I/O wasting, can not set different compression algorithm to different files, can not track the compression progress and more and more... I tried to find another solution in the form of different PHP classes, but none was perfectly suited to this task.

This was the point where I decided to develop my own solution.

Goals

  • [x] Working standard ZIP compressor
  • [x] Implement zip64 extension
  • [x] Handle zip creation with a huge amount of file
  • [x] Compress large files in small chunks
  • [x] No unwanted I/O
  • [x] Optimize for high speed
  • [x] Optimize for low memory usage
  • [x] Optimize for high speed
  • [x] Selectable compression algorithm and level
  • [x] Per file compression algorithm setting
  • [x] After adding a new file, it is immediately compresses it and writes out the data
  • [x] Easy to use
  • [x] Error handling
  • [x] Documented public interfaces
  • [x] Well documented source

Ideas to be tested and reimplemented

  • [x] Streamable zip creation
  • [x] Switch between 32bit and 64bit mode
  • [x] Compress small files in one step
  • [x] Add file from string
  • [x] Print out debug informations _Technically, these features were the basics of the finished class, but due to optimization and simplicity, they are not currently in the classroom._

My motivation

I had to make my own class because I needed a functionality that I did not find in a ready-made library, and I did not find it effective and perfect way if i modify one of them. I did not want to create a zip library with the same features as the hundreds of other zip classes. I have studied many different open source zip compression libraries that were also fantastic in themselves. By combining best techniques from these with my own ideas, I've created the zipFly64 library.

Interestings of this library

  • Using a way to easily define exception classes with multiple message and exception code pairs
  • An abstract exception class that enable us to hide some levels from the default textual representation's backtrace log for the easier debugging
  • Really small codebase
  • Ability to print out the generated headers in a fancy form
  • Custom created php stream filter to calculate the uncompressed input file size and the crc32 hash value, by examining the chunks readed by the compressor
  • Data compression using stream filters attached to the read chain of the source file. Allowing to process the source file chunk-by-chunk

Whats new

2018-12-21

  • Documented the source code
  • Configurable duplicate entry filter
  • New methods for add entries to the archive: - addFile - Add entry from file - addFromString - Add entry from string input - addFromStream - Add entry from stream resource
  • Compress small files in one step instead of streamed processing
  • Ability to directly stream out the generated ZIP file to the browser or any stream resource without saving it to the local storage.
  • Separated the debug functionality
  • Improved Exception system
  • Code cleanup
  • Speed and memory optimizations
  • Minimum required PHP version: 5.6.3

Warnings & Limitations

This library and the associated files are non commercial product. It should not have unexpected results. However if any damage is caused by this software the author can not be responsible. The use of this software is at the risk of the user.


  Files folder image Files (16)  
File Role Description
Files folder imagezipFly (1 file, 2 directories)
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Read me
Accessible without login Plain text file test-stream.php Example - Added debug mode - Added options to enable/disable the ZIP64 extension - Added option to enable/disable the streamed zip feature - New class hierarchy for the new static variables based centralization - Lot of memory and performance optimization - Code cleanup
Accessible without login Plain text file test.php Example Example script
Accessible without login Plain text file _config.yml Data Auxiliary data

  Files folder image Files (16)  /  zipFly  
File Role Description
Files folder imageExceptions (4 files)
Files folder imageparts (6 files)
  Plain text file zipFly64.php Class Class source

  Files folder image Files (16)  /  zipFly  /  Exceptions  
File Role Description
  Plain text file abstractException.php Class Class source
  Plain text file directoryException.php Class Class source
  Plain text file fileException.php Class Class source
  Plain text file zipFlyException.php Class Class source

  Files folder image Files (16)  /  zipFly  /  parts  
File Role Description
  Plain text file base.php Class - Added debug mode - Added options to enable/disable the ZIP64 extension - Added option to enable/disable the streamed zip feature - New class hierarchy for the new static variables based centralization - Lot of memory and performance optimization - Code cleanup
  Plain text file constants.php Class Class source
  Plain text file debugger.php Class Class source
  Plain text file entry.php Class Class source
  Plain text file hashStream.php Class Class source
  Plain text file headers.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:263
This week:0
All time:7,781
This week:488Up