Description
Scenario:
I have a function that downloads the zipped code from aws s3-bucket. Extracts the zip file in a temp folder and then modifies few files from the extracted zip file and create a zip after modification. Later when I try to delete the temp folder it throws me "The process cannot access the file because it is being used by another process."
(NOTE: everything takes place in the same process meaning in the same function).
After carefully looking the code found in the below code
def writeEntries(entries, path, io)
entries.each { |e|
zipFilePath = path == "" ? e : File.join(path, e)
diskFilePath = File.join(@inputDir, zipFilePath)
puts "Deflating " + diskFilePath
if File.directory?(diskFilePath)
io.mkdir(zipFilePath)
subdir =Dir.entries(diskFilePath); subdir.delete("."); subdir.delete("..")
writeEntries(subdir, zipFilePath, io)
else
io.get_output_stream(zipFilePath) { |f| f.puts(File.open(diskFilePath, "rb").read())}
end
}
end
In line io.get_output_stream(zipFilePath) { |f| f.puts(File.open(diskFilePath, "rb").read())}, the file is opened but not closed.
Solution :
So I modified the above line of code as follows:
disk_file = File.open(diskFilePath, "rb")
io.get_output_stream(zipFilePath) { |f|
f.puts(disk_file.read())
}
disk_file.close
and was successfully able to delete the temp folder.
Would be great if the modification can be included in the file.
Thanks,
Zaid