Skip to content

Commit 96eefa0

Browse files
committed
Fix extract path
1 parent 636e2b5 commit 96eefa0

3 files changed

Lines changed: 7 additions & 17 deletions

File tree

lib/doc2text/xml_based_document_file.rb

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'zip'
2-
require 'tmpdir'
32

43
module Doc2Text
54
module XmlBasedDocument
@@ -9,22 +8,13 @@ def initialize(document_path)
98
end
109

1110
def unpack
12-
destination_root = Pathname.new(extract_path).realpath
13-
1411
Zip::File.open(@document_path) {
1512
|zip_file|
13+
Dir.mkdir(extract_path)
1614
zip_file.each do |entry|
17-
entry_path = Pathname.new(entry.name)
18-
19-
next if entry_path.absolute?
20-
destination_path = destination_root.join(entry.name).cleanpath
21-
22-
unless destination_path.to_s.start_with?(destination_root.to_s + File::SEPARATOR)
23-
raise "Unsafe zip entry: #{entry.name}"
24-
end
25-
26-
FileUtils.mkdir_p(destination_path.dirname)
27-
zip_file.extract entry, entry.name, destination_directory: extract_path
15+
zipped_file_extract_path = File.join extract_path, File.dirname(entry.name)
16+
FileUtils.mkdir_p zipped_file_extract_path
17+
zip_file.extract entry, nil, destination_directory: extract_path
2818
end
2919
}
3020
end
@@ -51,7 +41,7 @@ def extract_extension
5141
end
5242

5343
def extract_path
54-
@extract_path ||= Dir.mktmpdir(".#{File.basename(@document_path)}_#{extract_extension}")
44+
@extract_path ||= File.join File.dirname(@document_path), ".#{File.basename(@document_path)}_#{extract_extension}"
5545
end
5646
end
5747
end

spec/unpack_docx_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def rspec_extract_docx
88
entries = Dir.glob "#{@odt.extract_path}/**/*"
99
mandatory_files = %w([Content_Types].xml).map { |entry|
1010
File.join @odt.extract_path, entry }
11-
expect(mandatory_files.to_set).to be_subset(entries.to_set)
11+
expect(entries.to_set.subset? mandatory_files.to_set)
1212

1313
@odt.clean
1414
end

spec/unpack_odt_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def rspec_extract_odt
99
entries = Dir.glob "#{@odt.extract_path}/**/*"
1010
mandatory_files = %w(manifest.rdf content.xml settings.xml styles.xml META-INF META-INF/manifest.xml meta.xml mimetype).map { |entry|
1111
File.join @odt.extract_path, entry }
12-
expect(mandatory_files.to_set).to be_subset(entries.to_set)
12+
expect(entries.to_set.subset? mandatory_files.to_set)
1313

1414
@odt.clean
1515
end

0 commit comments

Comments
 (0)