| 1 | package IO::Uncompress::Untar; | ||
| 2 | |||
| 3 | require 5.006; | ||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use IO::File; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Archive::Tar::Stream; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ; | ||
| 9 | |||
| 10 | require Exporter; | ||
| 11 | |||
| 12 | our @ISA = qw(Exporter); | ||
| 13 | our($VERSION)='1.0'; | ||
| 14 | our($UntarError) = ''; | ||
| 15 | |||
| 16 | our %EXPORT_TAGS = ( 'all' => [ qw( $UntarError ) ] ); | ||
| 17 | |||
| 18 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 19 | |||
| 20 | our @EXPORT = qw( ); | ||
| 21 | |||
| 22 | |||
| 23 | sub new { | ||
| 24 | my $class = shift; | ||
| 25 | my $this={}; | ||
| 26 | $this->{handle}=shift; | ||
| 27 | $this->{z} = new IO::Uncompress::AnyUncompress $this->{handle}; | ||
| 28 | $this->{ts} = Archive::Tar::Stream->new(infh => $this->{z}); $this->{ts}->SafeCopy(0); | ||
| 29 | $this->{opt}=shift; | ||
| 30 | $this->{raw}=''; | ||
| 31 | $this->{header}={}; | ||
| 32 | $this->{loc}=0; | ||
| 33 | $this->{i}=0; | ||
| 34 | $this->{readoffset}=0; | ||
| 35 | bless $this,$class; | ||
| 36 | return $this; | ||
| 37 | } # new | ||
| 38 | |||
| 39 | |||
| 40 | sub nextStream { | ||
| 41 | my $this = shift ; | ||
| 42 | $this->{readoffset}=0; | ||
| 43 | if(!$this->{loc}) { | ||
| 44 | $this->{header}=$this->{ts}->ReadHeader(); | ||
| 45 | $this->{loc}+=512; | ||
| 46 | } else { | ||
| 47 | while(($this->{header}->{size} > $this->{i} )&&(defined $this->{raw})) { | ||
| 48 | my $blks=int(($this->{header}->{size}-$this->{i}-1)/512)+1; | ||
| 49 | $blks=1602 if($blks>1602); | ||
| 50 | $this->{raw} = $this->{ts}->ReadBlocks($blks); | ||
| 51 | $this->{i}+=$blks*512; | ||
| 52 | $this->{loc}+=$blks*512; | ||
| 53 | } | ||
| 54 | $this->{header}=$this->{ts}->ReadHeader(); | ||
| 55 | $this->{loc}+=512; $this->{i}=0; | ||
| 56 | } | ||
| 57 | |||
| 58 | return 0 if(!((defined $this->{raw})&&(ref $this->{header}))); | ||
| 59 | return 1; | ||
| 60 | } # nextStream | ||
| 61 | |||
| 62 | |||
| 63 | sub getHeaderInfo { | ||
| 64 | my $this = shift ; | ||
| 65 | $this->nextStream() unless($this->{loc}); | ||
| 66 | return undef if(!ref $this->{header}); | ||
| 67 | $this->{header}->{UncompressedLength}=$this->{header}->{size}; | ||
| 68 | $this->{header}->{Name}=$this->{header}->{name}; | ||
| 69 | $this->{header}->{Time}=$this->{header}->{mtime}; | ||
| 70 | return $this->{header}; | ||
| 71 | } # getHeaderInfo | ||
| 72 | |||
| 73 | |||
| 74 | sub read { | ||
| 75 | my $this = shift; | ||
| 76 | my $bytes = $_[1] || 512*1600; | ||
| 77 | ++$this->{rec}; # debugging - block accidental recursion | ||
| 78 | #warn "$this $bytes r=" . $this->{rec}; | ||
| 79 | my $offset = $_[2]; | ||
| 80 | die "non zero offset not implimented" if($offset); | ||
| 81 | my $maxleft=$this->{header}->{size}-$this->{i}; | ||
| 82 | $bytes=$maxleft if($bytes>$maxleft); | ||
| 83 | if((!defined $this->{raw})||($bytes>length($this->{raw}))) { | ||
| 84 | my $blks=int(($bytes-length( $this->{raw} )-1 )/512)+1; | ||
| 85 | $this->{raw}.=$this->{ts}->ReadBlocks($blks) if($this->{rec}<2); | ||
| 86 | warn "Blocked recursion $this->{rec}" if($this->{rec}>1); | ||
| 87 | $this->{i}+=$blks*512; $this->{loc}+=$blks*512; | ||
| 88 | } | ||
| 89 | --$this->{rec}; | ||
| 90 | $_[0]=substr($this->{raw},$this->{readoffset},$bytes); | ||
| 91 | $this->{raw}=substr($this->{raw},$bytes); | ||
| 92 | #$this->{readoffset}+=$bytes; | ||
| 93 | #warn "$this got=" . length($_[0]); | ||
| 94 | return length($_[0]); | ||
| 95 | } # read | ||
| 96 | |||
| 97 | sub close { | ||
| 98 | my $this = shift; | ||
| 99 | # $this->{ts}->close(); | ||
| 100 | $this->{z}->close(); | ||
| 101 | } | ||
| 102 | |||
| 103 | 1; | ||
| 104 | |||
| 105 | __END__ | ||