PhpZip
هي مكتبة php للعمل الممتد مع أرشيفات ZIP.
التوثيق الروسي
إصدار | PHP | التوثيق |
---|---|---|
^4.0 (رئيسي) | ^7.4|^8.0 | حاضِر |
^3.0 | ^5.5|^7.0 | المستندات v3.3 |
PhpZipZipFile
php-zip
وclass ZipArchive
).php-bz2
.ZIP64
(حجم الملف أكثر من 4 جيجابايت أو عدد الإدخالات في الأرشيف أكثر من 65535).انتباه!
بالنسبة للأنظمة 32 بت، طريقة التشفير
Traditional PKWARE Encryption (ZipCrypto)
غير مدعومة حاليًا. استخدم طريقة التشفيرWinZIP AES Encryption
كلما أمكن ذلك.
Traditional PKWARE Encryption (ZipCrypto)
وطرق تشفير WinZIP AES Encryption
.PHP
>= 7.4 أو PHP
>= 8.0 (يفضل 64 بت).bzip2
لضغط BZIP2.openssl
لدعم WinZip Aes Encryption
. composer require nelexa/zip
أحدث نسخة مستقرة:
// create new archive
$ zipFile = new PhpZip ZipFile ();
try {
$ zipFile
-> addFromString ( ' zip/entry/filename ' , ' Is file content ' ) // add an entry from the string
-> addFile ( ' /path/to/file ' , ' data/tofile ' ) // add an entry from the file
-> addDir ( __DIR__ , ' to/path/ ' ) // add files from the directory
-> saveAsFile ( $ outputFilename ) // save the archive to a file
-> close (); // close archive
// open archive, extract, add files, set password and output to browser.
$ zipFile
-> openFile ( $ outputFilename ) // open archive from file
-> extractTo ( $ outputDirExtract ) // extract files to the specified directory
-> deleteFromRegex ( ' ~^.~ ' ) // delete all hidden (Unix) files
-> addFromString ( ' dir/file.txt ' , ' Test file ' ) // add a new entry from the string
-> setPassword ( ' password ' ) // set password for all entries
-> outputAsAttachment ( ' library.jar ' ); // output to the browser without saving to a file
}
catch ( PhpZip Exception ZipException $ e ){
// handle exception
}
finally {
$ zipFile -> close ();
}
يمكن العثور على أمثلة أخرى في المجلد tests/
Zip Entry - ملف أو مجلد في أرشيف ZIP. كل إدخال في الأرشيف له خصائص معينة، على سبيل المثال: اسم الملف، طريقة الضغط، طريقة التشفير، حجم الملف قبل الضغط، حجم الملف بعد الضغط، CRC32 وغيرها.
PhpZipZipFile
SplFileInfo
إلى أرشيف ZIP.SymfonyComponentFinderFinder
إلى أرشيف ZIP.تهيئة أرشيف ZIP
$ zipFile = new PhpZip ZipFile ();
يفتح أرشيف مضغوط من ملف.
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> openFile ( ' file.zip ' );
يفتح أرشيف مضغوط من سلسلة.
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> openFromString ( $ stringContents );
يفتح أرشيف مضغوط من الدفق.
$ stream = fopen ( ' file.zip ' , ' rb ' );
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> openFromStream ( $ stream );
إرجاع عدد الإدخالات في الأرشيف.
$ zipFile = new PhpZip ZipFile ();
$ count = count ( $ zipFile );
// or
$ count = $ zipFile -> count ();
إرجاع قائمة ملفات الأرشيف.
$ zipFile = new PhpZip ZipFile ();
$ listFiles = $ zipFile -> getListFiles ();
// example array contents:
// array (
// 0 => 'info.txt',
// 1 => 'path/to/file.jpg',
// 2 => 'another path/',
// 3 => '0',
// )
إرجاع محتويات الإدخال باستخدام اسمه.
// $entryName = 'path/to/example-entry-name.txt';
$ zipFile = new PhpZip ZipFile ();
$ contents = $ zipFile [ $ entryName ];
// or
$ contents = $ zipFile -> getEntryContents ( $ entryName );
يتحقق مما إذا كان هناك إدخال في الأرشيف.
// $entryName = 'path/to/example-entry-name.txt';
$ zipFile = new PhpZip ZipFile ();
$ hasEntry = isset ( $ zipFile [ $ entryName ]);
// or
$ hasEntry = $ zipFile -> hasEntry ( $ entryName );
التحقق من أن الإدخال في الأرشيف هو دليل.
// $entryName = 'path/to/';
$ zipFile = new PhpZip ZipFile ();
$ isDirectory = $ zipFile -> isDirectory ( $ entryName );
استخراج محتويات الأرشيف. يجب أن يكون الدليل موجودا.
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> extractTo ( $ directory );
استخراج بعض الملفات إلى الدليل. يجب أن يكون الدليل موجودا.
// $toDirectory = '/tmp';
$ extractOnlyFiles = [
' filename1 ' ,
' filename2 ' ,
' dir/dir/dir/ '
];
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> extractTo ( $ toDirectory , $ extractOnlyFiles );
ZipFile
هو مكرر. يمكن تكرار كافة الإدخالات في حلقة foreach
.
foreach ( $ zipFile as $ entryName => $ contents ){
echo " Filename: $ entryName " . PHP_EOL ;
echo " Contents: $ contents " . PHP_EOL ;
echo ' ----------------------------- ' . PHP_EOL ;
}
يمكن التكرار من خلال Iterator
.
$ iterator = new ArrayIterator ( $ zipFile );
while ( $ iterator -> valid ())
{
$ entryName = $ iterator -> key ();
$ contents = $ iterator -> current ();
echo " Filename: $ entryName " . PHP_EOL ;
echo " Contents: $ contents " . PHP_EOL ;
echo ' ----------------------------- ' . PHP_EOL ;
$ iterator -> next ();
}
إرجاع تعليق أرشيف Zip.
$ zipFile = new PhpZip ZipFile ();
$ commentArchive = $ zipFile -> getArchiveComment ();
إرجاع تعليق الإدخال باستخدام اسم الإدخال.
$ zipFile = new PhpZip ZipFile ();
$ commentEntry = $ zipFile -> getEntryComment ( $ entryName );
تتيح لك جميع طرق إضافة الإدخالات إلى أرشيف ZIP تحديد طريقة لضغط المحتوى.
تتوفر طرق الضغط التالية:
PhpZipConstantsZipCompressionMethod::STORED
- بدون ضغطPhpZipConstantsZipCompressionMethod::DEFLATED
- ضغط التفريغPhpZipConstantsZipCompressionMethod::BZIP2
- ضغط Bzip2 بالامتداد ext-bz2
يضيف ملفًا إلى أرشيف ZIP من المسار المحدد.
$ zipFile = new PhpZip ZipFile ();
// $file = '...../file.ext';
// $entryName = 'file2.ext'
$ zipFile -> addFile ( $ file );
// you can specify the name of the entry in the archive (if null, then the last component from the file name is used)
$ zipFile -> addFile ( $ file , $ entryName );
// you can specify a compression method
$ zipFile -> addFile ( $ file , $ entryName , PhpZip Constants ZipCompressionMethod:: STORED ); // No compression
$ zipFile -> addFile ( $ file , $ entryName , PhpZip Constants ZipCompressionMethod:: DEFLATED ); // Deflate compression
$ zipFile -> addFile ( $ file , $ entryName , PhpZip Constants ZipCompressionMethod:: BZIP2 ); // BZIP2 compression
يضيف SplFileInfo
إلى أرشيف ZIP.
// $file = '...../file.ext';
// $entryName = 'file2.ext'
$ zipFile = new PhpZip ZipFile ();
$ splFile = new SplFileInfo ( ' README.md ' );
$ zipFile -> addSplFile ( $ splFile );
$ zipFile -> addSplFile ( $ splFile , $ entryName );
// or
$ zipFile [ $ entryName ] = new SplFileInfo ( $ file );
// set compression method
$ zipFile -> addSplFile ( $ splFile , $ entryName , $ options = [
PhpZip Constants ZipOptions:: COMPRESSION_METHOD => PhpZip Constants ZipCompressionMethod:: DEFLATED ,
]);
إضافة ملفات من SymfonyComponentFinderFinder
إلى أرشيف ZIP.
$ finder = new Symfony Component Finder Finder ();
$ finder
-> files ()
-> name ( ' *.{jpg,jpeg,gif,png} ' )
-> name ( ' /^[0-9a-f]./ ' )
-> contains ( ' /lorems+ipsum$/i ' )
-> in ( ' path ' );
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> addFromFinder ( $ finder , $ options = [
PhpZip Constants ZipOptions:: COMPRESSION_METHOD => PhpZip Constants ZipCompressionMethod:: DEFLATED ,
PhpZip Constants ZipOptions:: MODIFIED_TIME => new DateTimeImmutable ( ' -1 day 5 min ' )
]);
إضافة ملف إلى أرشيف ZIP باستخدام محتوياته.
$ zipFile = new PhpZip ZipFile ();
$ zipFile [ $ entryName ] = $ contents ;
// or
$ zipFile -> addFromString ( $ entryName , $ contents );
// you can specify a compression method
$ zipFile -> addFromString ( $ entryName , $ contents , PhpZip Constants ZipCompressionMethod:: STORED ); // No compression
$ zipFile -> addFromString ( $ entryName , $ contents , PhpZip Constants ZipCompressionMethod:: DEFLATED ); // Deflate compression
$ zipFile -> addFromString ( $ entryName , $ contents , PhpZip Constants ZipCompressionMethod:: BZIP2 ); // BZIP2 compression
إضافة إدخال من الدفق إلى أرشيف ZIP.
$ zipFile = new PhpZip ZipFile ();
// $stream = fopen(..., 'rb');
$ zipFile -> addFromStream ( $ stream , $ entryName );
// or
$ zipFile [ $ entryName ] = $ stream ;
// you can specify a compression method
$ zipFile -> addFromStream ( $ stream , $ entryName , PhpZip Constants ZipCompressionMethod:: STORED ); // No compression
$ zipFile -> addFromStream ( $ stream , $ entryName , PhpZip Constants ZipCompressionMethod:: DEFLATED ); // Deflate compression
$ zipFile -> addFromStream ( $ stream , $ entryName , PhpZip Constants ZipCompressionMethod:: BZIP2 ); // BZIP2 compression
إضافة دليل جديد.
$ zipFile = new PhpZip ZipFile ();
// $path = "path/to/";
$ zipFile -> addEmptyDir ( $ path );
// or
$ zipFile [ $ path ] = null ;
يضيف كافة الإدخالات من صفيف.
$ entries = [
' file.txt ' => ' file contents ' , // add an entry from the string contents
' empty dir/ ' => null , // add empty directory
' path/to/file.jpg ' => fopen ( ' ..../filename ' , ' rb ' ), // add an entry from the stream
' path/to/file.dat ' => new SplFileInfo ( ' ..../filename ' ), // add an entry from the file
];
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> addAll ( $ entries );
يضيف الملفات إلى الأرشيف من الدليل على المسار المحدد بدون أدلة فرعية.
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> addDir ( $ dirName );
// you can specify the path in the archive to which you want to put entries
$ localPath = ' to/path/ ' ;
$ zipFile -> addDir ( $ dirName , $ localPath );
// you can specify a compression method
$ zipFile -> addDir ( $ dirName , $ localPath , PhpZip Constants ZipCompressionMethod:: STORED ); // No compression
$ zipFile -> addDir ( $ dirName , $ localPath , PhpZip Constants ZipCompressionMethod:: DEFLATED ); // Deflate compression
$ zipFile -> addDir ( $ dirName , $ localPath , PhpZip Constants ZipCompressionMethod:: BZIP2 ); // BZIP2 compression
يضيف الملفات إلى الأرشيف من الدليل على المسار المحدد باستخدام الدلائل الفرعية.
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> addDirRecursive ( $ dirName );
// you can specify the path in the archive to which you want to put entries
$ localPath = ' to/path/ ' ;
$ zipFile -> addDirRecursive ( $ dirName , $ localPath );
// you can specify a compression method
$ zipFile -> addDirRecursive ( $ dirName , $ localPath , PhpZip Constants ZipCompressionMethod:: STORED ); // No compression
$ zipFile -> addDirRecursive ( $ dirName , $ localPath , PhpZip Constants ZipCompressionMethod:: DEFLATED ); // Deflate compression
$ zipFile -> addDirRecursive ( $ dirName , $ localPath , PhpZip Constants ZipCompressionMethod:: BZIP2 ); // BZIP2 compression
يضيف الملفات من مكرر الدلائل.
// $directoryIterator = new DirectoryIterator($dir); // without subdirectories
// $directoryIterator = new RecursiveDirectoryIterator($dir); // with subdirectories
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> addFilesFromIterator ( $ directoryIterator );
// you can specify the path in the archive to which you want to put entries
$ localPath = ' to/path/ ' ;
$ zipFile -> addFilesFromIterator ( $ directoryIterator , $ localPath );
// or
$ zipFile [ $ localPath ] = $ directoryIterator ;
// you can specify a compression method
$ zipFile -> addFilesFromIterator ( $ directoryIterator , $ localPath , PhpZip Constants ZipCompressionMethod:: STORED ); // No compression
$ zipFile -> addFilesFromIterator ( $ directoryIterator , $ localPath , PhpZip Constants ZipCompressionMethod:: DEFLATED ); // Deflate compression
$ zipFile -> addFilesFromIterator ( $ directoryIterator , $ localPath , PhpZip Constants ZipCompressionMethod:: BZIP2 ); // BZIP2 compression
مثال مع تجاهل بعض الملفات:
$ ignoreFiles = [
' file_ignore.txt ' ,
' dir_ignore/sub dir ignore/ '
];
// $directoryIterator = new DirectoryIterator($dir); // without subdirectories
// $directoryIterator = new RecursiveDirectoryIterator($dir); // with subdirectories
// use PhpZipUtilIteratorIgnoreFilesFilterIterator for non-recursive search
$ zipFile = new PhpZip ZipFile ();
$ ignoreIterator = new PhpZip Util Iterator IgnoreFilesRecursiveFilterIterator (
$ directoryIterator ,
$ ignoreFiles
);
$ zipFile -> addFilesFromIterator ( $ ignoreIterator );
يضيف ملفات من دليل بنمط الكرة الأرضية بدون أدلة فرعية.
$ globPattern = ' **.{jpg,jpeg,png,gif} ' ; // example glob pattern -> add all .jpg, .jpeg, .png and .gif files
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> addFilesFromGlob ( $ dir , $ globPattern );
// you can specify the path in the archive to which you want to put entries
$ localPath = ' to/path/ ' ;
$ zipFile -> addFilesFromGlob ( $ dir , $ globPattern , $ localPath );
// you can specify a compression method
$ zipFile -> addFilesFromGlob ( $ dir , $ globPattern , $ localPath , PhpZip Constants ZipCompressionMethod:: STORED ); // No compression
$ zipFile -> addFilesFromGlob ( $ dir , $ globPattern , $ localPath , PhpZip Constants ZipCompressionMethod:: DEFLATED ); // Deflate compression
$ zipFile -> addFilesFromGlob ( $ dir , $ globPattern , $ localPath , PhpZip Constants ZipCompressionMethod:: BZIP2 ); // BZIP2 compression
يضيف ملفات من دليل عن طريق نمط الكرة الأرضية مع الدلائل الفرعية.
$ globPattern = ' **.{jpg,jpeg,png,gif} ' ; // example glob pattern -> add all .jpg, .jpeg, .png and .gif files
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> addFilesFromGlobRecursive ( $ dir , $ globPattern );
// you can specify the path in the archive to which you want to put entries
$ localPath = ' to/path/ ' ;
$ zipFile -> addFilesFromGlobRecursive ( $ dir , $ globPattern , $ localPath );
// you can specify a compression method
$ zipFile -> addFilesFromGlobRecursive ( $ dir , $ globPattern , $ localPath , PhpZip Constants ZipCompressionMethod:: STORED ); // No compression
$ zipFile -> addFilesFromGlobRecursive ( $ dir , $ globPattern , $ localPath , PhpZip Constants ZipCompressionMethod:: DEFLATED ); // Deflate compression
$ zipFile -> addFilesFromGlobRecursive ( $ dir , $ globPattern , $ localPath , PhpZip Constants ZipCompressionMethod:: BZIP2 ); // BZIP2 compression
يضيف ملفات من دليل بنمط PCRE بدون أدلة فرعية.
$ regexPattern = ' /.(jpe?g|png|gif)$/si ' ; // example regex pattern -> add all .jpg, .jpeg, .png and .gif files
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> addFilesFromRegex ( $ dir , $ regexPattern );
// you can specify the path in the archive to which you want to put entries
$ localPath = ' to/path/ ' ;
$ zipFile -> addFilesFromRegex ( $ dir , $ regexPattern , $ localPath );
// you can specify a compression method
$ zipFile -> addFilesFromRegex ( $ dir , $ regexPattern , $ localPath , PhpZip Constants ZipCompressionMethod:: STORED ); // No compression
$ zipFile -> addFilesFromRegex ( $ dir , $ regexPattern , $ localPath , PhpZip Constants ZipCompressionMethod:: DEFLATED ); // Deflate compression
$ zipFile -> addFilesFromRegex ( $ dir , $ regexPattern , $ localPath , PhpZip Constants ZipCompressionMethod:: BZIP2 ); // BZIP2 compression
يضيف ملفات من دليل بواسطة نمط PCRE مع الدلائل الفرعية.
$ regexPattern = ' /.(jpe?g|png|gif)$/si ' ; // example regex pattern -> add all .jpg, .jpeg, .png and .gif files
$ zipFile -> addFilesFromRegexRecursive ( $ dir , $ regexPattern );
// you can specify the path in the archive to which you want to put entries
$ localPath = ' to/path/ ' ;
$ zipFile -> addFilesFromRegexRecursive ( $ dir , $ regexPattern , $ localPath );
// you can specify a compression method
$ zipFile -> addFilesFromRegexRecursive ( $ dir , $ regexPattern , $ localPath , PhpZip Constants ZipCompressionMethod:: STORED ); // No compression
$ zipFile -> addFilesFromRegexRecursive ( $ dir , $ regexPattern , $ localPath , PhpZip Constants ZipCompressionMethod:: DEFLATED ); // Deflate compression
$ zipFile -> addFilesFromRegexRecursive ( $ dir , $ regexPattern , $ localPath , PhpZip Constants ZipCompressionMethod:: BZIP2 ); // BZIP2 compression
حذف إدخال في الأرشيف باستخدام اسمه.
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> deleteFromName ( $ entryName );
يحذف الإدخالات في الأرشيف باستخدام نمط الكرة الأرضية.
$ globPattern = ' **.{jpg,jpeg,png,gif} ' ; // example glob pattern -> delete all .jpg, .jpeg, .png and .gif files
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> deleteFromGlob ( $ globPattern );
يحذف الإدخالات في الأرشيف باستخدام نمط PCRE.
$ regexPattern = ' /.(jpe?g|png|gif)$/si ' ; // example regex pattern -> delete all .jpg, .jpeg, .png and .gif files
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> deleteFromRegex ( $ regexPattern );
يحذف جميع الإدخالات في أرشيف ZIP.
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> deleteAll ();
إعادة تسمية الإدخال المحدد باسمه.
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> rename ( $ oldName , $ newName );
قم بتعيين مستوى الضغط لجميع الملفات الموجودة في الأرشيف.
لاحظ أن هذا الأسلوب لا ينطبق على الإدخالات التي تتم إضافتها بعد تشغيل هذا الأسلوب.
بشكل افتراضي، مستوى الضغط هو 5 ( PhpZipConstantsZipCompressionLevel::NORMAL
) أو مستوى الضغط المحدد في الأرشيف لضغط Deflate.
تتراوح القيم من 1 ( PhpZipConstantsZipCompressionLevel::SUPER_FAST
) إلى 9 ( PhpZipConstantsZipCompressionLevel::MAXIMUM
) مدعومة. كلما زاد الرقم، كان الضغط أفضل وأطول.
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> setCompressionLevel ( PhpZip Constants ZipCompressionLevel:: MAXIMUM );
يضبط مستوى الضغط للإدخال حسب اسمه.
تتراوح القيم من 1 ( PhpZipConstantsZipCompressionLevel::SUPER_FAST
) إلى 9 ( PhpZipConstantsZipCompressionLevel::MAXIMUM
) مدعومة. كلما زاد الرقم، كان الضغط أفضل وأطول.
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> setCompressionLevelEntry ( $ entryName , PhpZip Constants ZipCompressionLevel:: FAST );
يضبط طريقة الضغط للإدخال حسب اسمه.
تتوفر طرق الضغط التالية:
PhpZipConstantsZipCompressionMethod::STORED
- بدون ضغطPhpZipConstantsZipCompressionMethod::DEFLATED
- ضغط التفريغPhpZipConstantsZipCompressionMethod::BZIP2
- ضغط Bzip2 بالامتداد ext-bz2
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> setCompressionMethodEntry ( $ entryName , PhpZip Constants ZipCompressionMethod:: DEFLATED );
قم بتعيين تعليق أرشيف ZIP.
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> setArchiveComment ( $ commentArchive );
قم بتعيين تعليق الإدخال المحدد حسب اسمه.
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> setEntryComment ( $ entryName , $ comment );
اختيار الإدخالات في الأرشيف لإجراء العمليات عليها.
$ zipFile = new PhpZip ZipFile ();
$ matcher = $ zipFile -> matcher ();
تحديد الملفات من الأرشيف واحدًا تلو الآخر:
$ matcher
-> add ( ' entry name ' )
-> add ( ' another entry ' );
حدد ملفات متعددة في الأرشيف:
$ matcher -> add ([
' entry name ' ,
' another entry name ' ,
' path/ '
]);
تحديد الملفات بالتعبير العادي:
$ matcher -> match ( ' ~.jpe?g$~i ' );
حدد كافة الملفات في الأرشيف:
$ matcher -> all ();
count() - يحصل على عدد الإدخالات المحددة:
$ count = count ( $ matcher );
// or
$ count = $ matcher -> count ();
getMatches() - يُرجع قائمة بالإدخالات المحددة:
$ entries = $ matcher -> getMatches ();
// example array contents: ['entry name', 'another entry name'];
استدعاء () - استدعاء وظيفة قابلة للاستدعاء على الإدخالات المحددة:
// example
$ matcher -> invoke ( static function ( $ entryName ) use ( $ zipFile ) {
$ newName = preg_replace ( ' ~.(jpe?g)$~i ' , ' .no_optimize.$1 ' , $ entryName );
$ zipFile -> rename ( $ entryName , $ newName );
});
وظائف للعمل على الإدخالات المحددة:
$ matcher -> delete (); // remove selected entries from a ZIP archive
$ matcher -> setPassword ( $ password ); // sets a new password for the selected entries
$ matcher -> setPassword ( $ password , $ encryptionMethod ); // sets a new password and encryption method to selected entries
$ matcher -> setEncryptionMethod ( $ encryptionMethod ); // sets the encryption method to the selected entries
$ matcher -> disableEncryption (); // disables encryption for selected entries
الدعم المطبق لطرق التشفير:
PhpZipConstantsZipEncryptionMethod::PKWARE
- تشفير PKWARE التقليدي (قديم)PhpZipConstantsZipEncryptionMethod::WINZIP_AES_256
- تشفير WinZip AES 256 بت (مستحسن)PhpZipConstantsZipEncryptionMethod::WINZIP_AES_192
- تشفير WinZip AES 192 بتPhpZipConstantsZipEncryptionMethod::WINZIP_AES_128
- تشفير WinZip AES 128 بت قم بتعيين كلمة المرور للأرشيف المفتوح.
لا يشترط تعيين كلمة مرور لإضافة إدخالات جديدة أو حذف إدخالات موجودة، ولكن إذا كنت تريد استخراج المحتوى أو تغيير الطريقة/مستوى الضغط أو طريقة التشفير أو تغيير كلمة المرور، ففي هذه الحالة يجب تحديد كلمة المرور.
$ zipFile -> setReadPassword ( $ password );
يحصل على كلمة مرور لقراءة الإدخال المحدد باسمه.
$ zipFile -> setReadPasswordEntry ( $ entryName , $ password );
تعيين كلمة مرور جديدة لجميع الملفات في الأرشيف.
لاحظ أن هذا الأسلوب لا ينطبق على الإدخالات التي تتم إضافتها بعد تشغيل هذا الأسلوب.
$ zipFile -> setPassword ( $ password );
يمكنك ضبط طريقة التشفير:
$ encryptionMethod = PhpZip Constants ZipEncryptionMethod:: WINZIP_AES_256 ;
$ zipFile -> setPassword ( $ password , $ encryptionMethod );
يعين كلمة مرور جديدة للإدخال المحدد باسمه.
$ zipFile -> setPasswordEntry ( $ entryName , $ password );
يمكنك ضبط طريقة التشفير:
$ encryptionMethod = PhpZip Constants ZipEncryptionMethod:: WINZIP_AES_256 ;
$ zipFile -> setPasswordEntry ( $ entryName , $ password , $ encryptionMethod );
تعطيل التشفير لجميع الإدخالات الموجودة بالفعل في الأرشيف.
لاحظ أن هذا الأسلوب لا ينطبق على الإدخالات التي تتم إضافتها بعد تشغيل هذا الأسلوب.
$ zipFile -> disableEncryption ();
تعطيل تشفير الإدخال المحدد باسمه.
$ zipFile -> disableEncryptionEntry ( $ entryName );
التراجع عن كافة التغييرات التي تم إجراؤها في الأرشيف.
$ zipFile -> unchangeAll ();
التراجع عن التغييرات التي تم إجراؤها على تعليق الأرشيف.
$ zipFile -> unchangeArchiveComment ();
التراجع عن تغييرات الإدخال المحدد باسمه.
$ zipFile -> unchangeEntry ( $ entryName );
يحفظ الأرشيف في ملف.
$ zipFile -> saveAsFile ( $ filename );
يكتب الأرشيف إلى الدفق.
// $fp = fopen($filename, 'w+b');
$ zipFile -> saveAsStream ( $ fp );
إخراج أرشيف ZIP كسلسلة.
$ rawZipArchiveBytes = $ zipFile -> outputAsString ();
يقوم بإخراج أرشيف ZIP إلى المتصفح.
$ zipFile -> outputAsAttachment ( $ outputFilename );
يمكنك ضبط نوع Mime:
$ mimeType = ' application/zip ' ;
$ zipFile -> outputAsAttachment ( $ outputFilename , $ mimeType );
يُخرج أرشيف ZIP كاستجابة PSR-7.
يمكن استخدام طريقة الإخراج في أي إطار عمل متوافق مع PSR-7.
// $response = ....; // instance PsrHttpMessageResponseInterface
$ zipFile -> outputAsPsr7Response ( $ response , $ outputFilename );
يمكنك ضبط نوع Mime:
$ mimeType = ' application/zip ' ;
$ zipFile -> outputAsPsr7Response ( $ response , $ outputFilename , $ mimeType );
يقوم بإخراج أرشيف ZIP كاستجابة Symfony.
يمكن استخدام طريقة الإخراج في إطار عمل Symfony.
$ response = $ zipFile -> outputAsSymfonyResponse ( $ outputFilename );
يمكنك ضبط نوع Mime:
$ mimeType = ' application/zip ' ;
$ response = $ zipFile -> outputAsSymfonyResponse ( $ outputFilename , $ mimeType );
مثال للاستخدام في وحدة تحكم Symfony:
<?php
namespace App Controller ;
use PhpZip ZipFile ;
use Symfony Component HttpFoundation Response ;
use Symfony Component Routing Annotation Route ;
class DownloadZipController
{
/**
* @Route("/downloads/{id}")
*
* @throws PhpZipExceptionZipException
*/
public function __invoke ( string $ id ): Response
{
$ zipFile = new ZipFile ();
$ zipFile [ ' file ' ] = ' contents ' ;
$ outputFilename = $ id . ' .zip ' ;
return $ zipFile -> outputAsSymfonyResponse ( $ outputFilename );
}
}
احفظ التغييرات وأعد فتح الأرشيف الذي تم تغييره.
$ zipFile -> rewrite ();
أغلق الأرشيف.
$ zipFile -> close ();
تثبيت التبعيات للتطوير:
composer install --dev
تشغيل الاختبارات:
vendor/bin/phpunit
تم توثيق التغييرات في صفحة الإصدارات.
قم بتحديث الإصدار الرئيسي في الملف composer.json
إلى ^4.0
.
{
"require" : {
"nelexa/zip" : " ^4.0 "
}
}
ثم قم بتثبيت التحديثات باستخدام Composer
:
composer update nelexa/zip
قم بتحديث التعليمات البرمجية الخاصة بك للعمل مع الإصدار الجديد: BC
zipalign
. سيتم وضع هذه الوظيفة في حزمة منفصلة nelexa/apkfile
. قم بتحديث الإصدار الرئيسي في الملف composer.json
إلى ^3.0
.
{
"require" : {
"nelexa/zip" : " ^3.0 "
}
}
ثم قم بتثبيت التحديثات باستخدام Composer
:
composer update nelexa/zip
قم بتحديث الكود الخاص بك للعمل مع الإصدار الجديد:
ZipOutputFile
في ZipFile
وإزالتها.new PhpZipZipOutputFile()
إلى new PhpZipZipFile()
PhpZipZipFile::openFromFile($filename);
إلى (new PhpZipZipFile())->openFile($filename);
PhpZipZipOutputFile::openFromFile($filename);
إلى (new PhpZipZipFile())->openFile($filename);
PhpZipZipFile::openFromString($contents);
إلى (new PhpZipZipFile())->openFromString($contents);
PhpZipZipFile::openFromStream($stream);
إلى (new PhpZipZipFile())->openFromStream($stream);
PhpZipZipOutputFile::create()
إلى new PhpZipZipFile()
PhpZipZipOutputFile::openFromZipFile(PhpZipZipFile $zipFile)
> (new PhpZipZipFile())->openFile($filename);
addFromFile
إلى addFile
setLevel
إلى setCompressionLevel
ZipFile::setPassword
إلى ZipFile::withReadPassword
ZipOutputFile::setPassword
إلى ZipFile::withNewPassword
ZipOutputFile::disableEncryptionAllEntries
إلى ZipFile::withoutPassword
ZipOutputFile::setComment
إلى ZipFile::setArchiveComment
ZipFile::getComment
إلى ZipFile::getArchiveComment
addDir
و addFilesFromGlob
و addFilesFromRegex
.getLevel
setCompressionMethod
setEntryPassword