Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am consuming a webservice through NETLink that returns data directly in the response as a gzipped csv file.

I can successfully process the data like this:

binz=response@data
BinaryWrite["tmp.csv.gz", binz]
Close["tmp.csv.gz"]
Import["tmp.csv.gz", "CSV"]

However, I am curious if there is a more direct way to achieve this? Thanks for any advice.

share|improve this question
add comment

2 Answers 2

The following seems to work:

(* Simulate your response *)
binz = ExportString[ {"A System for Doing", "Mathematics by Computer"}, {"GZIP", "CSV"} ];

(*Uncompress and read CSV*)
ImportString[binz, {"GZIP", "CSV"}]
(*
{{"A System for Doing"}, {"Mathematics by Computer"}}

*)

You may prefer to post a small data sample to test with your actual data format.

share|improve this answer
add comment

Base64 binary to text encoding may help, ie:

turtle = Import["ExampleData/turtle.jpg"];
gif = ExportString[turtle, "GIF"];
encoded = ExportString[gif, "Base64"];
ImportString[encoded, "Base64"]

enter image description here

However, Mathematica 7 requires this method:

turtle = Import["ExampleData/turtle.jpg"];
encoded = ExportString[turtle, {"Base64", "GIF"}];
ImportString[encoded, "Base64"]

For sending through NETLink you can use this:

kernel.Compute("ExportString[Graphics[Rectangle[]],{\"Base64\",\"EMF\"}]");
byte[] decodedBytes = Convert.FromBase64String(kernel.Result.ToString());
File.WriteAllBytes("C:\\Temp\\output.emf", decodedBytes);
share|improve this answer
    
+1. Did not know the form ExportString[expr, {"format", "format", ...}]. Where is it documented? It is equivalent to Fold[ExportString, expr, Reverse@{"format", "format", ...}]. For example, try: ExportString[Graphics[Rectangle[]], {"UUE", "Base64", "UUE", "EMF"}] === Fold[ExportString, Graphics[Rectangle[]], Reverse@{"UUE", "Base64", "UUE", "EMF"}]. –  Alexey Popkov Jun 11 '12 at 8:48
    
As far as I know the ExportString/ImportString combo uses temporary files to work on. So, although you're not explicitely using files, you're still doing it under the hood. –  Sjoerd C. de Vries Jun 11 '12 at 9:05
    
@ Alexey - I believe I found this syntax on the MathGroup. It doesn't seem to be eqivalent to Fold at least on Mma 7, since this does not work: encoded = Fold[ExportString, turtle, {"GIF", "Base64"}]; –  Chris Degnen Jun 11 '12 at 9:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.