c# - overwrite file in Zipping -
i try zip .pdf files in c#. code works fine when size of 1 of pdfs big, going overwrite pdf on rest of pdfs. not sure happening. tried increase size of buffer or zip file still same issue. have suggestion?
this code:
public void processziprequest(string strqueueid, string strbatchid, string strftppath) { int intreportcnt = 0; string strzipfilename = "order-" + strbatchid + "-" + strqueueid + "-" + datetime.now.tostring("mm-dd-yyyy-hh-mm") + ".zip"; strzipfilename = safefilename(strzipfilename); //memorystream ms = new memorystream(); filestream ms = new filestream(@"c:\surf\nikoo.zip", filemode.create); zipoutputstream ozipstream = new zipoutputstream(ms); // create zip stream ozipstream.setlevel(9); // maximum compression intreportcnt += 1; string strrptfilename=string.empty; memorystream outputstream = new memorystream(); if (strqueueid != null) { string[] filenames = directory.getfiles(@"c:\uploaded"); // setting report name path given report name foreach (string filename in filenames) { strrptfilename = filename.substring(filename.lastindexof("\\") + 1); filestream fs = file.openread(@"c:\uploaded\" + strrptfilename); int buffersize = 2048; int readcount; byte[] buffer = new byte[buffersize]; readcount = fs.read(buffer, 0, buffersize); while (readcount>0) { outputstream.write(buffer, 0, readcount); readcount = fs.read(buffer, 0, buffersize); } fs.close(); outputstream.position = 0; zipfile(ref outputstream, strrptfilename, ref ozipstream); } } outputstream.close(); ozipstream.finish(); ozipstream.flush(); ozipstream.isstreamowner = false; // false stops close closing underlying stream. ozipstream.close(); // must finish zipoutputstream before using outputmemstream. ms.close(); }
and zipfile method:
public void zipfile(ref memorystream msfile, string strfilename, ref zipoutputstream ozipstream) { zipentry ozipentry = new zipentry(strfilename); ozipentry.datetime = datetime.now; ozipentry.size = msfile.length; ozipstream.putnextentry(ozipentry); streamutils.copy(msfile, ozipstream, new byte[4096]); ozipstream.closeentry(); }
i found issue. have create new memoystream in loop , close @ end of loop.
foreach (string filename in filenames) { strrptfilename = filename.substring(filename.lastindexof("\\") + 1); outputstream = new memorystream(); filestream fs = file.openread(@"c:\uploaded\" + strrptfilename); int buffersize = 2048; int readcount; byte[] buffer = new byte[buffersize]; readcount = fs.read(buffer, 0, buffersize); while (readcount>0) { outputstream.write(buffer, 0, readcount); readcount = fs.read(buffer, 0, buffersize); } fs.close(); outputstream.position = 0; zipfile(ref outputstream, strrptfilename, ref ozipstream); fs.close(); outputstream.close(); }
Comments
Post a Comment