Last active
December 17, 2015 09:19
-
-
Save iterion/5586055 to your computer and use it in GitHub Desktop.
char encoding fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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