Class: OoxmlParser::OoxmlFile

Inherits:
Object
  • Object
show all
Defined in:
lib/ooxml_parser/common_parser/parser/ooxml_file.rb

Overview

Class for actions with OOXML file

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#pathString (readonly)

Returns path to file.

Returns:

  • (String)

    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_zipString

Copy this file and rename to zip

Returns:

  • (String)

    path to result 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

Parameters:

  • password (String)

    password to file

Returns:



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_foldersSymbol

Returns file type recognized by folder structure.

Returns:

  • (Symbol)

    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_folderString

Returns path to folder with zip.

Returns:

  • (String)

    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

#unzipvoid

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