Zip class

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Sorry to bump an old thread back, but I need a gzip/bz2 class that could EXTRACT tar files. Do you know where can I get one? I searched on hotscripts but most of the scripts there seem to be properiarity licsenced...
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Just use the command line


tar -xf file.tar, creates the directory ./file/
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

<yoda_off>
Let me start by saying I almost NEVER, EVER, EVER use other people's code, primarily because it doesn't do exactly what I want and secondarily because I like to learn why it's doing by making it myself.

However, I just used this zip class and it's flawless, so much so that I don't want to bother learning how / why it works. I think I'll keep it in my back pocket and pretend it's a built in PHP function :P

very nice work RM.
</yoda_off>
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Burrito wrote:However, I just used this zip class and it's flawless, so much so that I don't want to bother learning how / why it works. I think I'll keep it in my back pocket and pretend it's a built in PHP function :P
And now you know why the rest of us do that. :)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Roja wrote:And now you know why the rest of us do that. :)
:?:
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Burrito wrote:
Roja wrote:And now you know why the rest of us do that. :)
:?:
I was referring to why the rest of us *do* use packaged, proven solutions, instead of developing our own. You mentioned that you didn't do so, because you wanted to learn. But in this case, the packaged version was nearly perfect for your needs - which is a reason many others *do* use packaged solutions.

Does that explain my comment better?
Hwange
Forum Newbie
Posts: 5
Joined: Sun Mar 19, 2006 7:31 pm

How to add current foldername to path

Post by Hwange »

I must agree this is an awsome script that works with no alterations needed to run.

I would ask however if anyone can explain a little thing.

Using the example "zipcreate.ex3.php" works like a charm except I would like the zip path to include the name of the actual folder being zipped.

it's easier this way cos I can select multiple zip files when I want to unzip them and they'll go back as they were originally

in case this is a little confusing:-

e.g.
folder1\subfolder\file.jpg

will only unzip to

subfolder\file.jpg

I would like it to unzip back yo

folder1\subfolder\file.jpg

Cheers all
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Two options....

Either create another directory and copy or move folder1 and it's contents into the new directory and then use the third example to to zip the contents of your newly created directory.

Or add a 'prefix' variable and prepend that to $name when adding to the zip file. i.e. ...

after defining $zip_dir within the script add the following...

Code: Select all

$prefix  = basename($zip_dir) . '/';
then find the following line...

Code: Select all

$ZIP->add_file($contents, $name, filemtime($name));
and replace with....

Code: Select all

$ZIP->add_file($contents, $prefix . $name, filemtime($name));
and also find the following line...

Code: Select all

$ZIP->add_file('', $name, filemtime($name));
and replace with with...

Code: Select all

$ZIP->add_file('', $prefix . $name, filemtime($name));
Untested but should do what you want.

For what it's worth, I'm currently contemplating extending the class to add easy access to this and similar type functions.

If you are dynamically creating the entire directory contents, I'd go with the first option. If however the majority of your directory structure is static than I'd be more inclined to go for the second option.
Hwange
Forum Newbie
Posts: 5
Joined: Sun Mar 19, 2006 7:31 pm

Post by Hwange »

Thanks I'll give it a go.

My file structure is static and the path and foldernames are generated from MySQL

e.g

After a query to get what I want from my album database I link to your page like so.

Code: Select all

if (is_dir("$folderpath/$row[1]"))      
{$icon = "<img border='0' src='images/folder.ico'>&nbsp;"
              ."<a href='zipcreate.ex3.php?dvddir=$folderpath&albumname=$row[1]'>zipit</a>";}
I then use the values to fullfil your $zip_dir and $zipname

The reason I am going to such lengths is that I have over 5,000 albums neatly tagged and sorted into folders with the album name. I want to zip them into individual zip's named after the album (Folder their in)

Rather than right click and add to <foldername>.zip individually taking a LONG time I'd try to use this code to automate it a little. Well it's good practice for a php noobie anyways.

P.S. is there anything to stop the page timing out, it seems that if it takes too long to carry out the zip process the zip sometimes isn't created. After the statusbar loads 50 - 75 percent it jumps to the end.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Hwange wrote:P.S. is there anything to stop the page timing out, it seems that if it takes too long to carry out the zip process the zip sometimes isn't created. After the statusbar loads 50 - 75 percent it jumps to the end.
Well, there are three potential problems you might encounter here....

PHP may be timing out, you can get round that with set_time_limit()

Your webserver may be timing out, you need to look at your webservers config to adjust that.

PHP may be running out of memory.

By the sounds of it you are running this on your local machine? in which case, I'd be more inclined to script up something to run from the command line.
Hwange
Forum Newbie
Posts: 5
Joined: Sun Mar 19, 2006 7:31 pm

Post by Hwange »

Yes it's on a local server using apache.

Again like I said it's good practice to learn how PHP works.

Not clever enough to run up a script but if you can point me in any direction to find out some info I'd be greatful.

I emailed winzip yesterday asking if there is a way to select multiple folders, right click, and zip to individual zips with their foldernames rather than the only current option of zipping them all to the master folder but they just said.
Thank you for contacting us.

We at WinZip Computing occasionally receive this request. However, to
date we have not received enough user requests to warrant adding this to
the program. I will forward your vote for this feature to development.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

I'd say most of what you need to do the job is included in the example scripts (specifically the script you are already using (ex3)), it should just be a case of breaking them down to understand what they are doing. And from there just adding a few simple rountines on top.
Hwange
Forum Newbie
Posts: 5
Joined: Sun Mar 19, 2006 7:31 pm

Post by Hwange »

Thanks redmonkey

added

Code: Select all

$prefix  = basename($zip_dir) . '/';
Then changed the 3 lines of code and it worked straight away.

Only one problem, I have folders inside too e.g.

CD1\<tracks>
CD2\<tracks>

They were also zipped perfectly but for some reason when i tested the

"Right click > extract here"

it created duplicate empty folders CD1 and CD2 alongside the main album folder.

any ideas

Thanks again for all your time and support

P.S (Added Later)

If I open this zip with winzip the extra CD1 and CD2 folders only show up when viewed "Explorer style" but if I try to delete the folder from inside winzip it says it can't find it.

Thought it might help, if you are going to add further functions to this script in the future.
Hwange
Forum Newbie
Posts: 5
Joined: Sun Mar 19, 2006 7:31 pm

Post by Hwange »

Just trial and error but this done the trick

Changed

Code: Select all

return array($files, $directories);
To

Code: Select all

return array($files);
Any other implication or problems i might be causing by doing this?
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

Post by Sinemacula »

Glad I was pointed to your script -- even I was able to get it to work right away, zipping up a directory outside of the webspace with just minimal changes to zipcreate.ex3.php

I do have a couple of questions:

1. Is it possible to add the date to the $zipname? I've tried a couple of different things, but it doesn't seem to like having another variable in the definition of $zipname.

2. I'd like to go from zipping the directory to downloading the zipped file -- basically wanting to incorporate the end of zipcreate.ex2.php with zipcreate.ex3.php - but I haven't been able to figure out which variables I should use in place of $zipfile (which I assume I should do, since in ex2 it's defined by the building of the zip file, which is already done).

Any suggestions you could offer would be appreciated.

Thanks,
Scott
Post Reply