Skip to content

Instantly share code, notes, and snippets.

@iterion

iterion/encoding_fix.rb

Last active Dec 17, 2015
Embed
What would you like to do?
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