2021-05-24 01:24:22 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2010-11-30 16:27:59 +08:00
|
|
|
module Zip
|
2020-02-09 23:23:35 +08:00
|
|
|
class StreamableStream < DelegateClass(Entry) # :nodoc:all
|
2010-11-30 16:27:59 +08:00
|
|
|
def initialize(entry)
|
|
|
|
super(entry)
|
2019-09-27 23:13:56 +08:00
|
|
|
@temp_file = Tempfile.new(::File.basename(name))
|
2014-01-19 19:45:58 +08:00
|
|
|
@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
|
2020-02-09 21:13:21 +08:00
|
|
|
|
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
|
|
|
|
2020-02-19 15:28:46 +08:00
|
|
|
def write_to_zip_output_stream(output_stream)
|
|
|
|
output_stream.put_next_entry(self)
|
|
|
|
get_input_stream { |is| ::Zip::IOExtras.copy_stream(output_stream, 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.
|