Class: OoxmlParser::OoxmlFile
- Inherits:
-
Object
- Object
- OoxmlParser::OoxmlFile
- Defined in:
- lib/ooxml_parser/common_parser/parser/ooxml_file.rb
Overview
Class for actions with OOXML file
Instance Attribute Summary collapse
-
#path ⇒ String
readonly
Path to file.
Instance Method Summary collapse
-
#copy_file_and_rename_to_zip ⇒ String
Copy this file and rename to zip.
-
#decrypt(password) ⇒ OoxmlFile
Decrypt file protected with password.
-
#format_by_folders ⇒ Symbol
File type recognized by folder structure.
-
#initialize(path) ⇒ OoxmlFile
constructor
A new instance of OoxmlFile.
-
#path_to_folder ⇒ String
Path to folder with zip.
-
#unzip ⇒ void
Unzip specified file.
Constructor Details
#initialize(path) ⇒ OoxmlFile
Returns a new instance of OoxmlFile.
9 10 11 |
# File 'lib/ooxml_parser/common_parser/parser/ooxml_file.rb', line 9 def initialize(path) @path = path end |
Instance Attribute Details
#path ⇒ String (readonly)
Returns path to file.
7 8 9 |
# File 'lib/ooxml_parser/common_parser/parser/ooxml_file.rb', line 7 def path @path end |
Instance Method Details
#copy_file_and_rename_to_zip ⇒ String
Copy this file and rename to zip
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ooxml_parser/common_parser/parser/ooxml_file.rb', line 15 def copy_file_and_rename_to_zip file_name = File.basename(@path) tmp_folder = Dir.mktmpdir('ruby-ooxml-parser') @zip_path = "#{tmp_folder}/#{file_name}" FileUtils.rm_rf(tmp_folder) if File.directory?(tmp_folder) FileUtils.mkdir_p(tmp_folder) raise "Cannot find file by path #{@path}" unless File.exist?(@path) FileUtils.cp path, tmp_folder end |
#decrypt(password) ⇒ OoxmlFile
Decrypt file protected with password
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ooxml_parser/common_parser/parser/ooxml_file.rb', line 58 def decrypt(password) check_decryption_support file_name = File.basename(@path) tmp_folder = Dir.mktmpdir('ruby-ooxml-parser') decrypted_path = "#{tmp_folder}/#{file_name}" binary_password = password.encode('utf-16le').bytes.pack('c*').encode('binary') OoxmlDecrypt::EncryptedFile.decrypt_to_file(@path, binary_password, decrypted_path) OoxmlFile.new(decrypted_path) end |
#format_by_folders ⇒ Symbol
Returns file type recognized by folder structure.
47 48 49 50 51 52 53 |
# File 'lib/ooxml_parser/common_parser/parser/ooxml_file.rb', line 47 def format_by_folders return :docx if Dir.exist?("#{path_to_folder}/word") return :xlsx if Dir.exist?("#{path_to_folder}/xl") return :pptx if Dir.exist?("#{path_to_folder}/ppt") :zip end |
#path_to_folder ⇒ String
Returns path to folder with zip.
27 28 29 |
# File 'lib/ooxml_parser/common_parser/parser/ooxml_file.rb', line 27 def path_to_folder @zip_path.sub(File.basename(@zip_path), '') end |
#unzip ⇒ void
This method returns an undefined value.
Unzip specified file
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ooxml_parser/common_parser/parser/ooxml_file.rb', line 33 def unzip Zip.warn_invalid_date = false Zip::File.open(@zip_path) do |zip_file| raise LoadError, "There is no files in zip #{@zip_path}" if zip_file.entries.empty? zip_file.each do |file| file_path = File.join(path_to_folder, file.name) FileUtils.mkdir_p(File.dirname(file_path)) zip_file.extract(file, file_path) unless File.exist?(file_path) end end end |