my method takes list , creates zip file , log file. works smaller amounts of files. when go large amount of files (tested 11,000) throws systemoutofmemory exception.
by research learned method puts lot of load in memory. put in part flush streamwriter , zip archive. have file stream.
what efficient approach solve problem?
here code:
public static void backupfilestozip(string directory, string filefilter, string zipfilepath, bool backupinsubdir, string logfilepath, list<fileinfo> filestobackup) { fileinfo logfile = new fileinfo(logfilepath); fileinfo zipfile = new fileinfo(zipfilepath); int numberoffiles = filestobackup.count; if (!directory.exists(zipfile.directoryname)) directory.createdirectory(zipfile.directoryname); using (filestream ziptoopen = new filestream(zipfile.fullname, filemode.openorcreate)) { using (ziparchive archive = new ziparchive(ziptoopen, ziparchivemode.update)) { ziparchiveentry readmeentry = archive.createentry(logfile.name); using (streamwriter writer = new streamwriter(readmeentry.open())) { writer.writeline("this zip archive created: " + datetime.now.tostring("mm/dd/yy hh:mm:ss")); writer.writeline("zip file: " + zipfilepath); writer.writeline("source directory: " + directory); string backupinsubtext = "yes"; if (!backupinsubdir) backupinsubtext = "no"; writer.writeline("subdirectories included: " + backupinsubtext); writer.writeline("filter critera: " + filefilter); writer.writeline("number of files selected: " + numberoffiles + " (for # of files archived/skipped scroll down)"); writer.writeline(""); writer.writeline("file log:"); int filesarchivedcounter = 0; int filesskippedcounter = 0; int filessum = 0; taskbarmanager.instance.setprogressstate(taskbarprogressbarstate.normal); foreach (fileinfo file in filestobackup) { //ziparchiveentry readmeentry = archive.createentry(logfile.name); string datetimestampbegin = datetime.now.tostring("mm/dd/yy hh:mm:ss"); try { string relativepath = makerelativepath(directory, file.fullname); archive.createentryfromfile(file.fullname, relativepath); writer.writeline(datetimestampbegin + " - " + datetime.now.tostring("hh:mm:ss") + " archived: " + file.fullname); filesarchivedcounter++; } catch { writer.writeline(datetimestampbegin + " - " + " skipped: " + file.fullname); filesskippedcounter++; } filessum = filesskippedcounter + filesarchivedcounter; taskbarmanager.instance.setprogressvalue(filessum, numberoffiles); //write memory files every 75 items (to avoid out of memory exception) if (filessum % 75 == 0) { writer.flush(); ziptoopen.flush(); } } writer.writeline(""); writer.writeline("# of files archived: " + filesarchivedcounter); writer.writeline("# of files skipped: " + filesskippedcounter); } if (!string.isnullorempty(logfile.name)) { if (!directory.exists(logfile.directoryname)) directory.createdirectory(logfile.directoryname); readmeentry.extracttofile(logfile.fullname, true); } taskbarmanager.instance.setprogressstate(taskbarprogressbarstate.noprogress); } } } all string parameters of method log file.
the problem in implementation putting log entry @ beginning of archive , update after adding new files, zip cannot flushed. should write log file, not archive, , add archive when files added.
Comments
Post a Comment