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

**URL:** <https://www.ruby-forum.com/t/delayed-file-i-o-zlib-any-help-would-be-appreciated/82894>\
**Category:** Ruby\
**Created:** [March 16, 2006, 11:29pm UTC](https://www.ruby-forum.com/t/delayed-file-i-o-zlib-any-help-would-be-appreciated/82894 "2006-03-16T23:29:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [March 16, 2006, 11:29pm UTC](https://www.ruby-forum.com/t/delayed-file-i-o-zlib-any-help-would-be-appreciated/82894/1 "2006-03-16T23:29:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [March 17, 2006, 1:04am UTC](https://www.ruby-forum.com/t/delayed-file-i-o-zlib-any-help-would-be-appreciated/82894/2 "2006-03-17T01:04:06Z")

</div>

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/
```

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [March 17, 2006, 3:25pm UTC](https://www.ruby-forum.com/t/delayed-file-i-o-zlib-any-help-would-be-appreciated/82894/3 "2006-03-17T15:25:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [March 17, 2006, 3:31pm UTC](https://www.ruby-forum.com/t/delayed-file-i-o-zlib-any-help-would-be-appreciated/82894/4 "2006-03-17T15:31:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [March 17, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/delayed-file-i-o-zlib-any-help-would-be-appreciated/82894/5 "2006-03-17T15:46:56Z")

</div>

On 3/17/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto: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
