File: input_stream.rb

package info (click to toggle)
ruby-zip 3.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,120 kB
  • sloc: ruby: 9,958; makefile: 23
file content (212 lines) | stat: -rw-r--r-- 6,265 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# frozen_string_literal: true

##
module Zip
  # InputStream is the basic class for reading zip entries in a
  # zip file. It is possible to create a InputStream object directly,
  # passing the zip file name to the constructor, but more often than not
  # the InputStream will be obtained from a File (perhaps using the
  # ZipFileSystem interface) object for a particular entry in the zip
  # archive.
  #
  # A InputStream inherits IOExtras::AbstractInputStream in order
  # to provide an IO-like interface for reading from a single zip
  # entry. Beyond methods for mimicking an IO-object it contains
  # the method get_next_entry for iterating through the entries of
  # an archive. get_next_entry returns a Entry object that describes
  # the zip entry the InputStream is currently reading from.
  #
  # Example that creates a zip archive with ZipOutputStream and reads it
  # back again with a InputStream.
  #
  #   require 'zip'
  #
  #   Zip::OutputStream.open("my.zip") do |io|
  #
  #     io.put_next_entry("first_entry.txt")
  #     io.write "Hello world!"
  #
  #     io.put_next_entry("adir/first_entry.txt")
  #     io.write "Hello again!"
  #   end
  #
  #
  #   Zip::InputStream.open("my.zip") do |io|
  #
  #     while (entry = io.get_next_entry)
  #       puts "Contents of #{entry.name}: '#{io.read}'"
  #     end
  #   end
  #
  # java.util.zip.ZipInputStream is the original inspiration for this
  # class.
  class InputStream
    CHUNK_SIZE = 32_768 # :nodoc:

    include ::Zip::IOExtras::AbstractInputStream

    # Opens the indicated zip file. An exception is thrown
    # if the specified offset in the specified filename is
    # not a local zip entry header.
    #
    # @param context [String||IO||StringIO] file path or IO/StringIO object
    # @param offset [Integer] offset in the IO/StringIO
    def initialize(context, offset: 0, decrypter: nil)
      super()
      @archive_io = get_io(context, offset)
      @decompressor = ::Zip::NullDecompressor
      @decrypter = decrypter
      @current_entry = nil
      @complete_entry = nil
    end

    # Close this InputStream. All further IO will raise an IOError.
    def close
      @archive_io.close
    end

    # Returns an Entry object and positions the stream at the beginning of
    # the entry data. It is necessary to call this method on a newly created
    # InputStream before reading from the first entry in the archive.
    # Returns nil when there are no more entries.
    def get_next_entry
      unless @current_entry.nil?
        raise StreamingError, @current_entry if @current_entry.incomplete?

        @archive_io.seek(@current_entry.next_header_offset, IO::SEEK_SET)
      end

      open_entry
    end

    # Rewinds the stream to the beginning of the current entry.
    def rewind
      return if @current_entry.nil?

      @lineno = 0
      @pos    = 0
      @archive_io.seek(@current_entry.local_header_offset, IO::SEEK_SET)
      open_entry
    end

    # Modeled after IO.sysread
    def sysread(length = nil, outbuf = +'')
      @decompressor.read(length, outbuf)
    end

    # Returns the size of the current entry, or `nil` if there isn't one.
    def size
      return if @current_entry.nil?

      @current_entry.size
    end

    class << self
      # Same as #initialize but if a block is passed the opened
      # stream is passed to the block and closed when the block
      # returns.
      def open(filename_or_io, offset: 0, decrypter: nil)
        zio = new(filename_or_io, offset: offset, decrypter: decrypter)
        return zio unless block_given?

        begin
          yield zio
        ensure
          zio.close if zio
        end
      end
    end

    protected

    def get_io(io_or_file, offset = 0) # :nodoc:
      if io_or_file.respond_to?(:seek)
        io = io_or_file.dup
        io.seek(offset, ::IO::SEEK_SET)
        io
      else
        file = ::File.open(io_or_file, 'rb')
        file.seek(offset, ::IO::SEEK_SET)
        file
      end
    end

    def open_entry # :nodoc:
      @current_entry = ::Zip::Entry.read_local_entry(@archive_io)
      return if @current_entry.nil?

      if @current_entry.incomplete? && @current_entry.compressed_size == 0 && !@complete_entry
        raise StreamingError, @current_entry
      end

      @decompressor = assemble_io
      flush
      @current_entry
    end

    def assemble_io # :nodoc:
      io = if @current_entry.encrypted?
             raise Error, 'A password is required to decode this zip file.' if @decrypter.nil?

             get_decrypted_io
           else
             @archive_io
           end

      get_decompressor(io)
    end

    def get_decrypted_io # :nodoc:
      header = @archive_io.read(@decrypter.header_bytesize)
      @decrypter.reset!(header)

      compressed_size =
        if @current_entry.incomplete? && @current_entry.crc == 0 &&
           @current_entry.compressed_size == 0 && @complete_entry
          @complete_entry.compressed_size
        else
          @current_entry.compressed_size
        end

      if @decrypter.kind_of?(::Zip::AESDecrypter)
        compressed_size -= @decrypter.header_bytesize
        compressed_size -= ::Zip::AESEncryption::AUTHENTICATION_CODE_LENGTH
      end

      ::Zip::DecryptedIo.new(@archive_io, @decrypter, compressed_size)
    end

    def get_decompressor(io) # :nodoc:
      return ::Zip::NullDecompressor if @current_entry.nil?

      decompressed_size =
        if @current_entry.incomplete? && @current_entry.crc == 0 &&
           @current_entry.size == 0 && @complete_entry
          @complete_entry.size
        else
          @current_entry.size
        end

      decompressor_class = ::Zip::Decompressor.find_by_compression_method(
        @current_entry.compression_method
      )
      if decompressor_class.nil?
        raise ::Zip::CompressionMethodError, @current_entry.compression_method
      end

      decompressor_class.new(io, decompressed_size)
    end

    def produce_input # :nodoc:
      @decompressor.read(CHUNK_SIZE)
    end

    def input_finished? # :nodoc:
      @decompressor.eof?
    end
  end
end

# Copyright (C) 2002, 2003 Thomas Sondergaard
# rubyzip is free software; you can redistribute it and/or
# modify it under the terms of the ruby license.