class Rubib::Bib
Constants
- BIB_MAGIC
Attributes
entries[R]
name[R]
Public Class Methods
new(name, password = nil)
click to toggle source
# File lib/rubib.rb, line 43 def initialize(name, password = nil) @name = name @password = password @entries = nil end
Public Instance Methods
[](name)
click to toggle source
# File lib/rubib.rb, line 87 def [](name) read_index if @entries.nil? e = @entries[name] raise "Can not extract #{name} : data does not exists" if e.nil? raise "Can not extract #{name} : password required" if (1 == e.option) and (@password.nil?) data = nil bib_open do |file| file.seek(e.offset, IO::SEEK_SET) id = file.read(e.size) od = nil if 1 == e.option # EzCrypto key = EzCrypto::Key.with_password(@password, '', :algorithm => 'aes-128-cbc') od = key.decrypt(id) else od = id end z = Zlib::Inflate.new data = z.inflate(od) z.finish z.close end data end
create(dir_name, filter)
click to toggle source
# File lib/rubib.rb, line 71 def create(dir_name, filter) @entries = Hash.new # Initialises bib index d = Dir.new(dir_name) d.each do |f| if not ((f[0,1] == '.') and File.directory?(File.join(dir_name,f))) and (filter.include? File.extname(f)) @entries[f] = BibEntry.new end end pp @entries bib_write(dir_name) unless @entries.empty? end
read_index()
click to toggle source
# File lib/rubib.rb, line 49 def read_index bib_open do |file| magic = file.read_integer if BIB_MAGIC != magic raise "#{@name} is not a valid bib" end index_start_pos = file.read_big_integer file.seek(index_start_pos, IO::SEEK_SET) number_of_index_entries = file.read_integer @entries = Hash.new number_of_index_entries.times do size_of_entry_name = file.read_integer file_name = file.read(size_of_entry_name * 2) file_name.force_encoding('UTF-16LE').encode!('UTF-8') @entries[file_name] = file.read_bib_entry end end end
Private Instance Methods
bib_open() { |file| ... }
click to toggle source
# File lib/rubib.rb, line 118 def bib_open file = File.open(@name) file.extend ReadBinaryDataFromFile file.binmode if block_given? yield file file.close elsif file end end
bib_write(from_dir)
click to toggle source
# File lib/rubib.rb, line 130 def bib_write(from_dir) file = File.new(@name, "wb") #begin # Write bib header and reserve some space for future update of the index offset file << [BIB_MAGIC, 0].pack("Vq") # Write contents @entries.each_pair do |k,e| e.offset = file.pos File.open(File.join(from_dir, k)) do |f| f.binmode z = Zlib::Deflate.new(Zlib::BEST_COMPRESSION) cd = z.deflate(f.read, Zlib::FINISH) z.close if not @password.nil? key = EzCrypto::Key.with_password(@password, '', :algorithm => 'aes-128-cbc') ed = key.encrypt(cd) e.option = 1 else ed = cd e.option = 0 end file << ed end e.size = file.pos - e.offset end # Current position, where the index will be written index_pos = file.pos # Number of entries file << [@entries.length].pack("V") @entries.each_pair do |k, e| file << [k.length, k.downcase.encode('UTF-16LE')].pack("Va#{k.length * 2}") file << e.to_bin end # Write the offset where the index has been written file.pos = 4 file << ([index_pos].pack("q")) #ensure file.close #end end