lib/roo/openoffice.rb in roo-0.5.2 vs lib/roo/openoffice.rb in roo-0.5.3
- old
+ new
@@ -11,26 +11,27 @@
class Openoffice
@@nr = 0
- # initialization and opening of a spreasheet file
- # file will be created if 'create' is true
- # and the file does not exist
+ # initialization and opening of a spreadsheet file
+ # values for packed: :zip
def initialize(filename, packed=nil) #, create = false)
+ @tmpdir = "oo_"+$$.to_s
+ unless File.exists?(@tmpdir)
+ FileUtils::mkdir(@tmpdir)
+ end
+ filename = open_from_uri(filename) if filename[0,7] == "http://"
+ filename = unzip(filename) if packed and packed == :zip
if filename[-4..-1] != ".ods"
warn "are you sure, this is an openoffice file?"
end
#if create and ! File.exists?(filename)
# self.create_openoffice(filename)
#end
@cells_read = false
@filename = filename
- @tmpdir = "oo_"+$$.to_s
- unless File.exists?(@tmpdir)
- FileUtils::mkdir(@tmpdir)
- end
@@nr += 1
@file_nr = @@nr
extract_content
file = File.new(File.join(@tmpdir, @file_nr.to_s+"_roo_content.xml"))
@doc = REXML::Document.new file
@@ -58,11 +59,11 @@
#TODO: a better way for creating the file contents
# now you have to call mkbase64...rb to create an include file with all
# the empty files in an openoffice zip-file
load 'base64include.rb'
# puts @@empty_spreadsheet
- f = File.open(filename,'w')
+ f = File.open(filename,'wb')
f.print(Base64.decode64(@@empty_spreadsheet))
f.close
end
#if false
@@ -152,10 +153,16 @@
else
@cell_type["#{row},#{col}"]
end
end
+ def remove_tmp
+ if File.exists?(@tmpdir)
+ FileUtils::rm_r(@tmpdir)
+ end
+ end
+
# returns an array of sheet names in the spreadsheet
def sheets
return_sheets = []
oo_document_count = 0
@doc.each_element do |oo_document|
@@ -376,10 +383,43 @@
end
end
result
end
+protected
+
+ def process_zipfile_packed(zip, path='')
+ ret=nil
+ if zip.file.file? path
+ # extract and return filename
+ @tmpdir = "oo_"+$$.to_s
+ unless File.exists?(@tmpdir)
+ FileUtils::mkdir(@tmpdir)
+ end
+ file = File.open(File.join(@tmpdir, path),"wb")
+ file.write(zip.read(path))
+ file.close
+ return File.join(@tmpdir, path)
+ else
+ unless path.empty?
+ path += '/'
+ end
+ zip.dir.foreach(path) do |filename|
+ ret = process_zipfile_packed(zip, path + filename)
+ end
+ end
+ ret
+ end
+
+ def unzip(filename)
+ ret = nil
+ Zip::ZipFile.open(filename) do |zip|
+ ret = process_zipfile_packed zip
+ end
+ ret
+ end
+
private
# read the version of the OO-Version
def oo_version
sheet_found = false
@doc.each_element do |oo_document|
@@ -514,11 +554,11 @@
end
def process_zipfile(zip, path='')
if zip.file.file? path
if path == "content.xml"
- open(@tmpdir+'/'+@file_nr.to_s+'_roo_content.xml','w') {|f|
+ open(@tmpdir+'/'+@file_nr.to_s+'_roo_content.xml','wb') {|f|
f << zip.read(path)
}
end
else
unless path.empty?
@@ -593,7 +633,23 @@
"percentage" => :percentage,
}
def Openoffice.oo_type_2_roo_type(ootype)
return A_ROO_TYPE[ootype]
+ end
+
+
+ def open_from_uri(uri)
+ require 'open-uri' ;
+ tempfilename = File.join(@tmpdir, File.basename(uri))
+ f = File.open(tempfilename,"wb")
+ begin
+ open(uri) do |net|
+ f.write(net.read)
+ end
+ rescue
+ raise "could not open #{uri}"
+ end
+ f.close
+ File.join(@tmpdir, File.basename(uri))
end
end # class