c# - How can I unzip a file to a .NET memory stream? -
i have files (from 3rd parties) being ftp'd directory on our server. download them , process them 'x' minutes. works great.
now, of files .zip
files. means can't process them. need unzip them first.
ftp has no concept of zip/unzipping - i'll need grab zip file, unzip it, process it.
looking @ msdn zip api, there seems no way can unzip memory stream?
so way this...
- unzip file (what directory? need -very- temp location ...)
- read file contents
- delete file.
note: contents of file small - 4k <-> 1000k.
zip compression support built in:
using system.io; using system.io.compression; // ^^^ requires reference system.io.compression.dll static class program { const string path = ... static void main() { using(var file = file.openread(path)) using(var zip = new ziparchive(file, ziparchivemode.read)) { foreach(var entry in zip.entries) { using(var stream = entry.open()) { // whatever want stream // ... } } } } }
normally should avoid copying stream - use "as is", however, if absolutely need it in memorystream
, do:
using(var ms = new memorystream()) { stream.copyto(ms); ms.position = 0; // rewind // ms }
Comments
Post a Comment