c# - Raw Stream Has Data, Deflate Returns Zero Bytes -
i'm reading data (an adcenter report, happens), supposed zipped. reading contents ordinary stream, couple thousand bytes of gibberish, seems reasonable. feed stream deflatestream.
first, reports "block length not match complement." brief search suggests there two-byte prefix, , indeed if call readbyte() twice before opening deflatestream, exception goes away.
however, deflatestream returns nothing @ all. i've spent of afternoon chasing leads on this, no luck. me, stackoverflow, you're hope! can tell me i'm missing?
here's code. naturally enabled 1 of 2 commented blocks @ time when testing.
_results = new list<string[]>(); using (stream compressed = response.getresponsestream()) { // skip zlib prefix, conflicts deflate specification compressed.readbyte(); compressed.readbyte(); // reports reading 3,000-odd bytes, followed random characters /*byte[] buffer = new byte[4096]; int bytesread = compressed.read(buffer, 0, 4096); console.writeline("read {0} bytes.", bytesread.tostring("#,##0")); string content = encoding.ascii.getstring(buffer, 0, bytesread); console.writeline(content);*/ using (deflatestream decompressed = new deflatestream(compressed, compressionmode.decompress)) { // reports reading 0 bytes, , no output /*byte[] buffer = new byte[4096]; int bytesread = decompressed.read(buffer, 0, 4096); console.writeline("read {0} bytes.", bytesread.tostring("#,##0")); string content = encoding.ascii.getstring(buffer, 0, bytesread); console.writeline(content);*/ using (streamreader reader = new streamreader(decompressed)) while (reader.endofstream == false) _results.add(reader.readline().split('\t')); } }
as can guess last line, unzipped content should tdt.
just fun, tried decompressing gzipstream, reports magic number not correct. ms' docs "the downloaded report compressed using zip compression. must unzip report before can use contents."
here's code worked. had save content out file , read in. not seem reasonable, small quantities of data i'm working with, it's acceptable, i'll take it!
webrequest request = httpwebrequest.create(reporturl); webresponse response = request.getresponse(); _results = new list<string[]>(); using (stream compressed = response.getresponsestream()) { // save content temporary location string zipfilepath = @"\\server\folder\adcenter\temp.zip"; using (streamwriter file = new streamwriter(zipfilepath)) { compressed.copyto(file.basestream); file.flush(); } // first file temporary zip zipfile zipfile = zipfile.read(zipfilepath); if (zipfile.entries.count > 1) throw new applicationexception("found " + zipfile.entries.count.tostring("#,##0") + " entries in report; expected 1."); zipentry report = zipfile[0]; // extract data using (memorystream decompressed = new memorystream()) { report.extract(decompressed); decompressed.position = 0; // note stream not start @ beginning using (streamreader reader = new streamreader(decompressed)) while (reader.endofstream == false) _results.add(reader.readline().split('\t')); } }
you find deflatestream hugely limited in data decompress. in fact if expecting entire files of no use @ all. there hundereds of (mostly small) variations of zip files , deflatestream along 2 or 3 of them.
best way use dedicated library reading zip files/streams dotnetzip or sharpziplib (somewhat unmaintained).
Comments
Post a Comment