c# - Processing HttpPostedFile.InputStream before saving using SaveAs() -


i need check if file compressed before saving it. have created following extension method check if file compressed using zipfile (from sharplibzip library):

public static bool iscompressed(this httppostedfile postedfile) {     stream stream = postedfile.inputstream;      using (var zipfile = new zipfile(stream))     {         return zipfile.testarchive(true);     } } 

the problem when call httppostedfile.saveas() saves file 0kb:

if(uploadedfile.iscompressed()) {     uploadedfile.inputstream.seek(0, seekorigin.begin);     uploadedfile.saveas(physicalzipfile.fullname); } 

this has using stream in iscompressed(). have tried seeking beginning before saving problem remains.

could please explain problem here?

for stumbles upon this... here solution:

using ilspy decompiled assembly find zipfile.dispose() closes stream. there other cleanup code wouldn't recommend not disposing of zipfile instance.

on other hand, ziparchive (system.io.compression) has following overloaded constructor:

public ziparchive(stream stream, ziparchivemode mode, bool leaveopen) 

setting leaveopen true leave stream open after instance has been correctly disposed.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -