How you zip larger files in ruby

●2 ●6
calendar_today • schedule1 min read
— Originally published at dev.to

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

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

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
🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

Three Design-to-Code Rules Most Developers Ignore (That Will Make You a Better Engineer)

Joemetry - Sep 26

Core Web Vitals Benchmarks for Shopify Stores (2026 Data)

ApogeeWatcherverified - Sep 16

How Lighthouse Performance Scores Are Recorded and Calculated

ApogeeWatcherverified - Aug 18

Cloudflare Blocked My Server, So I Made My Visitors' Browsers Do the Fetching

bryanhamiltondevverified - Nov 15

The Memory Leak in addEventListener You’re Ignoring

Joemetry - Oct 15
chevron_left
163 Points • 8 Badges
2Posts
0Comments
5Connections
Software Engenier

Related Jobs

View all jobs →

Commenters (This Week)

3 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!