我想使用rubyzip来压缩一个现有的文件:
c:\textfile.txt
至
textfile.zip
我知道如何将流添加到文本文件中:
require 'zip/zip'
Zip::ZipFile.open("mp.zip", Zip::ZipFile::CREATE) {
|zipfile|
zipfile.get_output_stream("text.txt") { |f| f.puts "Creating text file" }
}而不是如何将现有文件添加到zip中。谢谢你的帮忙
发布于 2010-09-29 08:21:51
这将读入源文件,并将其一次1mb写入zipfile。
我在生产中使用类似的东西已经有一段时间了。
require 'zip/zip'
Zip::ZipFile.open("mp.zip", Zip::ZipFile::CREATE) do |zipfile|
zipfile.get_output_stream("text.txt") do |out_file|
File.open("text.txt") do |in_file|
while blk = in_file.read(1024**2)
out_file << blk
end
end
end
end希望这能回答你的问题。
https://stackoverflow.com/questions/3817426
复制相似问题