Johannes Graumann
2010-Jan-14 13:03 UTC
[R] memDecompress and zlib compressed base64 encoded string
Hi, I have zlib compressed strings (example is attached) and would like to decompress them using memDecompress ... I try this:> connection <- file("compressed.txt","r") > compressed <- readLines(connection) > memDecompress(as.raw(compressed),type="g")Error in memDecompress(as.raw(compressed), type = "g") : internal error -3 in memDecompress(2) In addition: Warning messages: 1: In memDecompress(as.raw(compressed), type = "g") : NAs introduced by coercion 2: In memDecompress(as.raw(compressed), type = "g") : out-of-range values treated as 0 in coercion to raw Can anyone nudge me into the right direction regarding this? Thanks, Joh -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: compressed.txt URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100114/e6345fea/attachment.txt>
Prof Brian Ripley
2010-Jan-14 21:37 UTC
[R] memDecompress and zlib compressed base64 encoded string
On Thu, 14 Jan 2010, Johannes Graumann wrote:> Hi, > > I have zlib compressed strings (example is attached)What is that file? Not gzip compression: gannet% file compressed.txt compressed.txt: ASCII text, with very long lines since gzip uses a magic header that 'file' knows about. And even if the header was stripped, such files are 8-bit and yours is ASCII. Try> x <- 'Johannes Graumann' > xx <- charToRaw(x) > xxx <- memCompress(xx, "g") > rawToChar(xxx)[1] "x\x9c\xf3\xca\xcfH\xcc\xcbK-Vp/J,\xcd\0052\001:\n\006\x90" to see what a real gzipped string looks like.> and would like to decompress them using memDecompress ... > > I try this: >> connection <- file("compressed.txt","r") >> compressed <- readLines(connection)You have not told us the 'at a minimum' information requested in the posting guide. But you should not expect that to read a binary file, especially not in a MBCS locale. We have readBin for that purpose.>> memDecompress(as.raw(compressed),type="g")I don't think you know what as.raw does: it does not convert bytes in a character string to raw (for which you need charToRaw). It is always a good idea to look at each stage of your computation:> as.raw(compressed)[1] 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [26] 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00> Error in memDecompress(as.raw(compressed), type = "g") : > internal error -3 in memDecompress(2) > In addition: Warning messages: > 1: In memDecompress(as.raw(compressed), type = "g") : > NAs introduced by coercion > 2: In memDecompress(as.raw(compressed), type = "g") : > out-of-range values treated as 0 in coercion to raw > > Can anyone nudge me into the right direction regarding this?> Thanks, Joh-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595