8000 Split Zip module up by class in to seperate files · rubyzip/rubyzip@cd038ae · GitHub
[go: up one dir, main page]

Skip to content

Commit cd038ae

Browse files
committed
Split Zip module up by class in to seperate files
1 parent 6582a63 commit cd038ae

19 files changed

+1945
-1834
lines changed

lib/zip/compressor.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Zip
2+
3+
VERSION = '0.9.4'
4+
5+
RUBY_MINOR_VERSION = RUBY_VERSION.split(".")[1].to_i
6+
7+
RUNNING_ON_WINDOWS = Config::CONFIG['host_os'] =~ /^win|mswin/i
8+
9+
# Ruby 1.7.x compatibility
10+
# In ruby 1.6.x and 1.8.0 reading from an empty stream returns
11+
# an empty string the first time and then nil.
12+
# not so in 1.7.x
13+
EMPTY_FILE_RETURNS_EMPTY_STRING_FIRST = RUBY_MINOR_VERSION != 7
14+
15+
class Compressor #:nodoc:all
16+
def finish
17+
end
18+
end
19+
end
20+
21+
# Copyright (C) 2002, 2003 Thomas Sondergaard
22+
# rubyzip is free software; you can redistribute it and/or
23+
# modify it under the terms of the ruby license.

lib/zip/decompressor.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Zip
2+
class Decompressor #:nodoc:all
3+
CHUNK_SIZE=32768
4+
def initialize(inputStream)
5+
super()
6+
@inputStream=inputStream
7+
end
8+
end
9+
end
10+
11+
# Copyright (C) 2002, 2003 Thomas Sondergaard
12+
# rubyzip is free software; you can redistribute it and/or
13+
# modify it under the terms of the ruby license.

lib/zip/deflater.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module Zip
2+
class Deflater < Compressor #:nodoc:all
3+
def initialize(outputStream, level = Zlib::DEFAULT_COMPRESSION)
4+
super()
5+
@outputStream = outputStream
6+
@zlibDeflater = Zlib::Deflate.new(level, -Zlib::MAX_WBITS)
7+
@size = 0
8+
@crc = Zlib::crc32
9+
end
10+
11+
def << (data)
12+
val = data.to_s
13+
@crc = Zlib::crc32(val, @crc)
14+
@size += val.size
15+
@outputStream << @zlibDeflater.deflate(data)
16+
end
17+
18+
def finish
19+
until @zlibDeflater.finished?
20+
@outputStream << @zlibDeflater.finish
21+
end
22+
end
23+
24+
attr_reader :size, :crc
25+
end
26+
end
27+
28+
# Copyright (C) 2002, 2003 Thomas Sondergaard
29+
# rubyzip is free software; you can redistribute it and/or
30+
# modify it under the terms of the ruby license.

lib/zip/inflater.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
module Zip
2+
class Inflater < Decompressor #:nodoc:all
3+
def initialize(inputStream)
4+
super
5+
@zlibInflater = Zlib::Inflate.new(-Zlib::MAX_WBITS)
6+
@outputBuffer=""
7+
@hasReturnedEmptyString = ! EMPTY_FILE_RETURNS_EMPTY_STRING_FIRST
8+
end
9+
10+
def sysread(numberOfBytes = nil, buf = nil)
11+
readEverything = (numberOfBytes == nil)
12+
while (readEverything || @outputBuffer.length < numberOfBytes)
13+
break if internal_input_finished?
14+
@outputBuffer << internal_produce_input(buf)
15+
end
16+
return value_when_finished if @outputBuffer.length==0 && input_finished?
17+
endIndex= numberOfBytes==nil ? @outputBuffer.length : numberOfBytes
18+
return @outputBuffer.slice!(0...endIndex)
19+
end
20+
21+
def produce_input
22+
if (@outputBuffer.empty?)
23+
return internal_produce_input
24+
else
25+
return @outputBuffer.slice!(0...(@outputBuffer.length))
26+
end
27+
end
28+
29+
# to be used with produce_input, not read (as read may still have more data cached)
30+
# is data cached anywhere other than @outputBuffer? the comment above may be wrong
31+
def input_finished?
32+
@outputBuffer.empty? && internal_input_finished?
33+
end
34+
alias :eof :input_finished?
35+
alias :eof? :input_finished?
36+
37+
private
38+
39+
def internal_produce_input(buf = nil)
40+
retried = 0
41+
begin
42+
@zlibInflater.inflate(@inputStream.read(Decompressor::CHUNK_SIZE, buf))
43+
rescue Zlib::BufError
44+
raise if (retried >= 5) # how many times should we retry?
45+
retried += 1
46+
retry
47+
end
48+
end
49+
50+
def internal_input_finished?
51+
@zlibInflater.finished?
52+
end
53+
54+
# TODO: Specialize to handle different behaviour in ruby > 1.7.0 ?
55+
def value_when_finished # mimic behaviour of ruby File object.
56+
return nil if @hasReturnedEmptyString
57+
@hasReturnedEmptyString=true
58+
return ""
59+
end
60+
end
61+
end
62+
63+
# Copyright (C) 2002, 2003 Thomas Sondergaard
64+
# rubyzip is free software; you can redistribute it and/or
65+
# modify it under the terms of the ruby license.

lib/zip/null_compressor.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Zip
2+
class NullCompressor < Compressor #:nodoc:all
3+
include Singleton
4+
5+
def << (data)
6+
raise IOError, "closed stream"
7+
end
8+
9+
attr_reader :size, :compressed_size
10+
end
11+
end
12+
13+
# Copyright (C) 2002, 2003 Thomas Sondergaard
14+
# rubyzip is free software; you can redistribute it and/or
15+
# modify it under the terms of the ruby license.

lib/zip/null_decompressor.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Zip
2+
class NullDecompressor #:nodoc:all
3+
include Singleton
4+
def sysread(numberOfBytes = nil, buf = nil)
5+
nil
6+
end
7+
8+
def produce_input
9+
nil
10+
end
11+
12+
def input_finished?
13+
true
14+
end
15+
16+
def eof
17+
true
18+
end
19+
alias :eof? :eof
20+
end
21+
end
22+
23+
# Copyright (C) 2002, 2003 Thomas Sondergaard
24+
# rubyzip is free software; you can redistribute it and/or
25+
# modify it under the terms of the ruby license.

lib/zip/null_input_stream.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Zip
2+
class NullInputStream < NullDecompressor #:nodoc:all
3+
include IOExtras::AbstractInputStream
4+
end
5+
end
6+
7+
# Copyright (C) 2002, 2003 Thomas Sondergaard
8+
# rubyzip is free software; you can redistribute it and/or
9+
# modify it under the terms of the ruby license.

lib/zip/pass_thru_compressor.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Zip
2+
class PassThruCompressor < Compressor #:nodoc:all
3+
def initialize(outputStream)
4+
super()
5+
@outputStream = outputStream
6+
@crc = Zlib::crc32
7+
@size = 0
8+
end
9+
10+
def << (data)
11+
val = data.to_s
12+
@crc = Zlib::crc32(val, @crc)
13+
@size += val.size
14+
@outputStream << val
15+
end
16+
17+
attr_reader :size, :crc
18+
end
19+
end
20+
21+
# Copyright (C) 2002, 2003 Thomas Sondergaard
22+
# rubyzip is free software; you can redistribute it and/or
23+
# modify it under the terms of the ruby license.

lib/zip/pass_thru_decompressor.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module Zip
2+
class PassThruDecompressor < Decompressor #:nodoc:all
3+
def initialize(inputStream, charsToRead)
4+
super inputStream
5+
@charsToRead = charsToRead
6+
@readSoFar = 0
7+
@hasReturnedEmptyString = ! EMPTY_FILE_RETURNS_EMPTY_STRING_FIRST
8+
end
9+
10+
# TODO: Specialize to handle different behaviour in ruby > 1.7.0 ?
11+
def sysread(numberOfBytes = nil, buf = nil)
12+
if input_finished?
13+
hasReturnedEmptyStringVal=@hasReturnedEmptyString
14+
@hasReturnedEmptyString=true
15+
return "" unless hasReturnedEmptyStringVal
16+
return nil
17+
end
18+
19+
if (numberOfBytes == nil || @readSoFar+numberOfBytes > @charsToRead)
20+
numberOfBytes = @charsToRead-@readSoFar
21+
end
22+
@readSoFar += numberOfBytes
23+
@inputStream.read(numberOfBytes, buf)
24+
end
25+
26+
def produce_input
27+
sysread(Decompressor::CHUNK_SIZE)
28+
end
29+
30+
def input_finished?
31+
(@readSoFar >= @charsToRead)
32+
end
33+
alias :eof :input_finished?
34+
alias :eof? :input_finished?
35+
end
36+
end
37+
38+
# Copyright (C) 2002, 2003 Thomas Sondergaard
39+
# rubyzip is free software; you can redistribute it and/or
40+
# modify it under the terms of the ruby license.

0 commit comments

Comments
 (0)
0