2010-11-30 16:27:59 +08:00
|
|
|
module Zip
|
2015-03-24 00:06:01 +08:00
|
|
|
class StreamableStream < DelegateClass(Entry) # nodoc:all
|
2010-11-30 16:27:59 +08:00
|
|
|
def initialize(entry)
|
|
|
|
super(entry)
|
2014-01-19 19:45:58 +08:00
|
|
|
dirname = if zipfile.is_a?(::String)
|
|
|
|
::File.dirname(zipfile)
|
|
|
|
else
|
|
|
|
'.'
|
|
|
|
end
|
|
|
|
@temp_file = Tempfile.new(::File.basename(name), dirname)
|
|
|
|
@temp_file.binmode
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_output_stream
|
|
|
|
if block_given?
|
|
|
|
begin
|
2014-01-19 19:45:58 +08:00
|
|
|
yield(@temp_file)
|
2010-11-30 16:27:59 +08:00
|
|
|
ensure
|
2014-01-19 19:45:58 +08:00
|
|
|
@temp_file.close
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
else
|
2014-01-19 19:45:58 +08:00
|
|
|
@temp_file
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_input_stream
|
2015-03-24 00:11:32 +08:00
|
|
|
unless @temp_file.closed?
|
2010-11-30 16:27:59 +08:00
|
|
|
raise StandardError, "cannot open entry for reading while its open for writing - #{name}"
|
|
|
|
end
|
2014-01-19 19:45:58 +08:00
|
|
|
@temp_file.open # reopens tempfile from top
|
|
|
|
@temp_file.binmode
|
2010-11-30 16:27:59 +08:00
|
|
|
if block_given?
|
|
|
|
begin
|
2014-01-19 19:45:58 +08:00
|
|
|
yield(@temp_file)
|
2010-11-30 16:27:59 +08:00
|
|
|
ensure
|
2014-01-19 19:45:58 +08:00
|
|
|
@temp_file.close
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
else
|
2014-01-19 19:45:58 +08:00
|
|
|
@temp_file
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
end
|
2014-01-19 19:45:58 +08:00
|
|
|
|
2010-11-30 16:27:59 +08:00
|
|
|
def write_to_zip_output_stream(aZipOutputStream)
|
|
|
|
aZipOutputStream.put_next_entry(self)
|
2013-06-03 02:33:03 +08:00
|
|
|
get_input_stream { |is| ::Zip::IOExtras.copy_stream(aZipOutputStream, is) }
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2014-04-05 05:32:11 +08:00
|
|
|
|
|
|
|
def clean_up
|
|
|
|
@temp_file.unlink
|
|
|
|
end
|
2010-11-30 16:27:59 +08:00
|
|
|
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.
|