0

I'm using DotNetZip to add multiple MemoryStreams to a single archive. So far, my code works when I select 1 or 2 files, but does not work if I add more. I found the difference is the CRC32 are all 00000000 for those bad archive. Is it something about the archive size? Any help is appreciated! My code in C#:

foreach(.....){
       var zipEntryName=.....//Get the file name in string;
       var UDocument = .....//Get a object
       var UStream = UDocument .GetStream();
       UStream.Seek(0, SeekOrigin.Begin);
       ZipEntry entry = zipFile.AddEntry(zipEntryName,UStream );
 }
 var outputStream = new MemoryStream();
            outputStream.Seek(0, SeekOrigin.Begin);
            zipFile.Save(outputStream);
            outputStream.Flush();
            return outputStream;
Steven Zack
  • 4,984
  • 19
  • 57
  • 88

1 Answers1

0

I think its beacuse of memory leakage. you are creating object in foreach loop and here the problem comes if the loop iterates more times.

here the problem comes in your code:

var UDocument = .....//Get a object

a singleton is a class that can be instantiated once, and only once. use singleton class as below:

public static SingletonSample InstanceCreation()
{
    private static object lockingObject = new object();
    if(singletonObject == null)
    {
         lock (lockingObject)
         {
             singletonObject = new SingletonSample();

         }
    }
    return singletonObject;
}
ssube
  • 47,010
  • 7
  • 103
  • 140
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • First sentence looks right, but I don't think singleton is an appropriate solution. – Ben Voigt Nov 01 '13 at 17:00
  • @Be : Thanks , could you please show me this statement -> var UDocument = .....//Get a object – Sudhakar Tillapudi Nov 01 '13 at 17:13
  • No, since it isn't my question and isn't my code. I just don't see any reason to use a singleton here. – Ben Voigt Nov 01 '13 at 18:09
  • You also shouldn't spell singleton as `SingleTon`. The middle `T` is never capitalized, and doing so will confuse a lot of people. – ssube Nov 01 '13 at 20:04
  • I really don't think that's a solution for my code issue. Otherwise, how do you explain it works for 1 or 2 loop, but fails for more items in the foreach loop. But thanks for posting your solution. – Steven Zack Nov 01 '13 at 20:56