ZipArchive not working, no errors

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

ZipArchive not working, no errors

Post by Skara »

I'm trying to zip a (very large) set of files, but nothing happens and I can't seem to get any errors to return.

The following returns a single error, every time: #6, at the end.

Each file exists, it's readable, ZipArchive::addFile returns true, $zipArchive close()s without fail, and yet the final zip file isn't created.

What gives?

** Running this on Windows Server 2012 R2 as a local SAN, if that matters.

Code: Select all

//exceptions, throws ErrorException
set_error_handler('exception_error');

try {
    // Copy directory to temporary location
    rcopy($src_location,$tmp_location);

    // Initialize archive object
    $zipArchive = new ZipArchive();
    $zipArchive->open($archive_filename, ZipArchive::CREATE | ZipArchive::OVERWRITE);

    // Create recursive directory iterator
    $files = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($tmp_location),
        RecursiveIteratorIterator::LEAVES_ONLY
    );

    foreach ($files as $name => $file) {
        // Skip directories (they would be added automatically)
        if (!$file->isDir()) {
            // Get real and relative path for current file
            $filePath = $file->getRealPath();
            $relativePath = substr($filePath, strlen($tmp_location) + 1);

            if (!file_exists($filePath)) {
                _log(1,'Uploading Archives [Compression Error #1]','File Not Found ['.$filePath.']');
                throw new Exception('Giving Up');
            }
            elseif (!is_readable($filePath)) {
                _log(1,'Uploading Archives [Compression Error #2]','File Not Readable ['.$filePath.']');
                throw new Exception('Giving Up');
            }
            else {
                // Add current file to archive
                if (!$zipArchive->addFile($filePath, $relativePath)) {
                    _log(1,'Uploading Archives [Compression Error #7]','File Not Added ['.$filePath.' | '.$relativePath.']');
                    throw new Exception('Giving Up');
                }
            }
        }
    }

    // Zip archive will be created only after closing object
    if (!$zipArchive->close()) {
        _log(1,'Uploading Archives [Compression Error #3]','Zip Not Created ['.$zipArchive->getStatusString().']');
    }
}
catch (ErrorException $e) {
    _log(1,'Uploading Archives [Copy/Compression Error #4]',(string)$e);
}
catch (Exception $e) {
    _log(1,'Uploading Archives [Copy/Compression Error #5]',(string)$e);
}
finally {
    // Clean Up Memory
    unset($files);
    unset($zipArchive);
}

// Final Check
if (!file_exists($archive_filename)) {
    _log(1,'Uploading Archives [Compression Error #6]','Error Creating Archive ['.$archive_filename.'] ('.$src_location.')');
}
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: ZipArchive not working, no errors

Post by Weirdan »

Are you sure the file does not get created, or you're just trusting file_exists() return value? file_exists() is affected by stat cache (you may need to call clearstatcache() before calling file_exists()). file_exists() also requires directory listing permissions on the folder that contains the file (something not every user has on some system directories like C:\Windows\Temp).

What is the return value of $zipArchive->open()?
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Re: ZipArchive not working, no errors

Post by Skara »

Hm. I'll check the output of $zipArchive->open(), but the file definitely isn't created. I check in an explorer window for congruity, etc. It just isn't there. That's interesting about clearstatcache() though.

The script copies the directory to be zipped to the same directory as it's creating the zip file, so no issues with writing or reading.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Re: ZipArchive not working, no errors

Post by Skara »

Finally found time to get back around to this project. It kills resources, so it only runs at night, limiting my bugtesting abilities.

$zipArchive->open() returns true (no errors).

I truly can't figure out what's going on. There are no errors with any part of ZipArchive, and yet after it's finished the zip file simply isn't there.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: ZipArchive not working, no errors

Post by Weirdan »

The file could possibly be saved in a so-called virtual store (C:\Users\[User_name]\AppData\Local\VirtualStore\[Path])
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Re: ZipArchive not working, no errors

Post by Skara »

Watching the script run...

The source directory is copied to a temporary directory "5.tmp/" at 5.12GB
Then 5.zip.a01612 is created. It begins at 0B, but fills up as the script runs... watching it reach 3GB... And then when it reaches completion (I'm assuming at ->close()), the file disappears. Poof.

[[ No idea about the a01612 extention, not my doing; that appears to be in the functionality of ZipArchive. ]]

Thoughts?
Last edited by Skara on Tue Jul 28, 2015 9:05 am, edited 1 time in total.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Re: ZipArchive not working, no errors

Post by Skara »

C:\Users\[User_name]\AppData\Local\VirtualStore\[Path]

VirtualStore doesn't exist.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: ZipArchive not working, no errors

Post by Weirdan »

Have you checked the windows event log?
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Re: ZipArchive not working, no errors

Post by Skara »

Hm. I skimmed through it, but I don't see anything relevant from today. I'm not sure what I should be looking for, though.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Re: ZipArchive not working, no errors

Post by Skara »

I should add:

I gave up on ZipArchive once and tried using PharData to do the same thing with a .tar.gz file. No matter what I did, I kept getting thrown an exception that said it couldn't add a file "seek failed." Only found one other report of this error when trying to compress a large number of files. I wonder if there's not some weird permissions error going on, that PHP can read/write but somehow its extensions have issues? If that makes sense. Maybe it doesn't.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Re: ZipArchive not working, no errors

Post by Skara »

Blah. Giving up. I partially have PharData working now. Starting a new thread since I have an entirely new problem (probably).
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: ZipArchive not working, no errors

Post by Weirdan »

Something I didn't know about zip: specification (§4.4.8) says size fields (both compressed and uncompressed) are 4 bytes long - meaning it does not support file sizes above 4gb. Are you trying to create files larger than that? Are other implementations (like winzip) able to compress your data?
Post Reply