[R] zlib

Ben Stabler bstabler at ptvamerica.com
Wed Jan 5 18:52:54 CET 2005


I need to decompress an array of bytes that were compressed using zlib.  The
bytes are stored within a binary file.  I can seek to the location, read the
length of the compressed byte array, and read the bytes into R.  What I need
to do then is decompress the byte array to an array of doubles.  

The code looks something like this:

compresslength = readBin(infile, integer(), 1, 4)
data = readBin(infile, integer(), compresslength, 1)
#need something here like..... result = zlibDecompress(data)

I implemented it in Python using the zlib module and the decompress method.
The Python code is:

data = file.read(compresslength) #read x number of bytes
result = struct.unpack("3d", zlib.decompress(data)) #decompress data and
convert bytes to 3 doubles

I also tried:

data = readBin(gzcon(infile), integer(), compresslength, 1) 

But it returns what I asked for....a vector of 1 bytes with a length equal
to compresslength.  It seems that the arguments to readBin require
prespecification of the result, which I can't really provide.  gzfile and
gzcon wrap a connection, but then in order to readBin from the connection, I
need to specify the data type, the number of items, and the number of bytes
per item.  But I don't know this since zlib did the compressing.  The gzip
website says to use the zlib library for in-memory compression, so maybe
what I am trying to do cannot be done with what currently exists in R.  

The other issue in this is the conversion of the bytes to doubles.  Since I
have a lot of compress values - ~ 250000 - I'm guessing that I can't afford
to write a little R function to operate on a vector of 1 byte integers.  So
that is why I was hoping to find a way to do it with existing (compiled C
code) R.

Maybe I am missing something...I don't know :)  Thanks for your help.

Ben Stabler
PTV America, Inc.
1128 NE 2nd St, Suite 204
Corvallis, OR 97330
541-754-6836 x205
541-754-6837 fax
www.ptvamerica.com




More information about the R-help mailing list