rubyzip 1.3.0 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -8
- data/Rakefile +3 -0
- data/lib/zip.rb +4 -3
- data/lib/zip/central_directory.rb +9 -5
- data/lib/zip/constants.rb +52 -0
- data/lib/zip/crypto/decrypted_io.rb +40 -0
- data/lib/zip/crypto/traditional_encryption.rb +9 -9
- data/lib/zip/decompressor.rb +19 -1
- data/lib/zip/dos_time.rb +12 -7
- data/lib/zip/entry.rb +57 -38
- data/lib/zip/entry_set.rb +2 -0
- data/lib/zip/errors.rb +1 -0
- data/lib/zip/extra_field.rb +11 -9
- data/lib/zip/extra_field/generic.rb +10 -9
- data/lib/zip/extra_field/ntfs.rb +4 -0
- data/lib/zip/extra_field/old_unix.rb +3 -1
- data/lib/zip/extra_field/universal_time.rb +42 -12
- data/lib/zip/extra_field/unix.rb +3 -1
- data/lib/zip/extra_field/zip64.rb +4 -2
- data/lib/zip/file.rb +79 -54
- data/lib/zip/filesystem.rb +193 -177
- data/lib/zip/inflater.rb +24 -36
- data/lib/zip/input_stream.rb +33 -26
- data/lib/zip/ioextras.rb +1 -1
- data/lib/zip/ioextras/abstract_input_stream.rb +19 -8
- data/lib/zip/ioextras/abstract_output_stream.rb +1 -1
- data/lib/zip/null_decompressor.rb +1 -9
- data/lib/zip/output_stream.rb +14 -5
- data/lib/zip/pass_thru_compressor.rb +2 -2
- data/lib/zip/pass_thru_decompressor.rb +13 -22
- data/lib/zip/streamable_directory.rb +3 -3
- data/lib/zip/streamable_stream.rb +6 -10
- data/lib/zip/version.rb +1 -1
- data/samples/example.rb +2 -2
- data/samples/example_filesystem.rb +1 -1
- data/samples/gtk_ruby_zip.rb +19 -19
- data/samples/qtzip.rb +6 -6
- data/samples/write_simple.rb +2 -4
- data/samples/zipfind.rb +23 -22
- metadata +28 -169
- data/test/basic_zip_file_test.rb +0 -60
- data/test/case_sensitivity_test.rb +0 -69
- data/test/central_directory_entry_test.rb +0 -69
- data/test/central_directory_test.rb +0 -100
- data/test/crypto/null_encryption_test.rb +0 -57
- data/test/crypto/traditional_encryption_test.rb +0 -80
- data/test/data/WarnInvalidDate.zip +0 -0
- data/test/data/file1.txt +0 -46
- data/test/data/file1.txt.deflatedData +0 -0
- data/test/data/file2.txt +0 -1504
- data/test/data/globTest.zip +0 -0
- data/test/data/globTest/foo.txt +0 -0
- data/test/data/globTest/foo/bar/baz/foo.txt +0 -0
- data/test/data/globTest/food.txt +0 -0
- data/test/data/gpbit3stored.zip +0 -0
- data/test/data/mimetype +0 -1
- data/test/data/notzippedruby.rb +0 -7
- data/test/data/ntfs.zip +0 -0
- data/test/data/oddExtraField.zip +0 -0
- data/test/data/path_traversal/Makefile +0 -10
- data/test/data/path_traversal/jwilk/README.md +0 -5
- data/test/data/path_traversal/jwilk/absolute1.zip +0 -0
- data/test/data/path_traversal/jwilk/absolute2.zip +0 -0
- data/test/data/path_traversal/jwilk/dirsymlink.zip +0 -0
- data/test/data/path_traversal/jwilk/dirsymlink2a.zip +0 -0
- data/test/data/path_traversal/jwilk/dirsymlink2b.zip +0 -0
- data/test/data/path_traversal/jwilk/relative0.zip +0 -0
- data/test/data/path_traversal/jwilk/relative2.zip +0 -0
- data/test/data/path_traversal/jwilk/symlink.zip +0 -0
- data/test/data/path_traversal/relative1.zip +0 -0
- data/test/data/path_traversal/tilde.zip +0 -0
- data/test/data/path_traversal/tuzovakaoff/README.md +0 -3
- data/test/data/path_traversal/tuzovakaoff/absolutepath.zip +0 -0
- data/test/data/path_traversal/tuzovakaoff/symlink.zip +0 -0
- data/test/data/rubycode.zip +0 -0
- data/test/data/rubycode2.zip +0 -0
- data/test/data/test.xls +0 -0
- data/test/data/testDirectory.bin +0 -0
- data/test/data/zip64-sample.zip +0 -0
- data/test/data/zipWithDirs.zip +0 -0
- data/test/data/zipWithEncryption.zip +0 -0
- data/test/deflater_test.rb +0 -65
- data/test/encryption_test.rb +0 -42
- data/test/entry_set_test.rb +0 -163
- data/test/entry_test.rb +0 -154
- data/test/errors_test.rb +0 -35
- data/test/extra_field_test.rb +0 -76
- data/test/file_extract_directory_test.rb +0 -54
- data/test/file_extract_test.rb +0 -145
- data/test/file_permissions_test.rb +0 -65
- data/test/file_split_test.rb +0 -57
- data/test/file_test.rb +0 -666
- data/test/filesystem/dir_iterator_test.rb +0 -58
- data/test/filesystem/directory_test.rb +0 -139
- data/test/filesystem/file_mutating_test.rb +0 -87
- data/test/filesystem/file_nonmutating_test.rb +0 -508
- data/test/filesystem/file_stat_test.rb +0 -64
- data/test/gentestfiles.rb +0 -126
- data/test/inflater_test.rb +0 -14
- data/test/input_stream_test.rb +0 -182
- data/test/ioextras/abstract_input_stream_test.rb +0 -102
- data/test/ioextras/abstract_output_stream_test.rb +0 -106
- data/test/ioextras/fake_io_test.rb +0 -18
- data/test/local_entry_test.rb +0 -154
- data/test/output_stream_test.rb +0 -128
- data/test/pass_thru_compressor_test.rb +0 -30
- data/test/pass_thru_decompressor_test.rb +0 -14
- data/test/path_traversal_test.rb +0 -141
- data/test/samples/example_recursive_test.rb +0 -37
- data/test/settings_test.rb +0 -95
- data/test/test_helper.rb +0 -234
- data/test/unicode_file_names_and_comments_test.rb +0 -62
- data/test/zip64_full_test.rb +0 -51
- data/test/zip64_support_test.rb +0 -14
data/lib/zip/filesystem.rb
CHANGED
@@ -35,25 +35,25 @@ module Zip
|
|
35
35
|
|
36
36
|
module FileSystem
|
37
37
|
def initialize # :nodoc:
|
38
|
-
|
39
|
-
@
|
40
|
-
@
|
41
|
-
@
|
42
|
-
@
|
38
|
+
mapped_zip = ZipFileNameMapper.new(self)
|
39
|
+
@zip_fs_dir = ZipFsDir.new(mapped_zip)
|
40
|
+
@zip_fs_file = ZipFsFile.new(mapped_zip)
|
41
|
+
@zip_fs_dir.file = @zip_fs_file
|
42
|
+
@zip_fs_file.dir = @zip_fs_dir
|
43
43
|
end
|
44
44
|
|
45
45
|
# Returns a ZipFsDir which is much like ruby's builtin Dir (class)
|
46
46
|
# object, except it works on the Zip::File on which this method is
|
47
47
|
# invoked
|
48
48
|
def dir
|
49
|
-
@
|
49
|
+
@zip_fs_dir
|
50
50
|
end
|
51
51
|
|
52
52
|
# Returns a ZipFsFile which is much like ruby's builtin File (class)
|
53
53
|
# object, except it works on the Zip::File on which this method is
|
54
54
|
# invoked
|
55
55
|
def file
|
56
|
-
@
|
56
|
+
@zip_fs_file
|
57
57
|
end
|
58
58
|
|
59
59
|
# Instances of this class are normally accessed via the accessor
|
@@ -70,22 +70,22 @@ module Zip
|
|
70
70
|
class << self
|
71
71
|
def delegate_to_fs_file(*methods)
|
72
72
|
methods.each do |method|
|
73
|
-
class_eval <<-
|
73
|
+
class_eval <<-END_EVAL, __FILE__, __LINE__ + 1
|
74
74
|
def #{method} # def file?
|
75
|
-
@
|
75
|
+
@zip_fs_file.#{method}(@entry_name) # @zip_fs_file.file?(@entry_name)
|
76
76
|
end # end
|
77
|
-
|
77
|
+
END_EVAL
|
78
78
|
end
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
82
|
-
def initialize(
|
83
|
-
@
|
84
|
-
@
|
82
|
+
def initialize(zip_fs_file, entry_name)
|
83
|
+
@zip_fs_file = zip_fs_file
|
84
|
+
@entry_name = entry_name
|
85
85
|
end
|
86
86
|
|
87
|
-
def kind_of?(
|
88
|
-
super ||
|
87
|
+
def kind_of?(type)
|
88
|
+
super || type == ::File::Stat
|
89
89
|
end
|
90
90
|
|
91
91
|
delegate_to_fs_file :file?, :directory?, :pipe?, :chardev?, :symlink?,
|
@@ -98,7 +98,7 @@ module Zip
|
|
98
98
|
end
|
99
99
|
|
100
100
|
def get_entry
|
101
|
-
@
|
101
|
+
@zip_fs_file.__send__(:get_entry, @entry_name)
|
102
102
|
end
|
103
103
|
private :get_entry
|
104
104
|
|
@@ -168,28 +168,29 @@ module Zip
|
|
168
168
|
end
|
169
169
|
end
|
170
170
|
|
171
|
-
def initialize(
|
172
|
-
@
|
171
|
+
def initialize(mapped_zip)
|
172
|
+
@mapped_zip = mapped_zip
|
173
173
|
end
|
174
174
|
|
175
|
-
def get_entry(
|
176
|
-
unless exists?(
|
177
|
-
raise Errno::ENOENT, "No such file or directory - #{
|
175
|
+
def get_entry(filename)
|
176
|
+
unless exists?(filename)
|
177
|
+
raise Errno::ENOENT, "No such file or directory - #{filename}"
|
178
178
|
end
|
179
|
-
|
179
|
+
|
180
|
+
@mapped_zip.find_entry(filename)
|
180
181
|
end
|
181
182
|
private :get_entry
|
182
183
|
|
183
|
-
def unix_mode_cmp(
|
184
|
-
e = get_entry(
|
184
|
+
def unix_mode_cmp(filename, mode)
|
185
|
+
e = get_entry(filename)
|
185
186
|
e.fstype == 3 && ((e.external_file_attributes >> 16) & mode) != 0
|
186
187
|
rescue Errno::ENOENT
|
187
188
|
false
|
188
189
|
end
|
189
190
|
private :unix_mode_cmp
|
190
191
|
|
191
|
-
def exists?(
|
192
|
-
expand_path(
|
192
|
+
def exists?(filename)
|
193
|
+
expand_path(filename) == '/' || !@mapped_zip.find_entry(filename).nil?
|
193
194
|
end
|
194
195
|
alias exist? exists?
|
195
196
|
|
@@ -197,133 +198,133 @@ module Zip
|
|
197
198
|
alias owned? exists?
|
198
199
|
alias grpowned? exists?
|
199
200
|
|
200
|
-
def readable?(
|
201
|
-
unix_mode_cmp(
|
201
|
+
def readable?(filename)
|
202
|
+
unix_mode_cmp(filename, 0o444)
|
202
203
|
end
|
203
204
|
alias readable_real? readable?
|
204
205
|
|
205
|
-
def writable?(
|
206
|
-
unix_mode_cmp(
|
206
|
+
def writable?(filename)
|
207
|
+
unix_mode_cmp(filename, 0o222)
|
207
208
|
end
|
208
209
|
alias writable_real? writable?
|
209
210
|
|
210
|
-
def executable?(
|
211
|
-
unix_mode_cmp(
|
211
|
+
def executable?(filename)
|
212
|
+
unix_mode_cmp(filename, 0o111)
|
212
213
|
end
|
213
214
|
alias executable_real? executable?
|
214
215
|
|
215
|
-
def setuid?(
|
216
|
-
unix_mode_cmp(
|
216
|
+
def setuid?(filename)
|
217
|
+
unix_mode_cmp(filename, 0o4000)
|
217
218
|
end
|
218
219
|
|
219
|
-
def setgid?(
|
220
|
-
unix_mode_cmp(
|
220
|
+
def setgid?(filename)
|
221
|
+
unix_mode_cmp(filename, 0o2000)
|
221
222
|
end
|
222
223
|
|
223
|
-
def sticky?(
|
224
|
-
unix_mode_cmp(
|
224
|
+
def sticky?(filename)
|
225
|
+
unix_mode_cmp(filename, 0o1000)
|
225
226
|
end
|
226
227
|
|
227
228
|
def umask(*args)
|
228
229
|
::File.umask(*args)
|
229
230
|
end
|
230
231
|
|
231
|
-
def truncate(
|
232
|
+
def truncate(_filename, _len)
|
232
233
|
raise StandardError, 'truncate not supported'
|
233
234
|
end
|
234
235
|
|
235
|
-
def directory?(
|
236
|
-
entry = @
|
237
|
-
expand_path(
|
236
|
+
def directory?(filename)
|
237
|
+
entry = @mapped_zip.find_entry(filename)
|
238
|
+
expand_path(filename) == '/' || (!entry.nil? && entry.directory?)
|
238
239
|
end
|
239
240
|
|
240
|
-
def open(
|
241
|
-
|
242
|
-
case
|
241
|
+
def open(filename, mode = 'r', permissions = 0o644, &block)
|
242
|
+
mode.delete!('b') # ignore b option
|
243
|
+
case mode
|
243
244
|
when 'r'
|
244
|
-
@
|
245
|
+
@mapped_zip.get_input_stream(filename, &block)
|
245
246
|
when 'w'
|
246
|
-
@
|
247
|
+
@mapped_zip.get_output_stream(filename, permissions, &block)
|
247
248
|
else
|
248
|
-
raise StandardError, "openmode '#{
|
249
|
+
raise StandardError, "openmode '#{mode} not supported" unless mode == 'r'
|
249
250
|
end
|
250
251
|
end
|
251
252
|
|
252
|
-
def new(
|
253
|
-
open(
|
253
|
+
def new(filename, mode = 'r')
|
254
|
+
self.open(filename, mode)
|
254
255
|
end
|
255
256
|
|
256
|
-
def size(
|
257
|
-
@
|
257
|
+
def size(filename)
|
258
|
+
@mapped_zip.get_entry(filename).size
|
258
259
|
end
|
259
260
|
|
260
261
|
# Returns nil for not found and nil for directories
|
261
|
-
def size?(
|
262
|
-
entry = @
|
262
|
+
def size?(filename)
|
263
|
+
entry = @mapped_zip.find_entry(filename)
|
263
264
|
entry.nil? || entry.directory? ? nil : entry.size
|
264
265
|
end
|
265
266
|
|
266
|
-
def chown(
|
267
|
-
filenames.each do |
|
268
|
-
e = get_entry(
|
267
|
+
def chown(owner, group, *filenames)
|
268
|
+
filenames.each do |filename|
|
269
|
+
e = get_entry(filename)
|
269
270
|
e.extra.create('IUnix') unless e.extra.member?('IUnix')
|
270
|
-
e.extra['IUnix'].uid =
|
271
|
-
e.extra['IUnix'].gid =
|
271
|
+
e.extra['IUnix'].uid = owner
|
272
|
+
e.extra['IUnix'].gid = group
|
272
273
|
end
|
273
274
|
filenames.size
|
274
275
|
end
|
275
276
|
|
276
|
-
def chmod(
|
277
|
-
filenames.each do |
|
278
|
-
e = get_entry(
|
277
|
+
def chmod(mode, *filenames)
|
278
|
+
filenames.each do |filename|
|
279
|
+
e = get_entry(filename)
|
279
280
|
e.fstype = 3 # force convertion filesystem type to unix
|
280
|
-
e.unix_perms =
|
281
|
-
e.external_file_attributes =
|
281
|
+
e.unix_perms = mode
|
282
|
+
e.external_file_attributes = mode << 16
|
282
283
|
e.dirty = true
|
283
284
|
end
|
284
285
|
filenames.size
|
285
286
|
end
|
286
287
|
|
287
|
-
def zero?(
|
288
|
-
sz = size(
|
288
|
+
def zero?(filename)
|
289
|
+
sz = size(filename)
|
289
290
|
sz.nil? || sz == 0
|
290
291
|
rescue Errno::ENOENT
|
291
292
|
false
|
292
293
|
end
|
293
294
|
|
294
|
-
def file?(
|
295
|
-
entry = @
|
295
|
+
def file?(filename)
|
296
|
+
entry = @mapped_zip.find_entry(filename)
|
296
297
|
!entry.nil? && entry.file?
|
297
298
|
end
|
298
299
|
|
299
|
-
def dirname(
|
300
|
-
::File.dirname(
|
300
|
+
def dirname(filename)
|
301
|
+
::File.dirname(filename)
|
301
302
|
end
|
302
303
|
|
303
|
-
def basename(
|
304
|
-
::File.basename(
|
304
|
+
def basename(filename)
|
305
|
+
::File.basename(filename)
|
305
306
|
end
|
306
307
|
|
307
|
-
def split(
|
308
|
-
::File.split(
|
308
|
+
def split(filename)
|
309
|
+
::File.split(filename)
|
309
310
|
end
|
310
311
|
|
311
312
|
def join(*fragments)
|
312
313
|
::File.join(*fragments)
|
313
314
|
end
|
314
315
|
|
315
|
-
def utime(
|
316
|
-
|
317
|
-
get_entry(
|
316
|
+
def utime(modified_time, *filenames)
|
317
|
+
filenames.each do |filename|
|
318
|
+
get_entry(filename).time = modified_time
|
318
319
|
end
|
319
320
|
end
|
320
321
|
|
321
|
-
def mtime(
|
322
|
-
@
|
322
|
+
def mtime(filename)
|
323
|
+
@mapped_zip.get_entry(filename).mtime
|
323
324
|
end
|
324
325
|
|
325
|
-
def atime(
|
326
|
-
e = get_entry(
|
326
|
+
def atime(filename)
|
327
|
+
e = get_entry(filename)
|
327
328
|
if e.extra.member? 'UniversalTime'
|
328
329
|
e.extra['UniversalTime'].atime
|
329
330
|
elsif e.extra.member? 'NTFS'
|
@@ -331,8 +332,8 @@ module Zip
|
|
331
332
|
end
|
332
333
|
end
|
333
334
|
|
334
|
-
def ctime(
|
335
|
-
e = get_entry(
|
335
|
+
def ctime(filename)
|
336
|
+
e = get_entry(filename)
|
336
337
|
if e.extra.member? 'UniversalTime'
|
337
338
|
e.extra['UniversalTime'].ctime
|
338
339
|
elsif e.extra.member? 'NTFS'
|
@@ -352,27 +353,27 @@ module Zip
|
|
352
353
|
false
|
353
354
|
end
|
354
355
|
|
355
|
-
def symlink?(
|
356
|
+
def symlink?(_filename)
|
356
357
|
false
|
357
358
|
end
|
358
359
|
|
359
|
-
def socket?(
|
360
|
+
def socket?(_filename)
|
360
361
|
false
|
361
362
|
end
|
362
363
|
|
363
|
-
def ftype(
|
364
|
-
@
|
364
|
+
def ftype(filename)
|
365
|
+
@mapped_zip.get_entry(filename).directory? ? 'directory' : 'file'
|
365
366
|
end
|
366
367
|
|
367
|
-
def readlink(
|
368
|
+
def readlink(_filename)
|
368
369
|
raise NotImplementedError, 'The readlink() function is not implemented'
|
369
370
|
end
|
370
371
|
|
371
|
-
def symlink(
|
372
|
+
def symlink(_filename, _symlink_name)
|
372
373
|
raise NotImplementedError, 'The symlink() function is not implemented'
|
373
374
|
end
|
374
375
|
|
375
|
-
def link(
|
376
|
+
def link(_filename, _symlink_name)
|
376
377
|
raise NotImplementedError, 'The link() function is not implemented'
|
377
378
|
end
|
378
379
|
|
@@ -380,46 +381,48 @@ module Zip
|
|
380
381
|
raise NotImplementedError, 'The pipe() function is not implemented'
|
381
382
|
end
|
382
383
|
|
383
|
-
def stat(
|
384
|
-
raise Errno::ENOENT,
|
385
|
-
|
384
|
+
def stat(filename)
|
385
|
+
raise Errno::ENOENT, filename unless exists?(filename)
|
386
|
+
|
387
|
+
ZipFsStat.new(self, filename)
|
386
388
|
end
|
387
389
|
|
388
390
|
alias lstat stat
|
389
391
|
|
390
|
-
def readlines(
|
391
|
-
open(
|
392
|
+
def readlines(filename)
|
393
|
+
self.open(filename, &:readlines)
|
392
394
|
end
|
393
395
|
|
394
|
-
def read(
|
395
|
-
@
|
396
|
+
def read(filename)
|
397
|
+
@mapped_zip.read(filename)
|
396
398
|
end
|
397
399
|
|
398
|
-
def popen(*args, &
|
399
|
-
::File.popen(*args, &
|
400
|
+
def popen(*args, &a_proc)
|
401
|
+
::File.popen(*args, &a_proc)
|
400
402
|
end
|
401
403
|
|
402
|
-
def foreach(
|
403
|
-
open(
|
404
|
+
def foreach(filename, sep = $INPUT_RECORD_SEPARATOR, &a_proc)
|
405
|
+
self.open(filename) { |is| is.each_line(sep, &a_proc) }
|
404
406
|
end
|
405
407
|
|
406
408
|
def delete(*args)
|
407
|
-
args.each do |
|
408
|
-
if directory?(
|
409
|
-
raise Errno::EISDIR, "Is a directory - \"#{
|
409
|
+
args.each do |filename|
|
410
|
+
if directory?(filename)
|
411
|
+
raise Errno::EISDIR, "Is a directory - \"#{filename}\""
|
410
412
|
end
|
411
|
-
|
413
|
+
|
414
|
+
@mapped_zip.remove(filename)
|
412
415
|
end
|
413
416
|
end
|
414
417
|
|
415
|
-
def rename(
|
416
|
-
@
|
418
|
+
def rename(file_to_rename, new_name)
|
419
|
+
@mapped_zip.rename(file_to_rename, new_name) { true }
|
417
420
|
end
|
418
421
|
|
419
422
|
alias unlink delete
|
420
423
|
|
421
|
-
def expand_path(
|
422
|
-
@
|
424
|
+
def expand_path(path)
|
425
|
+
@mapped_zip.expand_path(path)
|
423
426
|
end
|
424
427
|
end
|
425
428
|
|
@@ -430,76 +433,79 @@ module Zip
|
|
430
433
|
# The individual methods are not documented due to their
|
431
434
|
# similarity with the methods in Dir
|
432
435
|
class ZipFsDir
|
433
|
-
def initialize(
|
434
|
-
@
|
436
|
+
def initialize(mapped_zip)
|
437
|
+
@mapped_zip = mapped_zip
|
435
438
|
end
|
436
439
|
|
437
440
|
attr_writer :file
|
438
441
|
|
439
|
-
def new(
|
440
|
-
ZipFsDirIterator.new(entries(
|
442
|
+
def new(directory_name)
|
443
|
+
ZipFsDirIterator.new(entries(directory_name))
|
441
444
|
end
|
442
445
|
|
443
|
-
def open(
|
444
|
-
|
446
|
+
def open(directory_name)
|
447
|
+
dir_iter = new(directory_name)
|
445
448
|
if block_given?
|
446
449
|
begin
|
447
|
-
yield(
|
450
|
+
yield(dir_iter)
|
448
451
|
return nil
|
449
452
|
ensure
|
450
|
-
|
453
|
+
dir_iter.close
|
451
454
|
end
|
452
455
|
end
|
453
|
-
|
456
|
+
dir_iter
|
454
457
|
end
|
455
458
|
|
456
459
|
def pwd
|
457
|
-
@
|
460
|
+
@mapped_zip.pwd
|
458
461
|
end
|
459
462
|
alias getwd pwd
|
460
463
|
|
461
|
-
def chdir(
|
462
|
-
unless @file.stat(
|
463
|
-
raise Errno::EINVAL, "Invalid argument - #{
|
464
|
+
def chdir(directory_name)
|
465
|
+
unless @file.stat(directory_name).directory?
|
466
|
+
raise Errno::EINVAL, "Invalid argument - #{directory_name}"
|
464
467
|
end
|
465
|
-
|
468
|
+
|
469
|
+
@mapped_zip.pwd = @file.expand_path(directory_name)
|
466
470
|
end
|
467
471
|
|
468
|
-
def entries(
|
472
|
+
def entries(directory_name)
|
469
473
|
entries = []
|
470
|
-
foreach(
|
474
|
+
foreach(directory_name) { |e| entries << e }
|
471
475
|
entries
|
472
476
|
end
|
473
477
|
|
474
478
|
def glob(*args, &block)
|
475
|
-
@
|
479
|
+
@mapped_zip.glob(*args, &block)
|
476
480
|
end
|
477
481
|
|
478
|
-
def foreach(
|
479
|
-
unless @file.stat(
|
480
|
-
raise Errno::ENOTDIR,
|
482
|
+
def foreach(directory_name)
|
483
|
+
unless @file.stat(directory_name).directory?
|
484
|
+
raise Errno::ENOTDIR, directory_name
|
481
485
|
end
|
482
|
-
|
486
|
+
|
487
|
+
path = @file.expand_path(directory_name)
|
483
488
|
path << '/' unless path.end_with?('/')
|
484
489
|
path = Regexp.escape(path)
|
485
|
-
|
486
|
-
@
|
487
|
-
match =
|
490
|
+
subdir_entry_regex = Regexp.new("^#{path}([^/]+)$")
|
491
|
+
@mapped_zip.each do |filename|
|
492
|
+
match = subdir_entry_regex.match(filename)
|
488
493
|
yield(match[1]) unless match.nil?
|
489
494
|
end
|
490
495
|
end
|
491
496
|
|
492
|
-
def delete(
|
493
|
-
unless @file.stat(
|
494
|
-
raise Errno::EINVAL, "Invalid argument - #{
|
497
|
+
def delete(entry_name)
|
498
|
+
unless @file.stat(entry_name).directory?
|
499
|
+
raise Errno::EINVAL, "Invalid argument - #{entry_name}"
|
495
500
|
end
|
496
|
-
|
501
|
+
|
502
|
+
@mapped_zip.remove(entry_name)
|
497
503
|
end
|
498
504
|
alias rmdir delete
|
499
505
|
alias unlink delete
|
500
506
|
|
501
|
-
def mkdir(
|
502
|
-
@
|
507
|
+
def mkdir(entry_name, permissions = 0o755)
|
508
|
+
@mapped_zip.mkdir(entry_name, permissions)
|
503
509
|
end
|
504
510
|
|
505
511
|
def chroot(*_args)
|
@@ -510,37 +516,42 @@ module Zip
|
|
510
516
|
class ZipFsDirIterator # :nodoc:all
|
511
517
|
include Enumerable
|
512
518
|
|
513
|
-
def initialize(
|
514
|
-
@
|
519
|
+
def initialize(filenames)
|
520
|
+
@filenames = filenames
|
515
521
|
@index = 0
|
516
522
|
end
|
517
523
|
|
518
524
|
def close
|
519
|
-
@
|
525
|
+
@filenames = nil
|
520
526
|
end
|
521
527
|
|
522
|
-
def each(&
|
523
|
-
raise IOError, 'closed directory' if @
|
524
|
-
|
528
|
+
def each(&a_proc)
|
529
|
+
raise IOError, 'closed directory' if @filenames.nil?
|
530
|
+
|
531
|
+
@filenames.each(&a_proc)
|
525
532
|
end
|
526
533
|
|
527
534
|
def read
|
528
|
-
raise IOError, 'closed directory' if @
|
529
|
-
|
535
|
+
raise IOError, 'closed directory' if @filenames.nil?
|
536
|
+
|
537
|
+
@filenames[(@index += 1) - 1]
|
530
538
|
end
|
531
539
|
|
532
540
|
def rewind
|
533
|
-
raise IOError, 'closed directory' if @
|
541
|
+
raise IOError, 'closed directory' if @filenames.nil?
|
542
|
+
|
534
543
|
@index = 0
|
535
544
|
end
|
536
545
|
|
537
|
-
def seek(
|
538
|
-
raise IOError, 'closed directory' if @
|
539
|
-
|
546
|
+
def seek(position)
|
547
|
+
raise IOError, 'closed directory' if @filenames.nil?
|
548
|
+
|
549
|
+
@index = position
|
540
550
|
end
|
541
551
|
|
542
552
|
def tell
|
543
|
-
raise IOError, 'closed directory' if @
|
553
|
+
raise IOError, 'closed directory' if @filenames.nil?
|
554
|
+
|
544
555
|
@index
|
545
556
|
end
|
546
557
|
end
|
@@ -550,60 +561,65 @@ module Zip
|
|
550
561
|
class ZipFileNameMapper # :nodoc:all
|
551
562
|
include Enumerable
|
552
563
|
|
553
|
-
def initialize(
|
554
|
-
@
|
564
|
+
def initialize(zip_file)
|
565
|
+
@zip_file = zip_file
|
555
566
|
@pwd = '/'
|
556
567
|
end
|
557
568
|
|
558
569
|
attr_accessor :pwd
|
559
570
|
|
560
|
-
def find_entry(
|
561
|
-
@
|
571
|
+
def find_entry(filename)
|
572
|
+
@zip_file.find_entry(expand_to_entry(filename))
|
562
573
|
end
|
563
574
|
|
564
|
-
def get_entry(
|
565
|
-
@
|
575
|
+
def get_entry(filename)
|
576
|
+
@zip_file.get_entry(expand_to_entry(filename))
|
566
577
|
end
|
567
578
|
|
568
|
-
def get_input_stream(
|
569
|
-
@
|
579
|
+
def get_input_stream(filename, &a_proc)
|
580
|
+
@zip_file.get_input_stream(expand_to_entry(filename), &a_proc)
|
570
581
|
end
|
571
582
|
|
572
|
-
def get_output_stream(
|
573
|
-
@
|
583
|
+
def get_output_stream(filename, permissions = nil, &a_proc)
|
584
|
+
@zip_file.get_output_stream(
|
585
|
+
expand_to_entry(filename), permissions, &a_proc
|
586
|
+
)
|
574
587
|
end
|
575
588
|
|
576
589
|
def glob(pattern, *flags, &block)
|
577
|
-
@
|
590
|
+
@zip_file.glob(expand_to_entry(pattern), *flags, &block)
|
578
591
|
end
|
579
592
|
|
580
|
-
def read(
|
581
|
-
@
|
593
|
+
def read(filename)
|
594
|
+
@zip_file.read(expand_to_entry(filename))
|
582
595
|
end
|
583
596
|
|
584
|
-
def remove(
|
585
|
-
@
|
597
|
+
def remove(filename)
|
598
|
+
@zip_file.remove(expand_to_entry(filename))
|
586
599
|
end
|
587
600
|
|
588
|
-
def rename(
|
589
|
-
@
|
590
|
-
|
601
|
+
def rename(filename, new_name, &continue_on_exists_proc)
|
602
|
+
@zip_file.rename(
|
603
|
+
expand_to_entry(filename),
|
604
|
+
expand_to_entry(new_name),
|
605
|
+
&continue_on_exists_proc
|
606
|
+
)
|
591
607
|
end
|
592
608
|
|
593
|
-
def mkdir(
|
594
|
-
@
|
609
|
+
def mkdir(filename, permissions = 0o755)
|
610
|
+
@zip_file.mkdir(expand_to_entry(filename), permissions)
|
595
611
|
end
|
596
612
|
|
597
613
|
# Turns entries into strings and adds leading /
|
598
614
|
# and removes trailing slash on directories
|
599
615
|
def each
|
600
|
-
@
|
616
|
+
@zip_file.each do |e|
|
601
617
|
yield('/' + e.to_s.chomp('/'))
|
602
618
|
end
|
603
619
|
end
|
604
620
|
|
605
|
-
def expand_path(
|
606
|
-
expanded =
|
621
|
+
def expand_path(path)
|
622
|
+
expanded = path.start_with?('/') ? path : ::File.join(@pwd, path)
|
607
623
|
expanded.gsub!(/\/\.(\/|$)/, '')
|
608
624
|
expanded.gsub!(/[^\/]+\/\.\.(\/|$)/, '')
|
609
625
|
expanded.empty? ? '/' : expanded
|
@@ -611,8 +627,8 @@ module Zip
|
|
611
627
|
|
612
628
|
private
|
613
629
|
|
614
|
-
def expand_to_entry(
|
615
|
-
expand_path(
|
630
|
+
def expand_to_entry(path)
|
631
|
+
expand_path(path)[1..-1]
|
616
632
|
end
|
617
633
|
end
|
618
634
|
end
|