Add files to a gzip file

I’d like to add several separate files into a new Gzip file created
within ruby.

The code I’ve got at the moment is as follows:

File.open(‘coursework_submissions.gz’, ‘wb’) do |f|
gz = Zlib::GzipWriter.new(f)
@student_list.each do |s|
temp = File.open("#{s.student.student_no}", ‘wb’)
temp.write(s.submission)
temp.close()
gz.add(File.open("#{s.student.student_no}", ‘r’))
File.delete("#{s.student.student_no}")
end
gz.close
end

However, this just produces a file, coursework_submissions.gz,
containing a file, coursework_submissions with the text of the files I
want to add contained inside. Where am I going wrong?

Thanks everyone.

Martyn J. wrote:

However, this just produces a file, coursework_submissions.gz,
containing a file, coursework_submissions with the text of the files I
want to add contained inside. Where am I going wrong?
Gzip is not a container format.

Usually one use TAR to pack multiple files/objects into one archive and
than gzip it.

2008/2/26, Joachim G. [email protected]:

than gzip it.
Hi, I also need to compress a folder containing several jpg files to a
.zip file. I don’t want to generate a .tar.gz file, but a .zip file
that standard windows users can open (I’m an Ubuntu user but my client
is not).

How can I do that?


Jaime I.
http://jaimeiniesta.com - http://railes.net - http://pagerankalert.com

On 20 Mar 2008, at 22:24, Jaime I. wrote:

Gzip is not a container format.
How can I do that?
Use rubyzip (http://rubyzip.sourceforge.net/) or the command line
version of zip (install it if needed using “sudo apt-get install
zip”). Google around for some examples on rubyzip. Although I
personally would prefer the command line calls.

Best regards

Peter De Berdt

On Thu, 2008-03-20 at 22:24 +0100, Jaime I. wrote:

Usually one use TAR to pack multiple files/objects into one archive and
than gzip it.

Hi, I also need to compress a folder containing several jpg files to a
.zip file. I don’t want to generate a .tar.gz file, but a .zip file
that standard windows users can open (I’m an Ubuntu user but my client
is not).

How can I do that?


probably something like (untested)

system(’/usr/bin/zip tmp/some_archive.zip -i path/to/files/*.jpg’)
send_file “tmp/some_archive.zip”, :type => “application/zip”

Craig

Another issue to keep in mind, is if the operation takes a long time
to finish(over 60 seconds), the browser may timeout. So you might want
to do an ajax callback to check if the file is ready for download
instead of having the GET response on the browser waiting.

h

On Feb 25, 5:13 pm, Martyn J. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs