Last active
November 28, 2020 01:56
-
-
Save nikukyugamer/e7755e1203afc16bf3132427b2339c13 to your computer and use it in GitHub Desktop.
zipファイルに格納されているファイルが文字化けしているか(一定の規則に沿っているか)どうかを確かめる
This file contains hidden or 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 mojibake_shiteru?(stdout) | |
error_info = nil | |
begin | |
stdout.split("\n") | |
rescue ArgumentError | |
require 'English' | |
error_info = $ERROR_INFO | |
end | |
# 「UTF-8 の場合が NG」なので「nil ではないこと」が true を返す(=文字化けをしていない)条件になっている | |
# TODO: 英数字だけで構成されたファイルだと誤判定をするかもしれない | |
!error_info.nil? | |
end | |
base_dir = 'app/assets/files' | |
zip_filenames = Dir.entries(base_dir).select { |filename| filename.end_with?('.zip') } | |
zip_filenames.each do |zip_filename| | |
zip_filepath = "#{base_dir}/#{zip_filename}" | |
command = `unzip -l #{zip_filepath}` | |
puts "#{zip_filename}: #{mojibake_shiteru?(command)}" | |
end | |
# .circleci/config.yml | |
# - run: | |
# name: Check Mojibake by Ruby | |
# command: | | |
# /usr/local/bin/ruby ~/repo/.circleci/check_mojibake.rb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment