Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to create temp files in the zip file directory #432

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/zip/file.rb
Expand Up @@ -54,7 +54,8 @@ class File < CentralDirectory
DEFAULT_OPTIONS = {
restore_ownership: false,
restore_permissions: false,
restore_times: false
restore_times: false,
use_system_temp_dir: true
}.freeze

attr_reader :name
Expand Down Expand Up @@ -111,6 +112,7 @@ def initialize(path_or_io, create = false, buffer = false, options = {})
@restore_ownership = options[:restore_ownership]
@restore_permissions = options[:restore_permissions]
@restore_times = options[:restore_times]
@use_system_temp_dir = options[:use_system_temp_dir]
end

class << self
Expand Down Expand Up @@ -272,7 +274,7 @@ def get_output_stream(entry, permission_int = nil, comment = nil, extra = nil, c
"cannot open stream to directory entry - '#{new_entry}'"
end
new_entry.unix_perms = permission_int
zip_streamable_entry = StreamableStream.new(new_entry)
zip_streamable_entry = StreamableStream.new(new_entry, @use_system_temp_dir)
@entry_set << zip_streamable_entry
zip_streamable_entry.get_output_stream(&aProc)
end
Expand Down
9 changes: 7 additions & 2 deletions lib/zip/streamable_stream.rb
@@ -1,8 +1,13 @@
module Zip
class StreamableStream < DelegateClass(Entry) # nodoc:all
def initialize(entry)
def initialize(entry, use_system_temp_dir)
super(entry)
@temp_file = Tempfile.new(::File.basename(name))
dirname = if use_system_temp_dir == false && zipfile.is_a?(::String)
::File.dirname(zipfile)
else
nil
end
@temp_file = Tempfile.new(::File.basename(name), dirname)
@temp_file.binmode
end

Expand Down
31 changes: 31 additions & 0 deletions test/streamable_stream_test.rb
@@ -0,0 +1,31 @@
require 'test_helper'

class FakeEntry
def name
'something'
end

def zipfile
'data/important_stuff.zip'
end
end

class StreamableStreamTest < MiniTest::Test
def test_use_system_temp_dir_true
entry = FakeEntry.new
stream = ::Zip::StreamableStream.new(entry, true)
stream.get_output_stream do |temp_file|
assert(temp_file.path.start_with?(Dir.tmpdir))
end
end

def test_use_system_temp_dir_false
entry = FakeEntry.new
FileUtils.mkdir_p(File.dirname(entry.zipfile))
stream = ::Zip::StreamableStream.new(entry, false)
stream.get_output_stream do |temp_file|
assert(temp_file.path.start_with?('data/'))
end
FileUtils.rm_r(File.dirname(entry.zipfile))
end
end