在Ruby中调用FileUtils.copy_entry时出现编码错误

时间:2017-06-08 01:54:48

标签: ruby encoding copy

我正在编写用于递归复制和粘贴的代码。
但是在调用FileUtils.copy_entry

时出现编码错误

错误讯息:

C:/Ruby200/lib/ruby/2.0.0/fileutils.rb:1535:in `join': incompatible character encodings: CP949 and UTF-8 (Encoding::CompatibilityError)
    from C:/Ruby200/lib/ruby/2.0.0/fileutils.rb:1535:in `join'
    from C:/Ruby200/lib/ruby/2.0.0/fileutils.rb:1218:in `path'
    from C:/Ruby200/lib/ruby/2.0.0/fileutils.rb:463:in `block in copy_entry'
    from C:/Ruby200/lib/ruby/2.0.0/fileutils.rb:1485:in `call'
    from C:/Ruby200/lib/ruby/2.0.0/fileutils.rb:1485:in `wrap_traverse'
    from C:/Ruby200/lib/ruby/2.0.0/fileutils.rb:1488:in `block in wrap_traverse'
    from C:/Ruby200/lib/ruby/2.0.0/fileutils.rb:1487:in `each'
    from C:/Ruby200/lib/ruby/2.0.0/fileutils.rb:1487:in `wrap_traverse'
    from C:/Ruby200/lib/ruby/2.0.0/fileutils.rb:1488:in `block in wrap_traverse'
    from C:/Ruby200/lib/ruby/2.0.0/fileutils.rb:1487:in `each'
    from C:/Ruby200/lib/ruby/2.0.0/fileutils.rb:1487:in `wrap_traverse'
    from C:/Ruby200/lib/ruby/2.0.0/fileutils.rb:460:in `copy_entry'
    from C:/Ruby200/lib/ruby/2.0.0/fileutils.rb:435:in `block in cp_r'
    from C:/Ruby200/lib/ruby/2.0.0/fileutils.rb:1558:in `block in fu_each_src_dest'
    from C:/Ruby200/lib/ruby/2.0.0/fileutils.rb:1574:in `fu_each_src_dest0'
    from C:/Ruby200/lib/ruby/2.0.0/fileutils.rb:1556:in `fu_each_src_dest'
    from C:/Ruby200/lib/ruby/2.0.0/fileutils.rb:434:in `cp_r'
    from copy_test.rb:11:in `copy_files'
    from copy_test.rb:81:in `block in <main>'
    from copy_test.rb:76:in `each'
    from copy_test.rb:76:in `<main>'


我这样叫copy_entry

def copy_files(src, dst)  
  FileUtils.mkdir_p(File.dirname(dst))
  FileUtils.copy_entry(src, dst)
end

src中有一些以我的本地语言命名的子文件夹和文件 所以我认为(Encoding::CompatibilityError)是因为这些子文件夹和使用本地语言的文件(不是英语)而发生的。
当我只用Enlgish文件夹和文件测试时,它工作。
但是,我也需要非英文文件夹和文件。

我该如何解决这个问题呢? 我应该定义一个替换copy_entry的新方法吗?

我的代码已添加:

# -*- encoding: cp949 -*-
require 'fileutils'

LIST_FILE = "backup_target_list.txt"
DEST_FILE = "backup_dest.txt"

def copy_files(src, dst)  
  FileUtils.mkdir_p(File.dirname(dst))
  FileUtils.copy_entry(src, dst)
end

def get_dst_base_name()
  cur_date = Time.now.to_s[0..9]
  return "backup_#{cur_date}"
end

def get_backup_list(list_file)
  if !File.exist?(list_file) then
    return nil
  end

  path_arr = []

  File.open(list_file, "r") do |f|
    f.each_line { |line|
      path_arr.push(make_path(line.gsub("\n", "")))
    }
  end

  return path_arr
end

def get_dest(dest_file)
  if !File.exist?(dest_file) then
    return nil
  end

  return File.open(dest_file, "r").readline
end

def make_path(*str)
  path_new = nil

  str.each do |item|    
    if item.class == Array then
      path_new = (path_new == nil ? File.join(item) : File.join(path_new, item)) 
    else 
      if item.include?(File::ALT_SEPARATOR) then
        path_new = (path_new == nil ? File.join(item.split(File::ALT_SEPARATOR)) : File.join(path_new, item.split(File::ALT_SEPARATOR)))
      else
        path_new = (path_new == nil ? File.join(item) : File.join(path_new, item))
      end        
    end
  end

  return path_new
end

get_backup_list(LIST_FILE).each do |path|
  src = path
  tmp = src.split(File::SEPARATOR)
  dst = make_path(get_dest(DEST_FILE), get_dst_base_name, tmp[1..tmp.length])
  print "src: #{src}\n=> dst: #{dst}\n"
  copy_files(src, dst)
end

1 个答案:

答案 0 :(得分:0)

我看到你正在使用cp949编码,并且引发的错误告诉CP949和UTF-8是不兼容的,那你为什么只使用UTF-8编码呢?所以用

代替shebang
# coding: UTF-8

为了确保从LIST_FILE读取的所有字符都是utf-8编码,请添加以下内容:

line.force_encoding('utf-8')