mezza-rubyzip 0.9.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/NEWS +162 -0
- data/README +68 -0
- data/Rakefile +13 -0
- data/TODO +16 -0
- data/lib/zip/compressor.rb +23 -0
- data/lib/zip/decompressor.rb +13 -0
- data/lib/zip/deflater.rb +30 -0
- data/lib/zip/inflater.rb +65 -0
- data/lib/zip/ioextras.rb +165 -0
- data/lib/zip/null_compressor.rb +15 -0
- data/lib/zip/null_decompressor.rb +25 -0
- data/lib/zip/null_input_stream.rb +9 -0
- data/lib/zip/pass_thru_compressor.rb +23 -0
- data/lib/zip/pass_thru_decompressor.rb +40 -0
- data/lib/zip/stdrubyext.rb +111 -0
- data/lib/zip/tempfile_bugfixed.rb +195 -0
- data/lib/zip/zip.rb +66 -0
- data/lib/zip/zip_central_directory.rb +137 -0
- data/lib/zip/zip_entry.rb +631 -0
- data/lib/zip/zip_entry_set.rb +66 -0
- data/lib/zip/zip_extra_field.rb +213 -0
- data/lib/zip/zip_file.rb +310 -0
- data/lib/zip/zip_input_stream.rb +134 -0
- data/lib/zip/zip_output_stream.rb +171 -0
- data/lib/zip/zip_streamable_directory.rb +15 -0
- data/lib/zip/zip_streamable_stream.rb +47 -0
- data/lib/zip/zipfilesystem.rb +610 -0
- data/lib/zip/ziprequire.rb +90 -0
- data/samples/example.rb +69 -0
- data/samples/example_filesystem.rb +33 -0
- data/samples/gtkRubyzip.rb +86 -0
- data/samples/qtzip.rb +101 -0
- data/samples/write_simple.rb +13 -0
- data/samples/zipfind.rb +74 -0
- metadata +92 -0
@@ -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.
|
@@ -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.
|
@@ -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.
|
@@ -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.
|
@@ -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,0 +1,111 @@
|
|
1
|
+
unless Enumerable.method_defined?(:inject)
|
2
|
+
module Enumerable #:nodoc:all
|
3
|
+
def inject(n = 0)
|
4
|
+
each { |value| n = yield(n, value) }
|
5
|
+
n
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module Enumerable #:nodoc:all
|
11
|
+
# returns a new array of all the return values not equal to nil
|
12
|
+
# This implementation could be faster
|
13
|
+
def select_map(&aProc)
|
14
|
+
map(&aProc).reject { |e| e.nil? }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
unless Object.method_defined?(:object_id)
|
19
|
+
class Object #:nodoc:all
|
20
|
+
# Using object_id which is the new thing, so we need
|
21
|
+
# to make that work in versions prior to 1.8.0
|
22
|
+
alias object_id id
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
unless File.respond_to?(:read)
|
27
|
+
class File # :nodoc:all
|
28
|
+
# singleton method read does not exist in 1.6.x
|
29
|
+
def self.read(fileName)
|
30
|
+
open(fileName) { |f| f.read }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class String #:nodoc:all
|
36
|
+
def starts_with(aString)
|
37
|
+
rindex(aString, 0) == 0
|
38
|
+
end
|
39
|
+
|
40
|
+
def ends_with(aString)
|
41
|
+
index(aString, -aString.size)
|
42
|
+
end
|
43
|
+
|
44
|
+
def ensure_end(aString)
|
45
|
+
ends_with(aString) ? self : self + aString
|
46
|
+
end
|
47
|
+
|
48
|
+
def lchop
|
49
|
+
slice(1, length)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class Time #:nodoc:all
|
54
|
+
|
55
|
+
#MS-DOS File Date and Time format as used in Interrupt 21H Function 57H:
|
56
|
+
#
|
57
|
+
# Register CX, the Time:
|
58
|
+
# Bits 0-4 2 second increments (0-29)
|
59
|
+
# Bits 5-10 minutes (0-59)
|
60
|
+
# bits 11-15 hours (0-24)
|
61
|
+
#
|
62
|
+
# Register DX, the Date:
|
63
|
+
# Bits 0-4 day (1-31)
|
64
|
+
# bits 5-8 month (1-12)
|
65
|
+
# bits 9-15 year (four digit year minus 1980)
|
66
|
+
|
67
|
+
|
68
|
+
def to_binary_dos_time
|
69
|
+
(sec/2) +
|
70
|
+
(min << 5) +
|
71
|
+
(hour << 11)
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_binary_dos_date
|
75
|
+
(day) +
|
76
|
+
(month << 5) +
|
77
|
+
((year - 1980) << 9)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Dos time is only stored with two seconds accuracy
|
81
|
+
def dos_equals(other)
|
82
|
+
to_i/2 == other.to_i/2
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.parse_binary_dos_format(binaryDosDate, binaryDosTime)
|
86
|
+
second = 2 * ( 0b11111 & binaryDosTime)
|
87
|
+
minute = ( 0b11111100000 & binaryDosTime) >> 5
|
88
|
+
hour = (0b1111100000000000 & binaryDosTime) >> 11
|
89
|
+
day = ( 0b11111 & binaryDosDate)
|
90
|
+
month = ( 0b111100000 & binaryDosDate) >> 5
|
91
|
+
year = ((0b1111111000000000 & binaryDosDate) >> 9) + 1980
|
92
|
+
begin
|
93
|
+
return Time.local(year, month, day, hour, minute, second)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
class Module #:nodoc:all
|
99
|
+
def forward_message(forwarder, *messagesToForward)
|
100
|
+
methodDefs = messagesToForward.map {
|
101
|
+
|msg|
|
102
|
+
"def #{msg}; #{forwarder}(:#{msg}); end"
|
103
|
+
}
|
104
|
+
module_eval(methodDefs.join("\n"))
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
# Copyright (C) 2002, 2003 Thomas Sondergaard
|
110
|
+
# rubyzip is free software; you can redistribute it and/or
|
111
|
+
# modify it under the terms of the ruby license.
|
@@ -0,0 +1,195 @@
|
|
1
|
+
#
|
2
|
+
# tempfile - manipulates temporary files
|
3
|
+
#
|
4
|
+
# $Id: tempfile_bugfixed.rb,v 1.2 2005/02/19 20:30:33 thomas Exp $
|
5
|
+
#
|
6
|
+
|
7
|
+
require 'delegate'
|
8
|
+
require 'tmpdir'
|
9
|
+
|
10
|
+
module BugFix #:nodoc:all
|
11
|
+
|
12
|
+
# A class for managing temporary files. This library is written to be
|
13
|
+
# thread safe.
|
14
|
+
class Tempfile < DelegateClass(File)
|
15
|
+
MAX_TRY = 10
|
16
|
+
@@cleanlist = []
|
17
|
+
|
18
|
+
# Creates a temporary file of mode 0600 in the temporary directory
|
19
|
+
# whose name is basename.pid.n and opens with mode "w+". A Tempfile
|
20
|
+
# object works just like a File object.
|
21
|
+
#
|
22
|
+
# If tmpdir is omitted, the temporary directory is determined by
|
23
|
+
# Dir::tmpdir provided by 'tmpdir.rb'.
|
24
|
+
# When $SAFE > 0 and the given tmpdir is tainted, it uses
|
25
|
+
# /tmp. (Note that ENV values are tainted by default)
|
26
|
+
def initialize(basename, tmpdir=Dir::tmpdir)
|
27
|
+
if $SAFE > 0 and tmpdir.tainted?
|
28
|
+
tmpdir = '/tmp'
|
29
|
+
end
|
30
|
+
|
31
|
+
lock = nil
|
32
|
+
n = failure = 0
|
33
|
+
|
34
|
+
begin
|
35
|
+
Thread.critical = true
|
36
|
+
|
37
|
+
begin
|
38
|
+
tmpname = sprintf('%s/%s%d.%d', tmpdir, basename, $$, n)
|
39
|
+
lock = tmpname + '.lock'
|
40
|
+
n += 1
|
41
|
+
end while @@cleanlist.include?(tmpname) or
|
42
|
+
::File.exist?(lock) or ::File.exist?(tmpname)
|
43
|
+
|
44
|
+
Dir.mkdir(lock)
|
45
|
+
rescue
|
46
|
+
failure += 1
|
47
|
+
retry if failure < MAX_TRY
|
48
|
+
raise "cannot generate tempfile `%s'" % tmpname
|
49
|
+
ensure
|
50
|
+
Thread.critical = false
|
51
|
+
end
|
52
|
+
|
53
|
+
@data = [tmpname]
|
54
|
+
@clean_proc = Tempfile.callback(@data)
|
55
|
+
ObjectSpace.define_finalizer(self, @clean_proc)
|
56
|
+
|
57
|
+
@tmpfile = ::File.open(tmpname, ::File::RDWR|::File::CREAT|::File::EXCL, 0600)
|
58
|
+
@tmpname = tmpname
|
59
|
+
@@cleanlist << @tmpname
|
60
|
+
@data[1] = @tmpfile
|
61
|
+
@data[2] = @@cleanlist
|
62
|
+
|
63
|
+
super(@tmpfile)
|
64
|
+
|
65
|
+
# Now we have all the File/IO methods defined, you must not
|
66
|
+
# carelessly put bare puts(), etc. after this.
|
67
|
+
|
68
|
+
Dir.rmdir(lock)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Opens or reopens the file with mode "r+".
|
72
|
+
def open
|
73
|
+
@tmpfile.close if @tmpfile
|
74
|
+
@tmpfile = ::File.open(@tmpname, 'r+')
|
75
|
+
@data[1] = @tmpfile
|
76
|
+
__setobj__(@tmpfile)
|
77
|
+
end
|
78
|
+
|
79
|
+
def _close # :nodoc:
|
80
|
+
@tmpfile.close if @tmpfile
|
81
|
+
@data[1] = @tmpfile = nil
|
82
|
+
end
|
83
|
+
protected :_close
|
84
|
+
|
85
|
+
# Closes the file. If the optional flag is true, unlinks the file
|
86
|
+
# after closing.
|
87
|
+
#
|
88
|
+
# If you don't explicitly unlink the temporary file, the removal
|
89
|
+
# will be delayed until the object is finalized.
|
90
|
+
def close(unlink_now=false)
|
91
|
+
if unlink_now
|
92
|
+
close!
|
93
|
+
else
|
94
|
+
_close
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# Closes and unlinks the file.
|
99
|
+
def close!
|
100
|
+
_close
|
101
|
+
@clean_proc.call
|
102
|
+
ObjectSpace.undefine_finalizer(self)
|
103
|
+
end
|
104
|
+
|
105
|
+
# Unlinks the file. On UNIX-like systems, it is often a good idea
|
106
|
+
# to unlink a temporary file immediately after creating and opening
|
107
|
+
# it, because it leaves other programs zero chance to access the
|
108
|
+
# file.
|
109
|
+
def unlink
|
110
|
+
# keep this order for thread safeness
|
111
|
+
::File.unlink(@tmpname) if ::File.exist?(@tmpname)
|
112
|
+
@@cleanlist.delete(@tmpname) if @@cleanlist
|
113
|
+
end
|
114
|
+
alias delete unlink
|
115
|
+
|
116
|
+
if RUBY_VERSION > '1.8.0'
|
117
|
+
def __setobj__(obj)
|
118
|
+
@_dc_obj = obj
|
119
|
+
end
|
120
|
+
else
|
121
|
+
def __setobj__(obj)
|
122
|
+
@obj = obj
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# Returns the full path name of the temporary file.
|
127
|
+
def path
|
128
|
+
@tmpname
|
129
|
+
end
|
130
|
+
|
131
|
+
# Returns the size of the temporary file. As a side effect, the IO
|
132
|
+
# buffer is flushed before determining the size.
|
133
|
+
def size
|
134
|
+
if @tmpfile
|
135
|
+
@tmpfile.flush
|
136
|
+
@tmpfile.stat.size
|
137
|
+
else
|
138
|
+
0
|
139
|
+
end
|
140
|
+
end
|
141
|
+
alias length size
|
142
|
+
|
143
|
+
class << self
|
144
|
+
def callback(data) # :nodoc:
|
145
|
+
pid = $$
|
146
|
+
lambda{
|
147
|
+
if pid == $$
|
148
|
+
path, tmpfile, cleanlist = *data
|
149
|
+
|
150
|
+
print "removing ", path, "..." if $DEBUG
|
151
|
+
|
152
|
+
tmpfile.close if tmpfile
|
153
|
+
|
154
|
+
# keep this order for thread safeness
|
155
|
+
::File.unlink(path) if ::File.exist?(path)
|
156
|
+
cleanlist.delete(path) if cleanlist
|
157
|
+
|
158
|
+
print "done\n" if $DEBUG
|
159
|
+
end
|
160
|
+
}
|
161
|
+
end
|
162
|
+
|
163
|
+
# If no block is given, this is a synonym for new().
|
164
|
+
#
|
165
|
+
# If a block is given, it will be passed tempfile as an argument,
|
166
|
+
# and the tempfile will automatically be closed when the block
|
167
|
+
# terminates. In this case, open() returns nil.
|
168
|
+
def open(*args)
|
169
|
+
tempfile = new(*args)
|
170
|
+
|
171
|
+
if block_given?
|
172
|
+
begin
|
173
|
+
yield(tempfile)
|
174
|
+
ensure
|
175
|
+
tempfile.close
|
176
|
+
end
|
177
|
+
|
178
|
+
nil
|
179
|
+
else
|
180
|
+
tempfile
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
end # module BugFix
|
187
|
+
if __FILE__ == $0
|
188
|
+
# $DEBUG = true
|
189
|
+
f = Tempfile.new("foo")
|
190
|
+
f.print("foo\n")
|
191
|
+
f.close
|
192
|
+
f.open
|
193
|
+
p f.gets # => "foo\n"
|
194
|
+
f.close!
|
195
|
+
end
|
data/lib/zip/zip.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
require 'iconv'
|
3
|
+
require 'singleton'
|
4
|
+
require 'tempfile'
|
5
|
+
require 'fileutils'
|
6
|
+
require 'stringio'
|
7
|
+
require 'zlib'
|
8
|
+
require 'zip/stdrubyext'
|
9
|
+
require 'zip/ioextras'
|
10
|
+
require 'rbconfig'
|
11
|
+
|
12
|
+
require 'zip/zip_entry'
|
13
|
+
require 'zip/zip_extra_field'
|
14
|
+
require 'zip/zip_entry_set'
|
15
|
+
require 'zip/zip_central_directory'
|
16
|
+
require 'zip/zip_file'
|
17
|
+
require 'zip/zip_input_stream'
|
18
|
+
require 'zip/zip_output_stream'
|
19
|
+
require 'zip/decompressor'
|
20
|
+
require 'zip/compressor'
|
21
|
+
require 'zip/null_decompressor'
|
22
|
+
require 'zip/null_compressor'
|
23
|
+
require 'zip/pass_thru_compressor'
|
24
|
+
require 'zip/pass_thru_decompressor'
|
25
|
+
require 'zip/inflater'
|
26
|
+
require 'zip/deflater'
|
27
|
+
require 'zip/zip_streamable_stream'
|
28
|
+
require 'zip/zip_streamable_directory'
|
29
|
+
|
30
|
+
if Tempfile.superclass == SimpleDelegator
|
31
|
+
require 'zip/tempfile_bugfixed'
|
32
|
+
Tempfile = BugFix::Tempfile
|
33
|
+
end
|
34
|
+
|
35
|
+
module Zlib #:nodoc:all
|
36
|
+
if ! const_defined? :MAX_WBITS
|
37
|
+
MAX_WBITS = Zlib::Deflate.MAX_WBITS
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
module Zip
|
42
|
+
|
43
|
+
VERSION = '0.9.4.1'
|
44
|
+
|
45
|
+
RUBY_MINOR_VERSION = RUBY_VERSION.split(".")[1].to_i
|
46
|
+
|
47
|
+
RUNNING_ON_WINDOWS = Config::CONFIG['host_os'] =~ /^win|mswin/i
|
48
|
+
|
49
|
+
# Ruby 1.7.x compatibility
|
50
|
+
# In ruby 1.6.x and 1.8.0 reading from an empty stream returns
|
51
|
+
# an empty string the first time and then nil.
|
52
|
+
# not so in 1.7.x
|
53
|
+
EMPTY_FILE_RETURNS_EMPTY_STRING_FIRST = RUBY_MINOR_VERSION != 7
|
54
|
+
|
55
|
+
class ZipError < StandardError ; end
|
56
|
+
|
57
|
+
class ZipEntryExistsError < ZipError; end
|
58
|
+
class ZipDestinationFileExistsError < ZipError; end
|
59
|
+
class ZipCompressionMethodError < ZipError; end
|
60
|
+
class ZipEntryNameError < ZipError; end
|
61
|
+
class ZipInternalError < ZipError; end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Copyright (C) 2002, 2003 Thomas Sondergaard
|
65
|
+
# rubyzip is free software; you can redistribute it and/or
|
66
|
+
# modify it under the terms of the ruby license.
|