rubyzip 压缩文件夹时遇到Errno::EACCES (Permission denied @ rb_sysopen -

最近在做下载html页面的时候,用了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"# 某个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()

这时出现了Errno::EACCES (Permission denied @ rb_sysopen的错误。

它的意思应该是没有权限打开文件。

按照这个思路分析,去排查权限的问题,但权限都是正常的,没有什么问题。

再去看说明文档,发现了其中的原由,文档里有这么一段:

# 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对象。

而我传入的Rails.root.join(download_path,"report.zip"),是一个Pathname对象,这导致了错误的出现。

我们平常在rails里较为常用的Rails.root.join和File.join的区别就是返回的对象不同,并且Rails.root使用时也可以像字符一样,以致我都认为它就是返回的字符串,所以才导致了这样的错误,以后还是要仔细些才行,不要再犯同样的错误,特此mark一下。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.创建文件夹 Dir.new %%1 Dir::mkdir #不指定目录全名称时,缺省为工作目录 Dir::ch...
    素还真人阅读 531评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,253评论 19 139
  • 谋事在人成事在天,凡事尽力就好,事事追求完美是强迫症状。完美与完整的差一字却有大智慧。 随缘是尊重天时地利人和顺势...
    纤雨纤阳心理护航阅读 208评论 0 0
  • 1. 遥远的救世主 2. 现代管理 3. 如何高效的阅读 4. 心智探索 5. 如何高效学习 6. 精力管理 我想...
    dc55e841db1c阅读 107评论 0 1
  • 说起吃,学校四个食堂,东西北清真,各有特色,各有亮点。 先从清真食堂说起吧。煲仔饭是招牌,煎得焦焦的米饭,藏在锅底...
    荔枝若夫阅读 1,342评论 0 0