resource lock while generating the zip using Zipfilegenerator class, Ruby

纵饮孤独 提交于 2019-12-11 08:39:07

问题


I have an applicaiton in angularJS and ruby on rails.

I am generating the subfolder and html files through code. Like my folder structure is like that

-Root

-----Sub1

----------Sub_Sub1

----------Sub_Sub2

----------index.html

-----Sub2

-----abc.html

I have generated the zip file of the directory through Zipfilegenerator.

I have made the functionality so that before generating the directory the system checks if any previous directory exists with the same name then it will delete that and then generates the new directory. Here is my code to delete folder and files.

#delete all files
Dir.glob("path/to/dir/.") do |rb_file|
File.delete(rb_file)
end

FileUtils.rm_rf("path/to/dir/") #delete all sub directory
FileUtils.mkdir("path/to/dir/") #create root directory

Here is the code to create zip

zf = ZipFileGenerator.new(directory_to_zip, output_file) 
zf.write()

Now i have a strange problem that first time everything works fine directory generates zip file generates but the second time the system fails to delete the files in the Root and Sub Folder that was created in the first attempt.

I have investigate the issue and found that when i comment the zip creation code then the files got deleted successfully.

So i am facing resource lock problem while generating the zip from directory. Can you please help me to sort out the problem.


回答1:


I have solved it using this code. I have changed the code in rubyzip class

disk_file = File.open(diskFilePath, "rb")
io.get_output_stream(zipFilePath) { |f|
f.puts(disk_file.read())
}
disk_file.close


来源:https://stackoverflow.com/questions/42866356/resource-lock-while-generating-the-zip-using-zipfilegenerator-class-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!