Skip to content

Instantly share code, notes, and snippets.

@iterion
Last active December 17, 2015 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iterion/5586055 to your computer and use it in GitHub Desktop.
Save iterion/5586055 to your computer and use it in GitHub Desktop.
char encoding fix
def try_invalid_char_fix file
temp = Tempfile.new('fix_file')
File.open(file, "r").each_line do |line|
original_encoding = line.encoding.name
new_line = line.encode('UTF-16', original_encoding, :invalid => :replace, :replace => '')
temp.write new_line.encode('UTF-8', 'UTF-16')
end
temp.close
begin
#make copy of the old file
FileUtils.mv(file, file + ".old")
#copy repair file over old file
FileUtils.mv(temp.path, file)
#Open permissions
File.chmod(0644, file + ".old")
File.chmod(0644, file)
rescue => error
puts "Failed to move file after repair: " + error.to_s
end
temp.unlink
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment