Archive::Zip on windows

Discussion in 'Perl Misc' started by ebm, Dec 14, 2007.

  1. ebm

    ebm Guest

    I'm trying to have Archive::Zip zip a file on a windows systems. When
    I create this file using the full path I end up with an empty zip
    file.
    Example:
    use Archive::Zip qw( :ERROR_CODES :CONSTANTS );

    my $file = 'c:/TEST.xls';
    my $zip = Archive::Zip->new();
    print STDOUT "Adding $file\n";

    $zip->addFile($file) or warn "Error adding file $file\n";
    die "write error." if $zip->writeToFileNamed ("c:/file.zip") !=
    AZ_OK;

    __END__

    I will end up with c:/file.zip but it will be empty. Now If I copy
    the xls file to the same directory the script is running in it will be
    zipped into c:/file.zip. It seems to be something to do with the C:\
    part of the path it doesn't like. The pod file says something about
    using Unix file formats..... Am i screwed or is there a way around
    this.

    Is there a way around this problem. This is apart of a larger script
    so I can't hard code any file names. Any ideas?
     
    ebm, Dec 14, 2007
    #1
    1. Advertisements

  2. ebm

    Ben Morrow Guest

    Quoth ebm <>:
    > I'm trying to have Archive::Zip zip a file on a windows systems. When
    > I create this file using the full path I end up with an empty zip
    > file.
    > Example:
    > use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
    >
    > my $file = 'c:/TEST.xls';
    > my $zip = Archive::Zip->new();
    > print STDOUT "Adding $file\n";
    >
    > $zip->addFile($file) or warn "Error adding file $file\n";
    > die "write error." if $zip->writeToFileNamed ("c:/file.zip") !=
    > AZ_OK;
    >
    > __END__
    >
    > I will end up with c:/file.zip but it will be empty. Now If I copy
    > the xls file to the same directory the script is running in it will be
    > zipped into c:/file.zip. It seems to be something to do with the C:\
    > part of the path it doesn't like. The pod file says something about
    > using Unix file formats..... Am i screwed or is there a way around
    > this.


    Try specifying a separate path to store the file as in the zip: a zip
    member cannot have a volume specification. So:

    $zip->addFile($file, 'TEST.xls');

    or use File::Spec to split up the path and join it back together, sans
    volume, as a Unix filespec.

    Ben
     
    Ben Morrow, Dec 15, 2007
    #2
    1. Advertisements

  3. ebm

    Ron Bergin Guest

    On Dec 14, 5:12 pm, Ben Morrow <> wrote:
    > Quoth ebm <>:
    >
    >
    >
    > > I'm trying to have Archive::Zip zip a file on a windows systems. When
    > > I create this file using the full path I end up with an empty zip
    > > file.
    > > Example:
    > > use Archive::Zip qw( :ERROR_CODES :CONSTANTS );

    >
    > > my $file = 'c:/TEST.xls';
    > > my $zip = Archive::Zip->new();
    > > print STDOUT "Adding $file\n";

    >
    > > $zip->addFile($file) or warn "Error adding file $file\n";
    > > die "write error." if $zip->writeToFileNamed ("c:/file.zip") !=
    > > AZ_OK;

    >
    > > __END__

    >
    > > I will end up with c:/file.zip but it will be empty. Now If I copy
    > > the xls file to the same directory the script is running in it will be
    > > zipped into c:/file.zip. It seems to be something to do with the C:\
    > > part of the path it doesn't like. The pod file says something about
    > > using Unix file formats..... Am i screwed or is there a way around
    > > this.

    I tested your code as well as a couple slight variations and they all
    worked for me.
    >
    > Try specifying a separate path to store the file as in the zip: a zip
    > member cannot have a volume specification. So:

    That is not correct. The volume specification will be stripped from
    the first parameter, but if you supply it in the optional second
    parameter, it will be retained in the zip.
    >
    > $zip->addFile($file, 'TEST.xls');
    >
    > or use File::Spec to split up the path and join it back together, sans
    > volume, as a Unix filespec.
    >

    The following test script worked for me and retained the volume spec
    in the zip, without the need to use File::Spec.

    The script was ran from and executed as D:/zipme.pl

    use strict;
    use warnings;
    use Archive::Zip qw( :ERROR_CODES :CONSTANTS );

    my $file = 'c:/testing/test.pl';
    print "Adding $file\n";

    my $zip = Archive::Zip->new();

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

    die "write error." if $zip->writeToFileNamed("d:/temp/file.zip") !=
    AZ_OK;
     
    Ron Bergin, Dec 15, 2007
    #3
  4. ebm

    Ben Morrow Guest

    Quoth Ron Bergin <>:
    > On Dec 14, 5:12 pm, Ben Morrow <> wrote:
    > >
    > > Try specifying a separate path to store the file as in the zip: a zip
    > > member cannot have a volume specification. So:

    > That is not correct. The volume specification will be stripped from
    > the first parameter, but if you supply it in the optional second
    > parameter, it will be retained in the zip.


    It's not a volume spec in that case, as zips use Unix filespecs. It's
    just a directory called 'c:'. I've no idea what would happen if you
    tried to extract such a zip under Win32.

    Ben
     
    Ben Morrow, Dec 15, 2007
    #4
  5. On Sat, 15 Dec 2007 12:02:04 +0000, Ben Morrow <>
    wrote:

    >It's not a volume spec in that case, as zips use Unix filespecs. It's
    >just a directory called 'c:'. I've no idea what would happen if you
    >tried to extract such a zip under Win32.


    C:\temp>zipme.pl c:\temp\win0.pl
    Adding c:\temp\win0.pl

    C:\temp>del win0.pl

    C:\temp>unzip file.zip
    Archive: file.zip
    inflating: c_/temp/win0.pl

    (With minimal modifications to zipme.pl and InfoZIP's unzip.)


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Dec 16, 2007
    #5
  6. ebm

    ebm Guest

    On Dec 14, 5:12 pm, Ben Morrow <> wrote:
    > Quothebm<>:
    >
    >
    >
    > > I'm trying to have Archive::Zip zip a file on a windows systems. When
    > > I create this file using the full path I end up with an empty zip
    > > file.
    > > Example:
    > > use Archive::Zip qw( :ERROR_CODES :CONSTANTS );

    >
    > > my $file = 'c:/TEST.xls';
    > > my $zip = Archive::Zip->new();
    > > print STDOUT "Adding $file\n";

    >
    > > $zip->addFile($file) or warn "Error adding file $file\n";
    > > die "write error." if $zip->writeToFileNamed ("c:/file.zip") !=
    > > AZ_OK;

    >
    > > __END__

    >
    > > I will end up with c:/file.zip but it will be empty. Now If I copy
    > > the xls file to the same directory the script is running in it will be
    > > zipped into c:/file.zip. It seems to be something to do with the C:\
    > > part of the path it doesn't like. The pod file says something about
    > > using Unix file formats..... Am i screwed or is there a way around
    > > this.

    >
    > Try specifying a separate path to store the file as in the zip: a zip
    > member cannot have a volume specification. So:
    >
    > $zip->addFile($file, 'TEST.xls');
    >
    > or use File::Spec to split up the path and join it back together, sans
    > volume, as a Unix filespec.
    >
    > Ben


    Thanks for the help, I'll give it a try. I ended up doing a work
    around where I copy the file to the same dir as the script. Zip the
    cached file I just copied and output the the desired location.
     
    ebm, Dec 17, 2007
    #6
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Graham Wood
    Replies:
    3
    Views:
    733
    A. Sinan Unur
    Jan 11, 2004
  2. Twig
    Replies:
    1
    Views:
    187
    A. Sinan Unur
    Jan 27, 2006
  3. MoshiachNow
    Replies:
    2
    Views:
    344
    Ilya Zakharevich
    Oct 4, 2006
  4. MoshiachNow

    Archive::Zip - zip file has "invalid" format

    MoshiachNow, Oct 5, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    231
  5. Bo Yang
    Replies:
    9
    Views:
    409
    -berlin.de
    Nov 20, 2006
Loading...

Share This Page