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

Allow Zip::Entry#extract to handle destinations with missing directories #255

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions lib/zip/entry.rb
Expand Up @@ -572,6 +572,7 @@ def create_file(dest_path, _continue_on_exists_proc = proc { Zip.continue_on_exi
raise ::Zip::DestinationFileExistsError,
"Destination '#{dest_path}' already exists"
end
::FileUtils.mkdir_p(::File.dirname(dest_path))
::File.open(dest_path, 'wb') do |os|
get_input_stream do |is|
set_extra_attributes_on_path(dest_path)
Expand Down
40 changes: 25 additions & 15 deletions test/file_extract_test.rb
Expand Up @@ -11,22 +11,11 @@ def setup
end

def test_extract
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
zf.extract(ENTRY_TO_EXTRACT, EXTRACTED_FILENAME)

assert(File.exist?(EXTRACTED_FILENAME))
AssertEntry.assert_contents(EXTRACTED_FILENAME,
zf.get_input_stream(ENTRY_TO_EXTRACT) { |is| is.read })

::File.unlink(EXTRACTED_FILENAME)

entry = zf.get_entry(ENTRY_TO_EXTRACT)
entry.extract(EXTRACTED_FILENAME)
assert_extract(EXTRACTED_FILENAME)
end

assert(File.exist?(EXTRACTED_FILENAME))
AssertEntry.assert_contents(EXTRACTED_FILENAME,
entry.get_input_stream { |is| is.read })
end
def test_extract_nested_file
assert_extract('test/data/generated/a/b/c/extEntry')
end

def test_extract_exists
Expand Down Expand Up @@ -80,4 +69,25 @@ def test_extract_non_entry_2
end
assert(!File.exist?(outFile))
end

private

def assert_extract(extracted_filename)
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
zf.extract(ENTRY_TO_EXTRACT, extracted_filename)

assert(File.exist?(extracted_filename))
AssertEntry.assert_contents(extracted_filename,
zf.get_input_stream(ENTRY_TO_EXTRACT) { |is| is.read })

::File.unlink(extracted_filename)

entry = zf.get_entry(ENTRY_TO_EXTRACT)
entry.extract(extracted_filename)

assert(File.exist?(extracted_filename))
AssertEntry.assert_contents(extracted_filename,
entry.get_input_stream { |is| is.read })
end
end
end