How you zip larger files in ruby

작성자

카테고리:

← 피드로
DEV Community · luiz filipe neves · 2026-06-22 개발(SW)

luiz filipe neves

Easy way that compress files in ruby is using zip lib.

Zip::File.open('./compress.zip', create: true) do |zip|
  zip.get_output_stream("./stuff_to_ziped.pdf") do |f|
    f.puts File.read("./stuff_to_zip.pdf")
   end
end

Enter fullscreen mode Exit fullscreen mode

When you need to zip larger files or when you need to perform any task that requires opening many files, good practice is to avoid loading all files into memory. To achieve this, you can use stream operations available in IO classes.

Let’s look at the code above again but with a thread that monitory the memory for us.

Thread.new do
  loop do
    print "r memory: %d MB" % "#{`ps -o rss= -p #{Process.pid}`.to_i/1024}t"
  end
end

Zip::File.open('./compress.zip', create: true) do |zip|
  zip.get_output_stream("./stuff_to_ziped.pdf") do |f|
    f.puts File.read("./stuff_to_zip.pdf")
   end
end

Enter fullscreen mode Exit fullscreen mode

Now you can see how much memory the program consumis until done. The method File.read load all bytes of file in memory. Lets see other implementation.

Thread.new do
  loop do
    print "r memory: %d MB" % "#{`ps -o rss= -p #{Process.pid}`.to_i/1024}t"
  end
end


Zip::File.open('./compactados.zip', create: true) do |zip|
    10.times do |i|
        zip.get_output_stream("./copy_#{i}.pdf") do |f| 
            input = File.new("./copy_#{i}.pdf")
            while !input.eof?
                f.puts(input.gets)
            end
        end
    end
end

Enter fullscreen mode Exit fullscreen mode

원문에서 계속 ↗

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다