Welcome Guest, Not a member yet? Register   Sign In
Making a valid zip file
#1

[eluser]rschla104[/eluser]
Hello,

I'm relatively new to CI but so far I'm loving it.

I've run into a small problem though. I've got a system where a user can upload things to a folder I create for them. I also load some files and directories into that folder. In the db, I set their folder_path = ./folders/user01 . Now, I get to the point that I want to zip up everything in that folder into a zip file that I then want to give the user the option to download. Things get a little tricky here. For the next part, the zip file must contain only the files and directories inside the user01 folder and not the folder or any of it's parent directories. So here's what I did based on some other info I found on the forums (the code is edited as if I was only doing this for one user for simplicity's sake):

Code:
$currentdir = getcwd();            
chdir(getcwd() .'/folders/user01/');
$this->zip->read_dir('./', FALSE);
chdir($currentdir);
$dest = './folders/user01.zip';
$this->zip->archive($dest);

This is placing the zip file in the appropriate place and it has the right size, but it has some odd behavior. When I download it, if I double click on it, it doesn't show anything in it. If I use 7-Zip, I can extract from it and everything comes out alright and is structured the way I need it to be. If I try to use Windows to extract it, it blocks me from doing so saying this file is blocked.

I'm using CI 1.7.3 in a WAMP environment if that makes a difference. If anyone could point me in the right direction towards getting a standard zip file that Windows will allow me to extract, that would be great as I would really rather not be forced to direct my users to download a third party software.
#2

[eluser]rschla104[/eluser]
made some progress

This puts all of the files into the zip properly, but doesn't keep the structure

Code:
$this->load->library('zip');

$dest = './folders/user01.zip';

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(getcwd().'/folders/user01/'));

foreach ($iterator as $key=>$value) {
   $this->zip->read_file($key);
}

$this->zip->archive($dest);

and if I pass read_file($key, TRUE) it makes the zip invalid again.
#3

[eluser]rschla104[/eluser]
Finally got it:

Code:
$this->load->library('zip');
$this->load->helper('file');

$dest = './folders/user01.zip';

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(getcwd().'/folders/user01/'));

foreach ($iterator as $key=>$value) {
   $exploded = explode('user01\\',$key);
   $this->zip->add_data($exploded[1], read_file($key));
}

$this->zip->archive($dest);

Hope this helps someone. Would have saved me a few hours.




Theme © iAndrew 2016 - Forum software by © MyBB