项目中有需求要把统计的csv文件压缩成zip,并且加密之后发送到指定的邮箱中。
调查发现ruby对于zip的第三方lib没有免费支持这种加密操作的,
一次偶然的机会在php的论坛中发现了灵感,可以直接调用zip命令,呵呵
http://forums.devshed.com/php-development-5/how-to-create-password-protected-zip-files-using-php-550248.html
echo system('zip -P pass file.zip file.txt');
ruby中也可以这么做
示例如下:
zip_filepath = File.join(folder, zip_filename)
csv_filepath = File.join(folder, csv_filename)
log_info "generating csv ..."
export_csv(day)
log_info "zip csv ..."
encrypted_success = system("zip -P 1231456 #{zip_filepath} #{csv_filepath}");
unless encrypted_success
zip_csv(zip_filepath, csv_filepath)
end
log_info "delivering mail ..."
UserNotify.deliver_csv_zip(day, mail_to, zip_filepath)
log_info "Mail with zipfile have deliveried to #{mail_to}"
其次,在不支持zip加密的地方,用ruby的zip库直接生产zip文件
def self.zip_csv(path, filename, day = Date.today)
Zip::ZipFile.open(path, Zip::ZipFile::CREATE) {
|zipfile|
zipfile.add("rate.csv", filename)
}
end
调查发现ruby对于zip的第三方lib没有免费支持这种加密操作的,
一次偶然的机会在php的论坛中发现了灵感,可以直接调用zip命令,呵呵
http://forums.devshed.com/php-development-5/how-to-create-password-protected-zip-files-using-php-550248.html
echo system('zip -P pass file.zip file.txt');
ruby中也可以这么做
示例如下:
zip_filepath = File.join(folder, zip_filename)
csv_filepath = File.join(folder, csv_filename)
log_info "generating csv ..."
export_csv(day)
log_info "zip csv ..."
encrypted_success = system("zip -P 1231456 #{zip_filepath} #{csv_filepath}");
unless encrypted_success
zip_csv(zip_filepath, csv_filepath)
end
log_info "delivering mail ..."
UserNotify.deliver_csv_zip(day, mail_to, zip_filepath)
log_info "Mail with zipfile have deliveried to #{mail_to}"
其次,在不支持zip加密的地方,用ruby的zip库直接生产zip文件
def self.zip_csv(path, filename, day = Date.today)
Zip::ZipFile.open(path, Zip::ZipFile::CREATE) {
|zipfile|
zipfile.add("rate.csv", filename)
}
end