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

B

Brian Corrigan

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.
 
B

bcorrigan78

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
 
B

bcorrigan78

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top