ruby 解压zip文件方法以及遇到的问题
wiki:
ruby代码:
def self.unzip_file zip_file_path
if Rails.env == "development"
self.window_unzip_file zip_file_path
else
self.linux_unzip_file zip_file_path
end
end
def self.window_unzip_file zip_file_path, file_type, company_id
if File.exists?(zip_file_path)
Zip::File.open(zip_file_path) do |zip_file|
zip_file.each do |f|
f_path = File.join(pdf_path, f.name)
FileUtils.rm_rf(f_path) if File.exists?(f_path)
zip_file.extract(f, f_path) #unless File.exist?(f_path)
end
end
end
end
def self.linux_unzip_file zip_file_path
zip_path = File.dirname(zip_file_path)
zip_name = File.basename(zip_file_path)
system("LANG=en_US.UTF-8;cd #{zip_path};unzip -o #{zip_name}")
end
遇到的问题 linux解压之后名字出现#U451#U936l编码,编码不对,指定编码LANG=en_US.UTF-8即可
- encdoing用来查看字符串的编码信息。
- force_encoding用来修正字符串编码信息,注意是修正。
- encode, encode!用来转码字符串。

浙公网安备 33010602011771号