class SomeController < ApplicationController
def some_action
file_stream = Zip::ZipOutputStream.write_buffer do |zip|
zip.put_next_entry "dir1/hello.txt"
zip.print "Hello"
zip.put_next_entry "dir2/hello.txt"
zip.print "World"
end
file_stream.rewind
respond_to do |format|
format.zip do
send_data file_stream.read, filename: "zip_file.zip"
end
end
end
end
例では二つのファイルを動的に作成してに書き込まれ、その後、ZIPファイルに保存されます。
しかし、既に存在するファイル(!)をZIPファイルに追加するにはどうすればよいですか?私の/app/assets/documents
フォルダのPDFファイルですか?
これははるかに簡単に達成できるはずですが、私はそれに関するドキュメントを見つけることができません。
ありがとうございました。ここで
'zip.put_next_entry" filename "を実行できますか? zip << File.binread( "file/path/and/filename") '? – matt
それはうまくいった!ありがとう、@マット! – Tintin81