Class: JavaClass::Classpath::TemporaryUnpacker

Inherits:
Object
  • Object
show all
Defined in:
lib/javaclass/classpath/temporary_unpacker.rb

Overview

Unpack a JAR (ZIP) into a temporary folder.

Author

Peter Kofler

Constant Summary collapse

COMMANDS =

Command templates for external too like 7zip or zip.

[
  # 7zip 9.20
  '7za x -bd -o<folder> -y <jar> 2>&1',
  # Unzip 5.42
  'unzip -o -qq <jar> -d <folder> 2>&1',
  # WinZip 8.1
  'WinZip32.exe -min -e -o <jar> <folder>',
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jarfile) ⇒ TemporaryUnpacker

Set the given jarfile to unpack.



26
27
28
29
30
31
32
33
34
# File 'lib/javaclass/classpath/temporary_unpacker.rb', line 26

def initialize(jarfile)
  @jarfile = jarfile

  if !defined?(@@unpack_strategies)
    # use unzip first, fallback by hand
    @@unpack_strategies = COMMANDS.map{ |c| Proc.new{ |jar, folder| TemporaryUnpacker::unpack_shell(c, jar, folder) } } + 
                          [ Proc.new{ |jar, folder| TemporaryUnpacker::unpack_ruby(jar, folder) } ]
  end
end

Instance Attribute Details

#folderObject (readonly)

The temporary folder. This folder will be deleted after Ruby shuts down.



23
24
25
# File 'lib/javaclass/classpath/temporary_unpacker.rb', line 23

def folder
  @folder
end

Instance Method Details

#create_temporary_folderObject

Create the temporary folder where it will be unpacked to.



37
38
39
40
41
42
# File 'lib/javaclass/classpath/temporary_unpacker.rb', line 37

def create_temporary_folder
  folder = File.join(find_temp_folder, "temp_#{File.basename(@jarfile)}_#{Time.now.to_i.to_s}")
  FileUtils.mkdir_p(folder)
  at_exit { FileUtils.rm_r(folder) }
  @folder = folder
end

#find_temp_folderObject

Return the temp folder if a variable is set, else returm /tmp.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/javaclass/classpath/temporary_unpacker.rb', line 62

def find_temp_folder
  TemporaryUnpacker::escape_folder(
  if ENV['TEMP']
    ENV['TEMP'] # Windows
  elsif ENV['TMP']
    ENV['TMP']
  else
    '/tmp'
  end
  )
end

#unpack!Object

Unpack the given jar file.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/javaclass/classpath/temporary_unpacker.rb', line 45

def unpack!
  unless defined?(@folder) && @folder
    raise IOError, 'no temporary folder created'
  end
    
  # Find the first working strategy and keep it
  if ! @@unpack_strategies.first.call(@jarfile, @folder)
    warn("Dropping unpacker for #{@jarfile}. Install 7zip or unzip!")
    @@unpack_strategies.delete_at(0)
    if @@unpack_strategies.empty?
      raise 'no suitable unpack strategy found'
    end
    unpack!
  end
end