def download<o:p></o:p>
filename='filename' #文件名 filename.zip<o:p></o:p>
if FileTest.file?('public/date/'+filename+'.zip') <o:p></o:p>
File.delete('public/date/'+filename+'.zip') <o:p></o:p>
End #如果文件已经存在,则先删除<o:p></o:p>
a='abcdefg'<o:p></o:p>
#\puts a.<o:p></o:p>
#读取数据库的内容<o:p></o:p>
@picture = Picture.find(:first)<o:p></o:p>
@data = @picture.picture_data<o:p></o:p>
Zip::ZipFile.open('public/date/'+filename+'.zip', Zip::ZipFile::CREATE) { #打开一个文件<o:p></o:p>
|zipfile|<o:p></o:p>
text=text+"内容"
text=text+"\r\n"<o:p></o:p>
zipfile.file.open("index.txt","w") { |f| f.puts text } #把数据库的内容写到文本文件里去<o:p></o:p>
<o:p> </o:p>
zipfile.dir.mkdir("mydir") #新建目录<o:p></o:p>
zipfile.file.open("mydir/index.txt", "w") { |f| f.puts @tempt1 }<o:p></o:p>
#把文本放在新建的目录下<o:p></o:p>
zipfile.file.open("mydir/second.jpg", "w") { |f| f.puts(@data) }<o:p></o:p>
#把数据库里的图片放在新建的目录下 <o:p></o:p>
}<o:p></o:p>
<o:p></o:p>
send_file 'public/date/'+filename+'.zip', :filename => filename+'.zip', :type => "application/octet-stream" #打包下载<o:p></o:p>
end
本文介绍了一种使用Ruby语言将包含图片在内的数据打包为ZIP文件的方法。该过程涉及检查并删除已存在的同名ZIP文件、从数据库中获取图片数据、创建ZIP文件并添加图片及文本内容。
1174

被折叠的 条评论
为什么被折叠?



