File.delete上的Ruby(Errno::EACCES)-以前的解决方案不';行不通
我的问题与这个问题的海报完全相同:。与他不同,解决方案中为他提供的更改对我不起作用 这是我的代码,这是一个压缩算法,我想删除原始文件:File.delete上的Ruby(Errno::EACCES)-以前的解决方案不';行不通,ruby,chmod,delete-file,Ruby,Chmod,Delete File,我的问题与这个问题的海报完全相同:。与他不同,解决方案中为他提供的更改对我不起作用 这是我的代码,这是一个压缩算法,我想删除原始文件: uncompressed_file = File.new(Rails.root + filepath) compressed_file = File.new(Rails.root + "#{filepath[0..filepath.size - 1]}.gz", "w+b") file_writer = Zlib::GzipWriter.new(compress
uncompressed_file = File.new(Rails.root + filepath)
compressed_file = File.new(Rails.root + "#{filepath[0..filepath.size - 1]}.gz", "w+b")
file_writer = Zlib::GzipWriter.new(compressed_file)
buf = ""
File.open(uncompressed_file, "rb") do | uncompressed |
while uncompressed.read(4096, buf)
file_writer << buf
end
file_writer.close
end
begin
files_changed_by_chmod = File.chmod(0777, uncompressed_file)
rescue
puts "Something happened"
end
puts "Number of files changed by CHMOD : " + files_changed_by_chmod.to_s
File.delete(uncompressed_file)
File.rename(Rails.root + "#{filepath[0..filepath.size - 1]}.gz", Rails.root + filepath)
我想这可能与你没有正确关闭文件有关。我冒昧地重写了你的代码,没有chmod的东西(我认为这是不必要的)
filename=
filename_gz=filename+“.gz”
filepath=Rails.root+文件名
filepath\u gz=Rails.root+filename\u gz
#gzip文件
buffer=“”
File.open(filepath)do | File|
Zlib::GzipWriter.open(filepath_gz)do|gz|
while file.read(4096,缓冲区)
gz Hi Sascha,非常感谢您的回复。它看起来非常优雅,代码确实允许我删除原始文件——但是,它会导致压缩文件失去完整性(在我对其进行加密、解密并再次解压缩之后)。我将用代码的其余部分更新原始问题。很高兴我至少可以帮助解决文件删除问题。事实证明,代码中的其他内容完全导致了完整性崩溃的问题。实际上,正是这个针对删除问题的修复程序暴露了另一个问题(它与路径有关)。再次感谢萨沙。
def inflate_attachment(filepath)
compressed_file = File.new(Rails.root + filepath)
File.open(compressed_file, "rb") do | compressed |
File.open(Rails.root + "#{filepath[0..filepath.size - 7]}_FULL.enc", 'w+b') do | decompressed |
gz = Zlib::GzipReader.new(compressed)
result = gz.read
decompressed.write(result)
gz.close
end
end
end
def encrypt_attachment(filepath, cipher)
unencrypted_file = File.new(Rails.root + filepath)
encrypted_file = File.new(Rails.root + "#{filepath[0..filepath.size - 1]}.enc", "w")
buf = ""
File.open(encrypted_file, "wb") do |outf|
File.open(unencrypted_file, "rb") do |inf|
while inf.read(4096, buf)
outf << cipher.update(buf)
end
outf << cipher.final
end
end
end
def decrypt_attachment(filepath, key, iv)
cipher = OpenSSL::Cipher.new(ENCRYPTION_TYPE)
cipher.decrypt
cipher.key = key
cipher.iv = iv
encrypted_file = File.new(Rails.root + filepath)
decrypted_file = File.new(Rails.root + "#{filepath[0..filepath.size - 5]}.dec", "w")
buf = ""
File.open(decrypted_file, "wb") do |outf|
File.open(encrypted_file, "rb") do |inf|
while inf.read(4096, buf)
outf << cipher.update(buf)
end
outf << cipher.final
end
end
end
filename = <your sourcefilename goes here>
filename_gz = filename + ".gz"
filepath = Rails.root + filename
filepath_gz = Rails.root + filename_gz
# gzip the file
buffer = ""
File.open(filepath) do |file|
Zlib::GzipWriter.open(filepath_gz) do |gz|
while file.read(4096, buffer)
gz << buffer
end
end
end
# moves the filepath_gz to filepath (overwriting the original file in the process!)
FileUtils.mv(filepath_gz, filepath)