Hamboy75
Hamboy75

Reputation: 1079

Concurrent writing to zip file

I have a process done in PHP. This process get a file from internet and put it inside a zip file. The target zipfile is based in an algorithm, there are 4096 zipfiles. The target zipfile is based in a hash of the url processed.

I have another program that launches http petitions so i can run the script concurrently (around 110 processes).

My question is simple. Since threads are pseudorandom, easly 2 threads can try to add files to the same zipfile in the same moment.

Is it possible? Will the file get corrupt if 2 proccess try to add files at same time?

Locking the file or something like that would be possible a possible solution. I was thinking to use semaphores, but reading, php semaphores dont work under windows.

I have seen this possible solution:

if ( !function_exists('sem_get') ) { 
    function sem_get($key) { return fopen(__FILE__.'.sem.'.$key, 'w+'); } 
    function sem_acquire($sem_id) { return flock($sem_id, LOCK_EX); } 
    function sem_release($sem_id) { return flock($sem_id, LOCK_UN); } 
}

Anyways the question is if it is allowed to add files to a zip file from 2 or more different php proccesses at same time.

Upvotes: 0

Views: 737

Answers (1)

Ben Hillier
Ben Hillier

Reputation: 2104

Short answer: No! The zip algorithm analyses and compresses one stream at a time.

This is tough under Windows. It's far from easy in Linux! I would be tempted to create a db table with a unique index, and use that index number to determine a filename, or at least flag that a file is being written to.

Upvotes: 1

Related Questions