Search code examples
ruby-on-railsangularjsrubyrubyzip

resource lock while generating the zip using Zipfilegenerator class, Ruby


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.


Solution

  • 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