Class: Android::Apk
- Inherits:
-
Object
- Object
- Android::Apk
- Defined in:
- lib/android/apk.rb
Overview
apk object class
Constant Summary collapse
- MANIFEST =
AndroidManifest file name
'AndroidManifest.xml'
- DEX =
dex file name
'classes.dex'
- RESOURCE =
resource file name
'resources.arsc'
Instance Attribute Summary collapse
-
#bindata ⇒ String
readonly
Binary data of apk.
- #dex ⇒ Android::Dex? readonly
- #manifest ⇒ Android::Manifest? readonly
-
#path ⇒ String
readonly
Apk file path.
- #resource ⇒ Resource? readonly
Instance Method Summary collapse
-
#digest(type = :sha1) ⇒ String
return hex digest string of apk file.
- #each_entry {|entry| ... } ⇒ Object
- #each_file {|name, data| ... } ⇒ Object
-
#entry(name) ⇒ Zip::ZipEntry
find and return zip entry with name.
-
#file(name) ⇒ String
find and return binary data with name.
-
#find {|name, data| ... } ⇒ Array
find files which is matched with block condition.
-
#initialize(filepath) ⇒ Apk
constructor
create new apk object.
-
#size ⇒ Integer
return apk file size.
-
#time ⇒ Time
returns date of AndroidManifest.xml as Apk date.
Constructor Details
#initialize(filepath) ⇒ Apk
create new apk object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/android/apk.rb', line 38 def initialize(filepath) @path = filepath raise NotFoundError, "'#{filepath}'" unless File.exist? @path begin @zip = Zip::ZipFile.open(@path) rescue Zip::ZipError => e raise NotApkFileError, e. end @bindata = File.open(@path, 'rb') {|f| f.read } @bindata.force_encoding(Encoding::ASCII_8BIT) raise NotApkFileError, "manifest file is not found." if @zip.find_entry(MANIFEST).nil? begin @manifest = Android::Manifest.new(self.file(MANIFEST)) rescue => e $stderr.puts "failed to parse manifest:#{e}" #$stderr.puts e.backtrace end begin @dex = Android::Dex.new(self.file(DEX)) rescue => e $stderr.puts "failed to parse dex:#{e}" #$stderr.puts e.backtrace end begin @resource = Android::Resource.new(self.file(RESOURCE)) rescue => e $stderr.puts "failed to parse resource:#{e}" #$stderr.puts e.backtrace end end |
Instance Attribute Details
#bindata ⇒ String (readonly)
Returns binary data of apk.
22 23 24 |
# File 'lib/android/apk.rb', line 22 def bindata @bindata end |
#manifest ⇒ Android::Manifest? (readonly)
17 18 19 |
# File 'lib/android/apk.rb', line 17 def manifest @manifest end |
#path ⇒ String (readonly)
Returns apk file path.
14 15 16 |
# File 'lib/android/apk.rb', line 14 def path @path end |
#resource ⇒ Resource? (readonly)
25 26 27 |
# File 'lib/android/apk.rb', line 25 def resource @resource end |
Instance Method Details
#digest(type = :sha1) ⇒ String
return hex digest string of apk file
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/android/apk.rb', line 80 def digest(type = :sha1) case type when :sha1 Digest::SHA1.hexdigest(@bindata) when :sha256 Digest::SHA256.hexdigest(@bindata) when :md5 Digest::MD5.hexdigest(@bindata) else raise ArgumentError end end |
#each_entry {|entry| ... } ⇒ Object
119 120 121 122 123 124 |
# File 'lib/android/apk.rb', line 119 def each_entry @zip.each do |entry| next unless entry.file? yield entry end end |
#each_file {|name, data| ... } ⇒ Object
102 103 104 105 106 107 |
# File 'lib/android/apk.rb', line 102 def each_file @zip.each do |entry| next unless entry.file? yield entry.name, @zip.read(entry) end end |
#entry(name) ⇒ Zip::ZipEntry
find and return zip entry with name
130 131 132 133 134 |
# File 'lib/android/apk.rb', line 130 def entry(name) entry = @zip.find_entry(name) raise NotFoundError, "'#{name}'" if entry.nil? return entry end |
#file(name) ⇒ String
find and return binary data with name
113 114 115 |
# File 'lib/android/apk.rb', line 113 def file(name) # get data by entry name(path) @zip.read(entry(name)) end |
#find {|name, data| ... } ⇒ Array
find files which is matched with block condition
145 146 147 148 149 150 151 152 |
# File 'lib/android/apk.rb', line 145 def find(&block) found = [] self.each_file do |name, data| ret = block.call(name, data) found << name if ret end found end |
#size ⇒ Integer
return apk file size
72 73 74 |
# File 'lib/android/apk.rb', line 72 def size @bindata.size end |
#time ⇒ Time
returns date of AndroidManifest.xml as Apk date
95 96 97 |
# File 'lib/android/apk.rb', line 95 def time entry(MANIFEST).time end |