What I want to solve
The following error occurs when downloading that file, compressed into a single zip file.
invalid byte sequence in UTF-8.
For this error, I have to remove illegal characters as UTF-8 from the string, so I used encode method to convert from UTF-8 to UTF-8, but the string I want to display is not displayed. It looks like the image.
file_name.encode!("UTF-8", "UTF-8", invalid: :replace)
Is there any solution to this problem?
I would be glad to know.
source code
Zip::File.open_buffer(obj) do |zip|
zip.each do |entry|
ext = File.extname(entry.name)
file_name = File.basename(entry.name)
# file_name.encode!("UTF-8", "UTF-8", invalid: :replace)
next if ext.blank? || file_name.count(".") > 1
dir = File.join(dir_name, File.dirname(entry.name))
FileUtils.mkpath(dir.to_s)
zip.extract(entry, dir + ".txt" || ".jpg" || ".csv") {true}
file_name.force_encoding("UTF-8")
new_file_name = "#{dir_name}/#{file_name}"
new_file_name.force_encoding("UTF-8")
File.rename(dir + ".txt" || ".jpg" || ".csv", new_file_name)
@input_dir << new_file_name
end
end
Zip::OutputStream.open(zip_file.path) do |zip_data|
@input_dir.each do |file|
zip_data.put_next_entry(file)
zip_data.write(File.read(file.to_s))
end
end
environment
mac OS Catarina 10.15.7 ruby "2.6.3"

file_name.codepointsalong with the expected result?65533is the replacement character, i.e. �. It seems like you ran the code after the conversion? Sorry for not being clear. Please runentry.name.codepointsand alsoentry.name.encodingand post their output.invalid byte sequence in UTF-8. Just tried outputting file_name.codepoints with the expected file name of the same name. [87, 78, 83, 95, 85, 80, 29992, 12486, 12441, 12540, 12479, 46, 116, 120, 116] entry.name.encoding is UTF-8.entry.name.bytesthen.