rubyzip 壓縮文件夾時(shí)遇到Errno::EACCES (Permission denied @ rb_sysopen -

最近在做下載html頁(yè)面的時(shí)候,用了rubyzip包,它的說明文檔里有這樣一段可以直接拿來用的

require'zip'# This is a simple example which uses rubyzip to# recursively generate a zip file from the contents of# a specified directory. The directory itself is not# included in the archive, rather just its contents.#

# Usage:#? directory_to_zip = "/tmp/input"#? output_file = "/tmp/out.zip"#? zf = ZipFileGenerator.new(directory_to_zip, output_file)#? zf.write()class ZipFileGenerator

? # Initialize with the directory to zip and the location of the output archive.def initialize(input_dir, output_file)

? ? @input_dir = input_dir

? ? @output_file = output_file

? end

? # Zip the input directory.def write

? ? entries = Dir.entries(@input_dir) - %w(. ..)

? ? ::Zip::File.open(@output_file, ::Zip::File::CREATE) do |zipfile|? ? ? write_entries entries, '', zipfile

? ? end

? end

? private

? # A helper method to make the recursion work.def write_entries(entries, path, zipfile)

? ? entries.each do |e|? ? ? zipfile_path = path =='' ? e : File.join(path, e)

? ? ? disk_file_path = File.join(@input_dir, zipfile_path)

? ? ? puts "Deflating #{disk_file_path}"if File.directory? disk_file_path

? ? ? ? recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)

? ? ? else? ? ? ? put_into_archive(disk_file_path, zipfile, zipfile_path)

? ? ? end

? ? end

? end

? def recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)

? ? zipfile.mkdir zipfile_path

? ? subdir = Dir.entries(disk_file_path) - %w(. ..)

? ? write_entries subdir, zipfile_path, zipfile

? end

? def put_into_archive(disk_file_path, zipfile, zipfile_path)

? ? zipfile.get_output_stream(zipfile_path) do |f|? ? ? f.write(File.open(disk_file_path, 'rb').read)

? ? end

? end

end


于是按照說明直接使用,我的代碼如下:

report_path ="path/folder"# 某個(gè)path下的folderoutput_file = Rails.root.join(download_path,"report.zip")

File.delete(output_file) if File.exists?(output_file)

zf = ZipFileGenerator.new(report_path, output_file)

zf.write()

這時(shí)出現(xiàn)了Errno::EACCES (Permission denied @ rb_sysopen的錯(cuò)誤。

它的意思應(yīng)該是沒有權(quán)限打開文件。

按照這個(gè)思路分析,去排查權(quán)限的問題,但權(quán)限都是正常的,沒有什么問題。

再去看說明文檔,發(fā)現(xiàn)了其中的原由,文檔里有這么一段:

# Usage:#? directory_to_zip = "/tmp/input"#? output_file = "/tmp/out.zip"#? zf = ZipFileGenerator.new(directory_to_zip, output_file)#? zf.write()

這說明directory_to_zip和output_file都是string對(duì)象。

而我傳入的Rails.root.join(download_path,"report.zip"),是一個(gè)Pathname對(duì)象,這導(dǎo)致了錯(cuò)誤的出現(xiàn)。

我們平常在rails里較為常用的Rails.root.join和File.join的區(qū)別就是返回的對(duì)象不同,并且Rails.root使用時(shí)也可以像字符一樣,以致我都認(rèn)為它就是返回的字符串,所以才導(dǎo)致了這樣的錯(cuò)誤,以后還是要仔細(xì)些才行,不要再犯同樣的錯(cuò)誤,特此mark一下。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.創(chuàng)建文件夾 Dir.new %%1 Dir::mkdir #不指定目錄全名稱時(shí),缺省為工作目錄 Dir::ch...
    素還真人閱讀 571評(píng)論 0 0
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,539評(píng)論 19 139
  • 謀事在人成事在天,凡事盡力就好,事事追求完美是強(qiáng)迫癥狀。完美與完整的差一字卻有大智慧。 隨緣是尊重天時(shí)地利人和順勢(shì)...
    纖雨纖陽(yáng)心理護(hù)航閱讀 219評(píng)論 0 0
  • 1. 遙遠(yuǎn)的救世主 2. 現(xiàn)代管理 3. 如何高效的閱讀 4. 心智探索 5. 如何高效學(xué)習(xí) 6. 精力管理 我想...
    dc55e841db1c閱讀 116評(píng)論 0 1
  • 說起吃,學(xué)校四個(gè)食堂,東西北清真,各有特色,各有亮點(diǎn)。 先從清真食堂說起吧。煲仔飯是招牌,煎得焦焦的米飯,藏在鍋底...
    荔枝若夫閱讀 1,458評(píng)論 0 0

友情鏈接更多精彩內(nèi)容