1 package Archive::Zip::MemberRead;
3 # Copyright (c) 2002 Sreeji K. Das. All rights reserved. This program is free
4 # software; you can redistribute it and/or modify it under the same terms
9 Archive::Zip::MemberRead - A wrapper that lets you read Zip archive members as if they were files.
16 use Archive::Zip::MemberRead;
17 $zip = new Archive::Zip("file.zip");
18 $fh = new Archive::Zip::MemberRead($zip, "subdir/abc.txt");
19 while (defined($line = $fh->getline()))
21 print $fh->input_line_number . "#: $line\n";
24 $read = $fh->read($buffer, 32*1024);
25 print "Read $read bytes as :$buffer:\n";
29 The Archive::Zip::MemberRead module lets you read Zip archive member data
30 just like you read data from files.
39 use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
41 use vars qw{$VERSION};
45 $VERSION = eval $VERSION;
48 =item Archive::Zip::Member::readFileHandle()
50 You can get a C<Archive::Zip::MemberRead> from an archive member by
51 calling C<readFileHandle()>:
53 my $member = $zip->memberNamed('abc/def.c');
54 my $fh = $member->readFileHandle();
55 while (defined($line = $fh->getline()))
63 sub Archive::Zip::Member::readFileHandle {
64 return Archive::Zip::MemberRead->new( shift() );
67 =item Archive::Zip::MemberRead->new($zip, $fileName)
69 =item Archive::Zip::MemberRead->new($zip, $member)
71 =item Archive::Zip::MemberRead->new($member)
73 Construct a new Archive::Zip::MemberRead on the specified member.
75 my $fh = Archive::Zip::MemberRead->new($zip, 'fred.c')
80 my ( $class, $zip, $file ) = @_;
81 my ( $self, $member );
83 if ( $zip && $file ) # zip and filename, or zip and member
85 $member = ref($file) ? $file : $zip->memberNamed($file);
87 elsif ( $zip && !$file && ref($zip) ) # just member
93 'Archive::Zip::MemberRead::new needs a zip and filename, zip and member, or member'
98 bless( $self, $class );
99 $self->set_member($member);
104 my ( $self, $member ) = @_;
106 $self->{member} = $member;
107 $self->set_compression(COMPRESSION_STORED);
111 sub set_compression {
112 my ( $self, $compression ) = @_;
113 $self->{member}->desiredCompressionMethod($compression) if $self->{member};
118 Rewinds an C<Archive::Zip::MemberRead> so that you can read from it again
119 starting at the beginning.
126 $self->_reset_vars();
127 $self->{member}->rewindData() if $self->{member};
133 $self->{partial} = 0;
134 $self->{line_no} = 0;
137 =item input_line_number()
139 Returns the current line number, but only if you're using C<getline()>.
140 Using C<read()> will not update the line number.
144 sub input_line_number {
146 return $self->{line_no};
151 Closes the given file handle.
158 $self->_reset_vars();
159 $self->{member}->endRead();
162 =item buffer_size([ $size ])
164 Gets or sets the buffer size used for reads.
165 Default is the chunk size used by Archive::Zip.
170 my ( $self, $size ) = @_;
173 return $self->{chunkSize} || Archive::Zip::chunkSize();
176 $self->{chunkSize} = $size;
182 Returns the next line from the currently open member.
183 Makes sense only for text files.
184 A read error is considered fatal enough to die.
185 Returns undef on eof. All subsequent calls would return undef,
186 unless a rewind() is called.
187 Note: The line returned has the newline removed.
191 # $self->{partial} flags whether the last line in the buffer is partial or not.
192 # A line is treated as partial if it does not ends with \n
195 my ( $temp, $status, $size, $buffer, @lines );
198 $size = $self->buffer_size();
200 while ( $$temp !~ /\n/ && $status != AZ_STREAM_END ) {
201 ( $temp, $status ) = $self->{member}->readChunk($size);
202 if ( $status != AZ_OK && $status != AZ_STREAM_END ) {
203 die "ERROR: Error reading chunk from archive - $status\n";
209 @lines = split( /\n/, $buffer );
211 if ( $#lines == -1 ) {
212 return ( $#{ $self->{lines} } == -1 )
214 : shift( @{ $self->{lines} } );
217 $self->{lines}->[ $#{ $self->{lines} } ] .= shift(@lines)
220 splice( @{ $self->{lines} }, @{ $self->{lines} }, 0, @lines );
221 $self->{partial} = !( $buffer =~ /\n$/ );
222 return shift( @{ $self->{lines} } );
225 =item read($buffer, $num_bytes_to_read)
227 Simulates a normal C<read()> system call.
228 Returns the no. of bytes read. C<undef> on error, 0 on eof, I<e.g.>:
230 $fh = new Archive::Zip::MemberRead($zip, "sreeji/secrets.bin");
233 $read = $fh->read($buffer, 1024);
234 die "FATAL ERROR reading my secrets !\n" if (!defined($read));
243 # All these $_ are required to emulate read().
248 my ( $temp, $status, $ret );
250 ( $temp, $status ) = $self->{member}->readChunk($size);
251 if ( $status != AZ_OK && $status != AZ_STREAM_END ) {
257 $ret = length($$temp);
268 Sreeji K. Das, <sreeji_k@yahoo.com>
269 See L<Archive::Zip> by Ned Konz without which this module does not make
272 Minor mods by Ned Konz.
276 Copyright (c) 2002 Sreeji K. Das. All rights reserved. This program is free
277 software; you can redistribute it and/or modify it under the same terms