Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

unzipping unix files on windows

by spikey_wan (Scribe)
on Jul 02, 2004 at 10:25 UTC ( [id://371351]=perlquestion: print w/replies, xml ) Need Help??

spikey_wan has asked for the wisdom of the Perl Monks concerning the following question:

Hello World!,

I am running on a w2k system, but need to be able to unzip text files that came from a unix system, and have been compressed there. i.e. file.txt.Z type files.

I have tried:

use Archive::Zip qw(AZ_OK); my $zip = Archive::Zip->new(); unless ($zip -> read($zipfile) == AZ_OK) { $status -> insert ('end', "Can not read file $zipfile!\nExiting!\n" +); ... }
But it can't handle the .Z files. Is there another way, or module that is capable of doing this?

Thanks,
Spike.

Replies are listed 'Best First'.
Re: unzipping unix files on windows
by zentara (Archbishop) on Jul 02, 2004 at 12:12 UTC
    .Z files are NOT zip files, they are made with compress. Read "man compress". You probably want the Compress::Zlib module.

    I'm not really a human, but I play one on earth. flash japh
Re: unzipping unix files on windows
by wufnik (Friar) on Jul 02, 2004 at 14:15 UTC
    here is one way to do it. tested for .gz files. should also work for .Z files.

    source: adapted from the compress::zlib example code.

    use Compress::Zlib; my $fil = shift || die ("need compressed file, stopped"); die "$fil not a gz'd file, stopped" unless (($fil =~ /.gz$/) || ($fil =~ /.Z$/)); (my $rootfil = $fil) =~ s/.gz$//; open OOTFIL, ">$rootfil" or die ("cannot open file for gunzip"); binmode OOTFIL or die ("cannot binmode $rootfil"); $gz = gzopen($fil, "rb") or die ("can't open $fil, $gzerrno, stopped") +; my $buffer; print OOTFIL $buffer while $gz->gzread($buffer) > 0; die "Error reading from file $file: $gzerrno\n" if $gzerrno != Z_STREA +M_END; $gz->gzclose(); close(OOTFIL);
    hasta la vista
    ...wufnik

    -- in the world of the mules there are no rules --
      Thanks for your help.

      Unfortunately, when I tried your code, it wouldn't do a .Z file. I get "Error reading from file...".

Re: unzipping unix files on windows
by nimdokk (Vicar) on Jul 02, 2004 at 12:20 UTC
    As zentara indicates, these are not Zip files, and cannot be unzipped (unless you happen to be using WinZip). The options would be to use the module mentioned (Compress::Zlib) or zip the files using a true zip program (such as 'gzip').

      The UNIX compress program uses a quite different implementation of the compression techniques used by zlib and gzip, so I'm not sure whether Compress::Zlib will be able to read .Z files.

      Anyway, keep in mind that there are several different compression methods, and that the one that people usually refer to as 'zip' refers to PKZip, which is compatible with WinZip and InfoZip.

      gzip and PKZip/WinZip/InfoZip use very different compression methods (just to say one difference, gzip can compress only one file, while the others can create compressed archives of several files), so compression and decompression programs will have to be paired. For example, gzip on one end and Compress::Zlib on the other.

      -- 
              dakkar - Mobilis in mobile
      

      Most of my code is tested...

      Perl is strongly typed, it just has very few types (Dan)

        Actually, I believe that gzip can uncompress .Z files but it cannot compress files into .Z format for licensing reasons or some such. So you may be good to go with that.

        --
        tbone1, YAPS (Yet Another Perl Schlub)
        And remember, if he succeeds, so what.
        - Chick McGee

        Actually, zip and gzip use the same compression algorithm, deflate. Deflate is a variant of LZ77, and is used in other file formats (PNG, PDF). The file formats are different, with gzip compressing a single file, and zip making an archive with multiple files.
Re: unzipping unix files on windows
by BrowserUk (Patriarch) on Jul 02, 2004 at 21:08 UTC

    Get yourself UnixUtils and use its compress.exe and a forked open.

    perl -e"open IN, 'compress.exe -dc file.z |' or die $!; my @data = <IN +>"

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
Re: unzipping unix files on windows
by idsfa (Vicar) on Jul 02, 2004 at 19:17 UTC

    Just to go completely the other direction, you could also ZIP (rather than compress) the test files on the Unix box. The lazy way would be to use the 'jar' utility (if java is installed).

    $ jar cf result.zip file1.txt file2.txt ...

    The perlish way would be to use a utility like the following to make the zip file ...

    my $zipfile = shift @ARGV; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); my $zip = Archive::Zip->new(); my $member = $zip->addFile( @ARGV ); die 'write error' unless $zip->writeToFileNamed( $zipfile ) == AZ_OK;

    If anyone needs me I'll be in the Angry Dome.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://371351]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-25 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found