问题如下,使用下面的形式压缩的 zip 文件,下载的 zip 解压时报错,csv 无法打开,求大神挑毛病,给建议: 代码:
dir = Dir.mktmpdir
FileUtils.rm_rf(dir) if File.exists?(dir)
FileUtils::mkdir_p(dir)
csv_file_path = File.join(dir, filename + '.csv')
zip_file_path = File.join(dir, filename + '.zip')
CSV.open(csv_file_path, 'w') do |csv|
csv_column_names = csv_columns.split(',')
csv << csv_column_names
resources.each do |record|
csv_line = record.attributes.values_at(*csv_column_names)
csv_line.map! do |column|
column.instance_of?(ActiveSupport::TimeWithZone) ? column.iso8601 : column
end
csv << csv_line
end
end
Zip::File.open(zip_file_path, Zip::File::CREATE) do |zip|
zip.add("#{filename}.csv", csv_file_path)
end