use Archive::Zip - Dont store full file path?

Posted on 2006-05-24
Last Modified: 2013-12-25
Hi there,

I am creating dynamic zip files on the server using Archive::Zip.

Now I have 2 problems, firstly the zip file containts full directory structure of the server, as in, /home/vhosts/httpd/ etc etc

Can I have it so just the files are stored rather than the directory structure?

I am using:

    $zip->addFile( $zipperFile )
               or warn "Error adding file $fileToAdd\n";

Also, the files in the above example are named pv_$fileID.mp3, but to make more sense, I would like to rename them to a field out of the database, so is there a way of saying "add this file, but call it this..." type command?

Any help appreciated!!

Question by:scooter41
    LVL 19

    Expert Comment

    It looks like you are creating the full file path, but the variable $fileID is not getting substituted in the name correctly.

    It's easy to just use the file name. If $fileID is unique (I'm guessing it should be), just use this:

    $zipperFile= 'pv_' . $fileID' . '.mp3';  # $fileID is unqiue combo like 1234
    $zip->addFile( $zipperFile )             # add file pb_1234.mp3 to the archive
        or warn "Error adding file $fileToAdd\n";

    Author Comment

    Hey teraplane, thanks for your reply,

    The actual script above works fine, there wasnt a problem with it, it does store, PV_1000054.mp3 for example, but the actual title of that file is Every_Body_Say_Yeah which would be a lot easier if I could add PV_10000054.mpe to the zip file, but rename it Every_Body_Say_Yeah.mp3 to the zip file.

    That is what I am trying to do.

    I guess with directories I could make it a local path and see what happens, I just thought there may be a tag that doesnt include the directories.
    LVL 19

    Accepted Solution

    Actually that is easy to do as well, supply a second parameter to addFile, and it will use that name in the archive, rather than actual file name.

    $zip->addFile( $zipperFile,"Every_Body_Say_Yeah.mp3" );

    Author Comment

    hey guess what, that worked perfectly, plus by specifying the name of the file, stripped the path data out too, mission successfull!

    Thanks heaps!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    In this tutorial I will aim to show you how simple is making a small application in WhizBase, how to add, remove and update data in the DB. I will make a small address book application where you can add, browse, update and remove addresses. I wi…
    It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
    The viewer will learn how to count occurrences of each item in an array.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now