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

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.

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/

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

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?

On 3/17/06, [email protected] [email protected] 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