Forum: Ruby Delayed File I/O & ZLIB - Any help would be appreciated

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Brian C. (Guest)
on 2006-03-17 00:29
(Received via mailing list)
Folks,

Hey Folks,

I've written a script to generate a list of all files on a machine,
then zip the results.  Im going to use it to audit some file servers.
The problem is that the ZIP compression/decrompression isn't working,
but its not what you might think.  The script completes, but seems to
not actually write anything.

Here is a short version that only looks at the C:/temp drive.  You can
change this at the top.

---Start Code Here--
require 'find'
require 'csv'
require 'zlib'

tempfile = "temp.csv"
outfile  = "out.zip"
dirname = "c:/temp"

# Prepare comma-delimited file for writing
CSV::Writer.generate(File.open(tempfile,"w+"), ',') do |csv|
      Find.find(dirname) do |f|
	  csv << [File.dirname(f), File.basename(f), File.mtime(f).to_s]
      end
end

# TEST: View the tempfile to make sure it has data.
#  This prints nothing, but the file DOES have info after
# the script completes.  Maybe the file isn't written at this point?
p IO.read(tempfile)

Zlib::GzipWriter.open(outfile) do |gz|
  gz << File.open(tempfile).read
end

Zlib::GzipReader.open(outfile) {|gz|
  print gz.read
}

# The ZIP file can't be extracted with WINZIP.  No idea why, but since
# I'll be using another RUBY script for extraction, im not sure that I
care.
Jeffrey S. (Guest)
on 2006-03-17 02:04
(Received via mailing list)
Brian C. wrote:
> Here is a short version that only looks at the C:/temp drive.  You can
>
> p IO.read(tempfile)
Are you sure that's not printing anything?  It should be print exactly
one line of output.

> care.
You didn't zip the file, you gzipped it.  I happen to prefer gzip, but
if you want zip, this might help:

	http://rubyzip.sourceforge.net/
(Guest)
on 2006-03-17 16:25
(Received via mailing list)
hey Jeffrey,

Thanks for the reply.  I realized the ZIP thing from another poster.  I
guess I thought that they were equivalent, but either way, GZ is fine.

Also, the line that:
read gz << File.open(tempfile).read
was replaced by
read gz << File.read(tempfile)

My output at this point is:
""
nil

Best,
Brian
(Guest)
on 2006-03-17 16:31
(Received via mailing list)
Ok, well I figured out part of the issue.  The CSV file isn't being
written before the Zlib call.  If I break this up into two scripts, the
first that writes the file,  the second that zips it, things work.

Any idea why the CSV file wouldn't be saved when the block ends?
Dimitri A. (Guest)
on 2006-03-17 16:46
(Received via mailing list)
On 3/17/06, removed_email_address@domain.invalid 
<removed_email_address@domain.invalid> wrote:
> Ok, well I figured out part of the issue.  The CSV file isn't being
> written before the Zlib call.  If I break this up into two scripts, the
> first that writes the file,  the second that zips it, things work.
>
> Any idea why the CSV file wouldn't be saved when the block ends?

The CSV::Writer::generate doesn't implicity close the file you've
opened in it's arguments.  This file is not closed until the script
exits, unless you explicity close the file, or open the file with a
block.  You might want to try something like:

CSV.open(tempfile,"w+", ',') do |csv|
     Find.find(dirname) do |f|
         csv << [File.dirname(f), File.basename(f), File.mtime(f).to_s]
     end
end

Note: I haven't tested the above, just looked at 'ri CSV.open'.

- Dimitri
This topic is locked and can not be replied to.