Ne-Lexa / php-zip

PhpZip is a php-library for extended work with ZIP-archives.
MIT License
489 stars 60 forks source link

Exctracted files are empty #6

Open uvo opened 6 years ago

uvo commented 6 years ago

Hi I'm using PhpZip 3.0.0 (2017-03-15) with a password to generate a zip. After extracting all files (images) have zero bytes. Working without a pw everything works fine.

My code to generate the zip-file: $zipFile ->withNewPassword( $password ) ->addDir( $dirToZip ) ->saveAsFile( $outputFilenamePwPotected ) ->close();

The code to extract: $zipFile ->openFile( $outputFilenamePwPotected ->withReadPassword($password) //->withoutPassword()// => no changes if not commented ->extractTo( $directory );

All files will be listed ($zipFile->getListFiles()) and counted ($zipFile->count()) correct but have zero bytes. Is there a option I'm missing? Thank you.

Ne-Lexa commented 6 years ago

Try to update the library to version 3.0.2. I tested the code and it's working. Specify your version of php and bit depth.

Try to run this code.

<?php
require "vendor/autoload.php";

$zipFile = new \PhpZip\ZipFile();
$password = '9ezR2tDVZpifnaKt2uvL';
$outputFilenamePwProtected = 'zip_protected.zip';
$zipFile
    ->withNewPassword($password)
    ->addDir(__DIR__)
    ->saveAsFile($outputFilenamePwProtected);
$zipFile->close();

$directory = sys_get_temp_dir() . '/zip-out';
if (!is_dir($directory)) {
    mkdir($directory, 0755, true);
}
$zipFile
    ->openFile($outputFilenamePwProtected)
    ->withReadPassword($password)
    ->extractTo($directory)
    ->close();
uvo commented 6 years ago

Hi thank you for the response and the example code and sorry for my late response.

I'm working on a MAC (OS Sierra) with MAMP Pro (php 5.6.10, 64bit, openssl/mycrypt enabled).

After updating the library to version 3.0.2 I tested your code and I run into an other problem: PHP Warning: openssl_encrypt(): Unknown cipher algorithm in .../vendor/nelexa/zip/src/PhpZip/Crypto/WinZipAesEngine.php on line 176

The solution was to change the encryption method. The working code is:

$zipFile = new \PhpZip\ZipFile(); $dirToZip = '../app/uploads/img/'; $outputFilenamePwProtected = '../tmp/zip_protected_example.zip'; $password = '9ezR2tDVZpifnaKt2uvL'; $encryptionMethod = \PhpZip\ZipFile::ENCRYPTION_METHOD_TRADITIONAL;

$zipFile ->withNewPassword($password, $encryptionMethod) ->addDir($dirToZip) ->saveAsFile($outputFilenamePwProtected); $zipFile->close();

if (!is_dir($directory)) { mkdir($directory, 0755, true); }

$zipFile ->openFile($outputFilenamePwProtected) ->withReadPassword($password) ->extractTo($directory); $zipFile->close();

Many thank for your support!

Ne-Lexa commented 6 years ago

PHP Warning: openssl_encrypt(): Unknown cipher algorithm in .../vendor/nelexa/zip/src/PhpZip/Crypto/WinZipAesEngine.php on line 176

This indicates that your php-extension openssl does not support the AES-256-CTR encryption method. A list of supported encryption methods can be found using the function openssl_get_cipher_methods ().

The following releases will check for support for this method and if it is not there will be an attempt to use mcrypt or an exception will be thrown.