Ruby zipfile delete file causing Errno::ENOENT

Viewed 14

I'm trying to work with zip file. I have a document I want to extract, add content to it, replace it and remove the one I extracted.

Whenever I just extract the file, add content, and then delete it - every thing works. But when I try to use replace - #zipfile.replace(document_entry, document_path) or add and then delete, I get different errors. If I just add the file without deleting it - it works ok. If I add the file and delete it or use replace function, it doesn't work and raises an error.

for example, when I use this code, I get: No such file or directory @ rb_sysopen - /home/shiran/Desktop/pop/app/documentfirst.xml (Errno::ENOENT) Any help would be appreciated, I'm stuck and have no idea what else to look for.

        document_entry = "word/document.xml" #this is the path in my zip I opened before
        document_path = "/home/shiran/Desktop/pop/app/documentfirst.xml"
        zipfile.extract(document_entry, document_path) 
        fd = File.open(document_path)
        file = Nokogiri::XML(fd)
#this is where I add content to my file which works ok
        wbody = file.xpath("//w:r")
        wtstart = Nokogiri::XML::Node.new "t", file 
        wtstart.content = "I am adding a new content after the header!"
        wbody.children.before(wtstart)
        File.write(document_path, file.to_xml)
        zipfile.remove(document_entry)
        zipfile.add(document_entry, document_path)
        fd.close
        file_exist = File.exist?(document_path)
        if file_exist
#raises an error here -
             File.delete(document_path) 
             puts "file exists" #(it does puts file exists)
        end
0 Answers
Related