Question

This code works fine

ZipStorer storer = ZipStorer.Create(@"H:\temp\sysInfo.zip", "");
if (systemInfoStream != null)
   storer.AddStream(ZipStorer.Compression.Deflate, "SystemInfo.txt", 
                    systemInfoStream, DateTime.Now, null);  

But when I try to use inner stream of ZipStorer manually is does not work fine:

MemoryStream result = new MemoryStream();
ZipStorer storer =ZipStorer.Create(new MemoryStream(), "");
if (systemInfoStream != null) {
    storer.AddStream(ZipStorer.Compression.Deflate, "SystemInfo.txt", 
                     systemInfoStream, DateTime.Now, null);
    storer.ZipFileStream.Position = 0;
    storer.ZipFileStream.CopyTo(result);
}
File.WriteAllBytes(@"H:\temp\sysInfo.zip", result.ToArray());

The result of the second sample is the damaged zip-file which can be read after repairing with WinRar. The first sample works just fine. The only significant difference that I see is that in the second sample I dont add file to the storer explicitly.

Was it helpful?

Solution

Looks like you should call something like storer.Close() to write a central directory before saving data to file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top